猿问

如何在C#中替换字符串中的多个子字符串?

我必须替换字符串中的多个子字符串(输入字符串的最大长度为 32)。我有一本大字典,其中可以包含数百万个项目作为键值对。我需要检查每个单词是否存在于字典中,并替换为相应的值(如果存在于字典中)。输入字符串可以有多个尾随空格。


该方法被调用了数百万次,因此,它严重影响了性能。


代码中是否有任何优化范围或其他更好的方法来做到这一点。


public static string RandomValueCompositeField(object objInput, Dictionary<string, string> g_rawValueRandomValueMapping) {


if (objInput == null)

    return null;


string input = objInput.ToString();

if (input == "")

    return input;


//List<string> ls = new List<string>();

int count = WhiteSpaceAtEnd(input);

foreach (string data in input.Substring(0, input.Length - count).Split(' ')) {


    try {

        string value;

        gs_dictRawValueRandomValueMapping.TryGetValue(data, out value);

        if (value != null) {


            //ls.Add(value.TrimEnd());

            input = input.Replace(data, value);

        }

        else {

            //ls.Add(data);

        }

    }

    catch(Exception ex) {


    }


}


//if (count > 0)

//    input = input + new string(' ', count);

    //ls.Add(new string(' ', count));


return input;

}

编辑:


我在问题中遗漏了一件重要的事情。子字符串在输入字符串中只能出现一次。字典键和值具有相同数量的字符。


慕神8447489
浏览 255回答 1
1回答

杨魅力

这是一个方法,它将接受输入字符串,并通过查找“单词”(任何连续的非空格)来构建一个新字符串,然后检查该单词是否在字典中,如果找到,则将其替换为相应的值。Replace这将解决“子词”替换的问题(如果你有“hello hell”并且你想用“heaven”替换“hell”并且你不希望它给你“heaveno heaven”)。它还解决了交换问题。例如,如果您想将“yes no”中的“yes”替换为“no”,将“no”替换为“yes”,您不希望它首先将其变为“no no”,然后再变为“yes yes”。public string ReplaceWords(string input, Dictionary<string, string> replacements){&nbsp; &nbsp; var builder = new StringBuilder();&nbsp; &nbsp; int wordStart = -1;&nbsp; &nbsp; int wordLength = 0;&nbsp; &nbsp; for(int i = 0; i < input.Length; i++)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; // If the current character is white space check if we have a word to replace&nbsp; &nbsp; &nbsp; &nbsp; if(char.IsWhiteSpace(input[i]))&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // If wordStart is not -1 then we have hit the end of a word&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(wordStart >= 0)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // get the word and look it up in the dictionary&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // if found use the replacement, if not keep the word.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var word = input.Substring(wordStart, wordLength);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(replacements.TryGetValue(word, out var replace))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; builder.Append(replace);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; builder.Append(word);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Make sure to reset the start and length&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; wordStart = -1;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; wordLength = 0;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // append whatever whitespace was found.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; builder.Append(input[i]);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; // If this isn't whitespace we set wordStart if it isn't already set&nbsp; &nbsp; &nbsp; &nbsp; // and just increment the length.&nbsp; &nbsp; &nbsp; &nbsp; else&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(wordStart == -1) wordStart = i;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; wordLength++;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; // If wordStart is not -1 then we have a trailing word we need to check.&nbsp; &nbsp; if(wordStart >= 0)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; var word = input.Substring(wordStart, wordLength);&nbsp; &nbsp; &nbsp; &nbsp; if(replacements.TryGetValue(word, out var replace))&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; builder.Append(replace);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; else&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; builder.Append(word);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; return builder.ToString();}
随时随地看视频慕课网APP
我要回答