views:

112

answers:

3

I have a string for example like " :)text :)text:) :-) word :-( " i need append it in textbox(or somewhere else), with condition:

Instead of ':)' ,':-(', etc. need to call function which enter specific symbol

I thinck exists solution with Finite-state machine, but how implement it don't know. Waiting for advises.

update: " :)text :)text:) :-) word :-( " => when we meet ':)' wec all functions Smile(":)") and it display image on the textbox

update: i like idea with delegates and Regex.Replace. Can i when meet ':)' send to the delegate parameter ':)' and when meet ':(' other parameter.

update: Found solution with converting to char and comparing every symbol to ':)' if is equal call smile(':)')

+7  A: 

You can use Regex.Replace with delegate where you can process matched input or you can simply use string.Replace method.

Updated:

you can do something like this:

        string text = "aaa :) bbb :( ccc";

        string replaced = Regex.Replace(text, @":\)|:\(", match => match.Value == ":)" ? "case1" : "case2"); 

replaced variable will have "aaa case1 bbb case2 ccc" value after execution.

Andrew Bezzub
+1 I like this answer a lot!
used2could
A: 

You could just create a dictionary with these specific characters as the key and pull the value.

used2could
+1  A: 

It seems that you want to replace portions of the string with those symbols, right? No need to build that yourself, just use string.Replace. Something like this:

string text = " :)text :)text:) :-) word :-( ";
text = text.Replace(":)", "☺").Replace(":(", "☹"); // similar for others
textbox.Text += text;

Note that this is not the most efficient code ever produced, but if this is for something like a chat program, you'll probably never know the difference.

Thomas