tags:

views:

60

answers:

4

Hello all. I need to correct unwanted chars in a string. unwanted characters:

"c" instead of "ç" "i" instead of "ı" "u" instead of "ü" "g" instead of "ğ" "o" instead of "ö" "s" instead of "ş"

I have written this method. But it doesnt work.

public string UrlCorrection(string text)
    {
        text = (text.ToLower()).Trim();
        var length = text.Length;
        char chr;
        string newtext="";
        for (int i = 0; i < length; i++)
        {
            chr = text[i];
            switch (chr)
            {
                case 'ç':
                    newtext = text.Replace("ç", "c");
                    break;
                case 'ı':
                    newtext = text.Replace("ı", "i");
                    break;
                case 'ü':
                    newtext = text.Replace("ü", "u");
                    break;
                case 'ğ':
                    newtext = text.Replace("ğ", "g");
                    break;
                case 'ö':
                    newtext = text.Replace("ö", "o");
                    break;
                case 'ş':
                    newtext = text.Replace("ş", "s");
                    break;
                default:
                    break;
            }

        }
        newtext = text;
        return text;
    }

How do i implenet this task?

+1  A: 

Do it this way:

public string UrlCorrection (string text)
{
    StringBuilder correctedText = new StringBuilder (text);

    return correctedText.Replace("ç", "c")
                        .Replace("ı", "i")
                        .Replace("ü", "u")
                        .Replace("ğ", "g")
                        .Replace("ö", "o")
                        .Replace("ş", "s")
                        .ToString ();
}
Developer Art
It will not work, because he calls text.Replace every time. Only the last matched character will be replaced.
klausbyskov
Right. Didn't look at the code before writing the answer.
Developer Art
not. this not solved my problem. I dont know where is the problem.
berotomanya
thank you. This is better way.
berotomanya
+4  A: 

Basically you can just do this:

newtext = text.Replace("ç", "c"); 
newtext = newtext.Replace("ı", "i"); 
newtext = newtext.Replace("ü", "u"); 
newtext = newtext.Replace("ğ", "g"); 
newtext = newtext.Replace("ö", "o"); 
newtext = newtext.Replace("ş", "s"); 

No need for the switch/case/indexing craziness.

klausbyskov
+1  A: 

Maybe it's not working because you're trying to match the char's directly. My method works and I used unicode codes to match the special chars, using this unicode chart. You don't have to loop through each char because Replace() replaces all instances of that char.

public string UrlCorrection(string text)
{
    text = text.ToLower().Trim();
    text = text
        .Replace('\u00E7','c')
        .Replace('\u0131','i')
        .Replace('\u00FC','u')
        .Replace('\u011F','g')
        .Replace('\u00F6','o')
        .Replace('\u015F','s');

    return text;
}

I've tested this with your special chars and it works just fine for me.

jlafay
A: 

It looks like you come from a C background and fail to consider that strings are immutable in .net (as well as in Java).

Your function can return a new string, with all characters replaced by their substitutes, but the original string will be unchanged.

basically, you can take klausbyskov's version, but instead of calling it like this:

UrlCorrection(url);

you have to call e.g.

url=UrlCorrection(url);
ammoQ