【问题标题】:Insert space before and after specific keywords if they are found in string using C#如果使用 C# 在字符串中找到特定关键字,则在特定关键字之前和之后插入空格
【发布时间】:2019-03-02 23:47:00
【问题描述】:

我有一个哑字符串

Today the auto industry is booming. Automated machines are big part of it and it's working great. They are doing very big autonomous work.

我希望能够提供一个关键字字典作为List<string>

然后我想在我的文本中搜索这些关键字。如果找到关键字,请检查前后是否包含空格。如果是,什么也不做。如果不包含,则在之前或之后添加空格。所以关键字可以是一个单词的开头中间或结尾的一部分。所以基本上在文本中使关键字成为单个单词。

示例关键字:auto, work, the

为了我的文字

Today the auto industry is booming. Automated machines are big part of it and it's working great. They are doing very big autonomous work.

结果应该是:

Today the auto industry is booming. Auto mated machines are big part of it and it's work ing great. The y are doing very big auto nomous work .

【问题讨论】:

  • 为什么不用正则表达式?这感觉是正则表达式的最佳时机......
  • 我不太懂正则表达式……但当然没关系。
  • 你可以搜索,我认为这是一个经典的正则表达式。见:stackoverflow.com/questions/5650909/…
  • 因为它应该是一个单词
  • 对于每个单词,将单词替换为" word "。然后将" word"(前面两个空格)替换为" word",将"word "(末尾两个空格)替换为"word ",以处理双空格问题(即那里已经有空格)。

标签: c# split


【解决方案1】:

这是一种方法。它通过关键字逐个关键字进行比较,忽略大小写。如果找到关键字,则检查前后是否有空格并添加。

public static string AddSpacesAroundWords(string text, List<string> words)
{
    foreach(string word in words) {
        int index = 0;
        while(index < text.Length) {
            index = text.IndexOf(word, index, StringComparison.CurrentCultureIgnoreCase);
            if(index == -1) {
                // no occurrence of this word anymore
                break;
            }
            // check if there is a space at the beginning of the word
            if(index > 0 && text[index - 1] != ' ') {
                text = text.Insert(index++ - 1, " ");
            }
            // check if there is a space at the end of the word
            if(index + word.Length < text.Length && text[index + word.Length] != ' ') {
                text = text.Insert(index++ + word.Length, " ");
            }
            index += word.Length;
        }
    }
    return text;
}

用法:

string original = "Today the auto industry is booming. Automated machines are big part of it and it's working great. They are doing very big autonomous work.";
var words = new List<string> { "auto", "work", "the" };
string result = AddSpacesAroundWords(original, words);
// result is now "Today the auto industry is booming. Auto mated machines are big part of it and it's work ing great. The y are doing very big auto nomous work ."

【讨论】:

    【解决方案2】:

    这只是 GregaMohorko 答案的修改/变体,我赞成:

    private void Form1_Load(object sender, EventArgs e)
    {
        tbKeywords.Text = "auto, work, the";
        textBox1.Text = "Today the auto industry is booming. Automated machines are big part of it and it's working great. They are doing very big autonomous work.";
    }
    
    private void button1_Click(object sender, EventArgs e)
    {
        textBox2.Text = AddSpaceAroundWords(textBox1.Text, tbKeywords.Text);        
    }
    
    private string AddSpaceAroundWords(string sentence, string CommaSeparatedKeyWords)
    {
        int index;
        string keyword;
        foreach (string key in CommaSeparatedKeyWords.Split(",".ToCharArray(), StringSplitOptions.RemoveEmptyEntries))
        {
            index = 0;
            keyword = key.Trim();       
            while ((index = sentence.IndexOf(keyword, index, StringComparison.InvariantCultureIgnoreCase)) != -1)
            {
                if ((index > 0) && (sentence[index - 1] != ' '))
                {
                    sentence = sentence.Insert(index++, " ");
                }
                if (((index + keyword.Length) < sentence.Length) && (sentence[index + keyword.Length] != ' ')) {
                    sentence = sentence.Insert(index++ + keyword.Length, " ");
                }
                index += keyword.Length;
            }
        }
        return sentence;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-29
      • 2019-10-13
      • 2021-12-20
      • 1970-01-01
      相关资源
      最近更新 更多