简体   繁体   English

用粗体字/字符串替换单词/字符串

[英]replace words /strings by bold word/string

Based in this replace multiple words by the same word in bold , I realised that this code has a problem/limitation .. 基于此, 用相同的粗体字替换多个单词 ,我意识到该代码有问题/局限性。

    public string MakeBold(string text, string[] splitwords)
{
    var sb = new StringBuilder();
    var words = text.Split(" ");   
    sb.Append(@"{\rtf1\ansi");
    foreach (var word in words){
      if (splitwords.Contains(word)){
         sb.Append(@"\b"+word+ @"\b0 ");
      }
      else
      {
         sb.Append(word);
         sb.Append(@" ");
      }
    }
    sb.Append(@"}");
    return sb.ToString();
}

With this line var words = text.Split(" "); 在此行中, var words = text.Split(" "); I´m separating all the words in the text, but what if the string I want to replace is : 我将文本中的所有单词分开,但是如果要替换的字符串是:

Text: "I have a text and I need to put some words in bold" 文字:“我有文字,我需要将一些文字加粗”

Words: "have a text"; 词:“有文字”; "bold" “胆大”

Result : "I have a text and I need to put some words in bold " 结果:“我有一个文本 ,需要用粗体显示一些单词”

EDIT :: 编辑::

I updated my code like this 我这样更新了代码

  private string Bold(string text, string[] splitwords)
   {

       StringBuilder builder = new StringBuilder();

       builder.Append(@"{\rtf1\ansi");

       foreach (string word in splitwords)
       {
               if (Regex.IsMatch(text, @"(?<![\w])" + word + @"(?![\w])" ))
           {
               text= text.Replace(word, @"\b " + word + " " + @"\b0");
               }
       }

       builder.Append(text);
       builder.Append(@"}");

       return builder.ToString();
   }

But if the if I have the text 但是如果我有文字

Text: "I have a text and I need to put some words in bold" 文字:“我有文字,我需要将一些文字加粗”

Words: "have"; 词:“有”; "have a text"; “有文字”; "bold" “胆大”

Result : "I have a text and I need to put some words in bold " 结果:“我一个文本,需要用粗体显示一些单词”

But in another cases it works fine HELP.... 但是在其他情况下,它可以很好地帮助...。

Try this : 尝试这个 :

    public string MakeBold(string text, string[] splitwords)
    {
        string returnValue = text;
        foreach (var word in splitwords)
        {
            returnValue = returnValue.Replace(word, @"\b" + word + @"\b0");
        }
        var finalString = new StringBuilder();
        finalString.Append(@"{\rtf1\ansi");
        finalString.Append(returnValue);
        finalString.Append(@"}");
        return finalString.ToString();
    }

We can check for all occurrences of each phrase: 我们可以检查每个短语的所有出现情况:

StringBuilder builder = new StringBuilder();
string[] words = text.Split(" ");
foreach (string phrase in splitwords)
{
    string[] phrasewords = phrase.Split(" ");
    for (int i = 0; i < words.Length - phrasewords.Length; i++)
    {
        bool match = true;
        for (int j = 0; j < phrasewords.Length; j++)
            if (words[i] != phrasewords[j])
            {
                match = false;
                break;
            }

        if (match)
        {
            builder.Append(@"\b" + phrase + @"\b0 ");
            i += phrasewords.Length - 1;
        }
        else
            builder.Append(words[i] + " ");
    }
}

This won't work for overlapping phrases, eg "one two" and "two three". 这不适用于重叠的短语,例如“一二”和“二三”。

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

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