简体   繁体   English

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

[英]How to replace multiple substrings in a string in C#?

I have to replace multiple substrings from a string (max length 32 of input string). 我必须替换一个字符串中的多个子字符串(输入字符串的最大长度为32)。 I have a big dictionary which can have millions of items as a key-value pair. 我有一本大字典,其中有成千上万的项作为键值对。 I need to check for each word if this word is present in the dictionary and replace with the respective value if present in the dictionary. 我需要检查每个词是否在字典中存在,如果在字典中存在,则用相应的值替换。 The input string can have multiple trailing spaces. 输入字符串可以有多个尾随空格。

This method is being called millions of time, due to this, it's affecting the performance badly. 因此,这种方法被称为数百万次,这严重影响了性能。

Is there any scope of optimization in the code or some other better way to do this. 代码中是否有任何优化范围或执行此操作的其他更好方法。

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

EDIT: 编辑:

I missed one important thing in the question. 我错过了问题中的一件重要事情。 substring can occur only once inthe input string. 子字符串只能在输入字符串中出现一次。 Dictionay key and value have same number of characters. Dictionay键和值的字符数相同。

Here's a method that will take an input string and will build a new string by finding "words" (any consecutive non-whitespace) and then checking if that word is in a dictionary and replacing it with the corresponding value if found. 这是一种方法,该方法将采用输入字符串并通过查找“单词”(任何连续的非空白),然后检查该单词是否在词典中并将其替换为相应的值(如果找到)来构建新的字符串。 This will fix the issues of Replace doing replacements on "sub-words" (if you have "hello hell" and you want to replace "hell" with "heaven" and you don't want it to give you "heaveno heaven"). 这将解决这个问题的Replace的“子言”做替代品(如果你有“你好地狱”,你要替换“地狱”与“天堂”,你不希望它给你“heaveno天堂”) 。 It also fixes the issue of swapping. 它还解决了交换问题。 For example if you want to replace "yes" with "no" and "no" with "yes" in "yes no" you don't want it to first turn that into "no no" and then into "yes yes". 例如,如果要在“是”中将“是”替换为“否”,将“否”替换为“是”,则不希望它先将其转换为“否”,然后再转换为“是”。

public string ReplaceWords(string input, Dictionary<string, string> replacements)
{
    var builder = new StringBuilder();
    int wordStart = -1;
    int wordLength = 0;
    for(int i = 0; i < input.Length; i++)
    {
        // If the current character is white space check if we have a word to replace
        if(char.IsWhiteSpace(input[i]))
        {
            // If wordStart is not -1 then we have hit the end of a word
            if(wordStart >= 0)
            {
                // get the word and look it up in the dictionary
                // if found use the replacement, if not keep the word.
                var word = input.Substring(wordStart, wordLength);
                if(replacements.TryGetValue(word, out var replace))
                {
                    builder.Append(replace);
                }
                else
                {
                    builder.Append(word);
                }
            }

            // Make sure to reset the start and length
            wordStart = -1;
            wordLength = 0;

            // append whatever whitespace was found.
            builder.Append(input[i]);
        }
        // If this isn't whitespace we set wordStart if it isn't already set
        // and just increment the length.
        else
        {
            if(wordStart == -1) wordStart = i;
            wordLength++;
        }
    }

    // If wordStart is not -1 then we have a trailing word we need to check.
    if(wordStart >= 0)
    {
        var word = input.Substring(wordStart, wordLength);
        if(replacements.TryGetValue(word, out var replace))
        {
            builder.Append(replace);
        }
        else
        {
            builder.Append(word);
        }
    }

    return builder.ToString();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM