如果在使用C#的字符串中找到特定关键字之前和之后插入空格

问题描述 投票:-1回答:2

我有一个愚蠢的字符串

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 .

c# split
2个回答
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 ."

1
投票

这只是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;
}
© www.soinside.com 2019 - 2024. All rights reserved.