Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bold selective text from string based on start/end position

In my ASP.NET page, I have a string (returned from SQL db). I would like to bold certain part of the string text based on given text position.

For example, if I have a string as follows:

"This is an example to show where to bold the text"

And I am given character start position: 6 and end position: 7, then I would bold the word "is" in my string, getting:

"This is an example to show where to bold the text"

Any ideas?

EDIT: Keep in mind I need to use the start/end position as there may be duplicate words in the string.

like image 435
viv_acious Avatar asked Oct 11 '25 20:10

viv_acious


1 Answers

  1. Insert a close tag into position 7 of the string
  2. Insert an open tag into position 5 (6 - 1) of the string.
  3. You will get a string like "This is an example…"

I.e. modify string (insert markup) from end to start:

var result = str.Insert(7, "</b>").Insert(6 - 1, "<b>");
like image 111
Viacheslav Ivanov Avatar answered Oct 14 '25 11:10

Viacheslav Ivanov