Replace方法不替换字符串中的字符

Moh*_*hfq 0 c# string replace

我有一个方法,应该删除字符串中的重复字符,并用i字符替换j字符,但替换不起作用!! 我的代码中有没有错误的陈述?

public string filter(string key)
        {
            string answer = "";
            string keyEdit = key;
            bool found = keyEdit.Contains('j');
            if (found)
            {
                keyEdit.Replace('j', 'i'); // Replace j character by i character 
            }
            answer = new string(keyEdit.Distinct().ToArray()); // Removing duplicate characters
            return answer;
        }
Run Code Online (Sandbox Code Playgroud)

Sam*_*sov 6

只需替换此行:

keyEdit.Replace('j', 'i');
Run Code Online (Sandbox Code Playgroud)

有了这个:

keyEdit=keyEdit.Replace('j', 'i');
Run Code Online (Sandbox Code Playgroud)

返回一个新字符串,其中当前实例中所有出现的指定字符串都替换为另一个指定的字符串.MSDN