English 中文(简体)
如何在文本档案中强调背景案文?
原标题:How do i highlight a background text in a text file?

我有一本书状,储存着文字档案的起始位置和理论价值。

For example: a sample text file (a.txt) may contain text like "how are you? how do you do?"

I have indexed the above text as follows

Dictionary<long,string> charLocation = new Dictionary<long,string>(); 

charLocation[0] = "how" 
charLocation[1] = "ow" 
charLocation[2] = "w" 
charLocation[4] = "are" 
charLocation[6] = "e" 
charLocation[5] = "re" 
charLocation[11] = "?" 
charLocation[9] = "ou?" 
charLocation[10] = "u?" 
charLocation[8] = "you?" 
charLocation[13] = "how" 
charLocation[14] = "ow" 
charLocation[15] = "w" 
charLocation[17] = "do" 
charLocation[18] = "o" 
charLocation[21] = "ou" 
charLocation[22] = "u" 
charLocation[20] = "you" 
charLocation[26] = "?" 
charLocation[24] = "do?" 
charLocation[25] = "o?"

现在,我要强调文本档案中的每一次“how”或“do”。

For this I want to first do a lookup in the dictionary collection and find each occurrence of the string, then open the text file and highlight the text for each occurrence.

How can i do this?

最佳回答

不是测试的,而是应当奏效。

public string HighLight (int startPoint, string text, string word)
{
  if (startPoint > = 0)
  {
    int startIndex = text.indexOf (word, startPoint);
    if (startIndex >= 0)
    {
      StringBuilder builder = new StringBuilder ();
      builder.Append (text.Substring ( 0, startIndex));
      builder.Append ("<strong>");
      builder.Append (text.Substring (startIndex + 1, word.Length));
      builder.Append ("</strong>");
      builder.Append (text.Substring (startIndex + word.Length + 1));
      return HighLight ((startIndex + "<strong>".Length + "</strong>".Length + word.Length, builder.ToString (), word);
    }
  }

  //Word not found.
  return text;
}

因此,你可以:

string myText = "how are you? how do you do?";
string hightLightedText = HighLight (0, myText, "how");

如果我的代码没有错误,那将回报“<strong>how</strong>。 是? <strong>how</strong> do You do?”

然后,你可以重新引用<代码><strong>和</strong>,与你想要的“光”案文相呼应。

问题回答

暂无回答




相关问题
Bring window to foreground after Mutex fails

I was wondering if someone can tell me what would be the best way to bring my application to the foreground if a mutex was not able to be created for a new instance. E.g.: Application X is running ...

How to start WinForm app minimized to tray?

I ve successfully created an app that minimizes to the tray using a NotifyIcon. When the form is manually closed it is successfully hidden from the desktop, taskbar, and alt-tab. The problem occurs ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

Handle DataTable.DataRow cell change event

I have a DataTable that has several DataColumns and DataRow. Now i would like to handle an event when cell of this DataRow is changed. How to do this in c#?

Apparent Memory Leak in DataGridView

How do you force a DataGridView to release its reference to a bound DataSet? We have a rather large dataset being displayed in a DataGridView and noticed that resources were not being freed after the ...

ALT Key Shortcuts Hidden

I am using VS2008 and creating forms. By default, the underscore of the character in a textbox when using an ampersand is not shown when I run the application. ex. "&Goto Here" is not ...

WPF-XAML window in Winforms Application

I have a Winforms application coded in VS C# 2008 and want to insert a WPF window into the window pane of Winforms application. Could you explain me how this is done.

热门标签