开发者

Correcting characters in a string?

开发者 https://www.devze.com 2023-01-16 15:51 出处:网络
I need to correct unwanted chars in a string. unwanted characters: \"c\" instead of \"ç\" \"i\" instead of \"ı\"

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

"c" instead of "ç" "i" instead of "ı" "u" instead of "ü" "g" instead o开发者_JS百科f "ğ" "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?


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.


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 ();
}


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.


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);
0

精彩评论

暂无评论...
验证码 换一张
取 消