简体   繁体   中英

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.
What I need to do is all the words that are in the wordList appear in the text in bold.
Example :
Text: "I have a text and I need to put some words in bold"

Words in list : "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)

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?

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

{\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?

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 .

(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."

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.

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.

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.

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 !

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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