简体   繁体   English

用粗体字替换多个字

[英]replace multiple words by the same word in bold

I have a text in a RichTextBox, and a list of words(wordList) - I´m using winforms. 我在RichTextBox中有一个文本,并且有一个单词列表(wordList)-我在使用winforms。
What I need to do is all the words that are in the wordList appear in the text in bold. 我需要做的是wordList中的所有单词都以粗体显示在文本中。
Example : 范例:
Text: "I have a text and I need to put some words in bold" 文字:“我有文字,我需要将一些文字加粗”

Words in list : "need"; "some"; "bold" 列表中的单词: "need"; "some"; "bold" "need"; "some"; "bold"

Result : "I have a text and I need to put some words in bold " 结果:“我有一个文本, 需要粗体显示 一些单词”
I tried this ( I saw a similar post here in stackoverflow) 我尝试了这个(我在stackoverflow中看到了类似的帖子)

text= "" + text+ "";

foreach (var word in wordList)
{
    string w = string.Format(" {0} ", word);
    if (text.Contains(w))
    {
        while (text.Contains(w))
        {
            text= text.Replace(w, "<b>"+w+"</b>");
        }
    }
}

text = text.Trim();

How can I say that the word w need to be in bold? 我怎么能说w一词要用粗体显示?

EDIT: 编辑:
I tried this solution 我尝试了这个解决方案

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

and the result is in the RichTextBox 结果在RichTextBox中

{\rtf1\ansiI have a text and I \bneed\b0to put \bsome\b0words in \bbold\b0}    

Any ideas why?? 任何想法为什么?

Am I right that this is happening in a postback/ajax call? 我是否正确,这是在回发/ ajax调用中发生的?

Are you needing the string back in its original form after this? 之后,您是否需要以原始形式返回字符串? as you will need to strip back out all the bold tags. 因为您将需要删除所有粗体标签。

however, for display purposes you could, split the string on a space, which would give you an array. 但是,出于显示目的,您可以将字符串分割在一个空格上,这将为您提供一个数组。 Then just replace all matched array items with + arrayItem + . 然后只需将所有匹配的数组项替换为+ arrayItem +即可

(You may be better putting them in a span, with a class actually. This would give you more flexibility - could change the colour, font size, bold, etc, etc.) (您最好将它们放在跨度的范围内,并与实际的类放在一起。这样可以给您更大的灵活性-可以更改颜色,字体大小,粗体等)

Also, are you wanting to bold the word if it appears in another word? 另外,如果要在另一个单词中出现该单词,是否要使其加粗? Eg if you are looking for "sell", and inputString is "selling my house", you would end up with: "selling my house." 例如,如果您正在寻找“出售”,而inputString在“出售我的房子”,那么您最终将得到:“出售我的房子”。

For simplicity: 为简单起见:

var inputString = "your input string!"
var outputString = "";
var wordsToFindArray = [string array of the words you are looking to make bold.]
foreach (wordToFind in wordsToFind)
{
    //pick one of these lines, not both, as the second will overwrite the first

    outputstring = inputstring.replace(wordToFind, "<span class=\"found-word\">" + wordToFind + "</span>"); //add span class
    outputstring = inputstring.replace(wordToFind, "<b>" + wordToFind + "</b>"); //boldify
}

return outputString;

However, this would also boldify / add the span class to words found inside words. 但是,这也会加粗/将span类添加到单词中找到的单词。

Alternatively, as mentioned above, you could do a split on the input string, on " " or ".", etc, then, run the foreach on each input string array item. 或者,如上所述,您可以对输入字符串,“”或“。”等进行拆分,然后在每个输入字符串数组项上运行foreach。

Ps. PS。 this is case insensitive. 这是不区分大小写的。

Hope that helps. 希望能有所帮助。

It seems you are confusing web with a rich text box. 似乎您将网络与富文本框混淆了。 The sample code only splits on spaces, and does not do anything with Capitals. 该示例代码仅在空格上分割,并且对Capitals无效。

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

Thanks to all, but I found the answer. 谢谢大家,但我找到了答案。
I was using RichTextBox but when I was displaying the result there I forgot to use .Rtf instead of .Text .... (stupid me!) everything is working fine ! 我使用的是RichTextBox,但是当我在其中显示结果时,却忘记了使用.Rtf而不是.Text ....(愚蠢的我!),一切正常!

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

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