0

我想替换文件中字符串内容中的字符。下面的字典将键显示为不需要的字符,我需要用字典中的值替换。

Dictionary<string, string> unwantedCharacters = new Dictionary<string, string>();
        unwantedCharacters["É"] = "@";
        unwantedCharacters["Ä"] = "[";
        unwantedCharacters["Ö"] = "\\";
        unwantedCharacters["Å"] = "]";
        unwantedCharacters["Ü"] = "^";
        unwantedCharacters["é"] = "`";
        unwantedCharacters["ä"] = "{";
        unwantedCharacters["ö"] = "|";
        unwantedCharacters["å"] = "}";
        unwantedCharacters["ü"] = "~";

这是我目前使用的代码,感觉执行时间太长了..

 for (int index = 0; index < fileContents.Length; index++)
        {
            foreach (KeyValuePair<string, string> item in unwantedCharacters)
            {
                if (fileContents.IndexOf(item.Key) > -1)
                {
                    fileContents = fileContents.Replace(item.Key, item.Value); // Replacing straight characters
                }
            }
        }

即,在两个级别循环..任何其他方式实现这一点..任何帮助将不胜感激

4

6 回答 6

3

由于您没有修改字符串的长度,因此如果您制作unwantedCharactersaDictionary<char, char>而不是<string, string>,您可以执行以下操作:

var charArray = fileContents.ToCharArray();
for (int i = 0; i < charArray.Length; i++)
{
    char replacement;
    if (unwantedCharacters.TryGetValue(charArray[i], out replacement))
        charArray[i] = replacement;
}
fileContents = new string(charArray);

性能O(n)与输入字符串的长度有关。

于 2013-02-22T09:13:47.297 回答
2

似乎 fileContents 在这里是一个字符串值。您可以简单地在字符串上调用替换。

foreach (KeyValuePair<string, string> item in unwantedCharacters)
{
    fileContents = fileContents.Replace(item.Key, item.Value); 
}
于 2013-02-22T08:59:13.450 回答
2

看这个答案:答案

但是在这段代码中放上你的字符:

IDictionary<string,string> map = new Dictionary<string,string>()
    {
       {"É", = "@"},
       {"Ä", = "["},
       {"Ö", = "\\"},
       ...
    };
于 2013-02-22T09:02:25.383 回答
2

为了替换字符串中的许多字符,请考虑使用StringBuilder 类。替换字符串中的一个字符会导致创建全新的字符串,因此效率非常低。试试下面的:

var sb = new StringBuilder(fileContents.Length);

foreach (var c in fileContents)
    sb.Append(unwantedCharacters.ContainsKey(c) ? unwantedCharacters[c] : c);

fileContents = sb.ToString();

我在这里假设您的字典包含字符 ( Dictionary<char, char>)。这是一个案例,只需发表评论,我将编辑解决方案。

我还假设,那fileContents是一个字符串。

您还可以使用LINQ代替StringBuilder

var fileContentsEnumerable = from c in fileContents
                             select unwantedCharacters.ContainsKey(c) ? unwantedCharacters[c] : c;

fileContents = new string(fileContentsEnumerable.ToArray());
于 2013-02-22T09:05:10.087 回答
1

你想建立一个过滤器。您处理文件的内容,并在处理文件时进行替换。

像这样的东西:

        using(StreamReader reader = new StreamReader("filename"))
        using (StreamWriter writer = new StreamWriter("outfile"))
        {
            char currChar = 0;
            while ((currChar = reader.Read()) >= 0)
            {
                char outChar = unwantedCharacters.ContainsKey(currChar)
                                   ? unwantedCharacters[currChar]
                                   : (char) currChar;
                writer.Write(outChar);
            }
        }

如果您的数据在内存中,则可以使用内存流,或者循环fileContents是字符串或字符数组。

这个解决方案是 O(n),其中 n 是文件的长度,这要归功于字典(请注意,您可以使用简单的稀疏数组而不是字典,并且您将获得相当多的速度)。

不要像其他人建议的那样遍历字典,因为每个替换都是 O(n),所以最终总时间为 O(n*d),d 是字典大小,因为你必须多次浏览文件。

于 2013-02-22T09:06:16.623 回答
0

删除foreach并替换为for从 0 到 的循环item.Count这篇文章会有所帮助,希望。

于 2013-02-22T09:07:23.630 回答