Click here to Skip to main content
15,881,139 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
I have a textbox1 Text map code

C#
string input = TextBox1.Text;
Dictionary<string, string> map = new Dictionary<string, string>();
map.Add("go", "GYO");
map.Add("oo", "OMO");
map.Add("Go", "OIO");

string temp = input;
foreach (var entry in map)
{
    string key = entry.Key;
    string value = entry.Value;
    temp = Regex.Replace(temp, key, match =>
    {
        bool isUpper = char.IsUpper(match.Value[0]);

        char[] result = value.ToCharArray();
        result[0] = isUpper
            ? char.ToUpper(result[0])
            : char.ToLower(result[0]);
        return new string(result);
    }, RegexOptions.IgnoreCase);



If i type : "go" it replaced to "GYO"
if i type : "oo" it replaced to "OMO"

BUT
IF I type : "Go" it also replaced to "GYO" it cant detect capital word.
when i already mapped "Go" for "OIO" but it shows result "GYO" which i was mapped for small letters "go".

My Question is :
How do i define result different for capital latter and small latter.

thanks in advanced.
Posted
Comments
phil.o 9-Dec-15 8:14am    
Surely the RegexOptions.IgnoreCase; if you want to handle different casings, then you shouldn't tell the regular expression engine not to handle them.
Try RegexOptions.Compiled instead.
doublecode 10-Dec-15 2:12am    
Firstly thanks for reply.

OK let me see is it works for me yet or not.

Anyway thanks
phil.o 10-Dec-15 2:21am    
You're welcome.
doublecode 10-Dec-15 2:26am    
Hello Bro,
I got solution. Anyway thanks a lot for your nice try ....

Have a great day.

1 solution

Why not:
C#
Dictionary<string, string> map = new Dictionary<string, string>();
map.Add("go", "GYO");
map.Add("oo", "OMO");
map.Add("Go", "OIO");

// usage

string mapItem;

if(map.TryGetValue(TextBox1.Text, out mapItem))
{
    // handle the valid map entry now in 'mapItem
}
else
{
    // handle invalid data in TextBox
}
 
Share this answer
 
Comments
johannesnestler 9-Dec-15 8:38am    
Yes, I think this would be much more efficent and clean...
doublecode 10-Dec-15 2:25am    
Works very nice and also clean code.

+9999999999999 Likes.....

thanks a lot..
doublecode 10-Dec-15 2:42am    
Opps...bro i got a problem.

If i type : "go" two time in textbox it goes to "else" statement.

but in previous code if i type "gogo"it was did "GYOGYO"
But your code is unable to do this.

could you give me another try kindly ?

However Thanks a lot for your nice try.
BillWoodruff 10-Dec-15 2:59am    
while you could have a Dictionary Key 'gogo' mapped to 'GYOGYO' Evidently, what you want is for the user to enter any number of valid "key terms" ... apparently without the need to separate them with some delimiter.

the issue of parsing the text in the TextBox so you can handle multiple entries with cases like "gogo" ... where there's no delimiter separating the terms ... that's a whole other complex task.

I suggest you post another question focused only on that task.

However, if you are the designer here, why not consider creating a custom TextBox that will make it easy for the user to enter only what you wish to allow ? Or, consider, requiring delimiters between each valid "key term" to make it easy to parse the text.

In that question, please make it clear exactly what you wish allow, and dis-allow the user to type in that TextBox.
doublecode 4-Mar-16 1:23am    
Thank you so much for your useful advice....About open a new post- ins't it possible to get a solution in (code solution) here ? or from you bro ?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900