文字列を取り、その中の一致の周りに強調表示コードを追加する強調表示アルゴリズムがあります。私が抱えている問題は、検索する文字列として「Find tæst」、検索する文字列として「taest」などの単語を使用することです。検索文字列の長さが一致の長さと一致しないため、一致の終わりを正確に見つけることができません。私の場合、IndexOf は一致を示していますが、結合された æ は 1 文字としてカウントされるため、一致の終了を検出できません。ここで IndexOf が機能するとは思わない。一致のインデックスと一致の長さを返すものは機能します。しかし、私は他に何を使うべきかわかりません。
' cycle through search words and replace them in the text
For intWord = LBound(m_arrSearchWords) To UBound(m_arrSearchWords)
If m_arrSearchWords(intWord).Length > 0 Then
' replace instances of the word with the word surrounded by bold codes
' find starting position
intPos = strText.IndexOf(m_arrSearchWords(intWord), System.StringComparison.CurrentCultureIgnoreCase)
Do While intPos <> -1
strText = strText.Substring(0, (intPos - 1) - 0 + 1) & cstrHighlightCodeOn & strText.Substring(intPos, m_arrSearchWords(intWord).Length) & cstrHighlightCodeOff & strText.Substring(intPos + m_arrSearchWords(intWord).Length)
intPos = strText.IndexOf(m_arrSearchWords(intWord), intPos + m_arrSearchWords(intWord).Length + cstrHighlightCodeOn.Length + cstrHighlightCodeOff.Length, System.StringComparison.CurrentCultureIgnoreCase)
Loop
End If
Next intWord
長さが文字列の末尾を超えているため、Substring メソッドは失敗しています。検索語で終わる文字列を修正しました (上には表示されていません)。しかし、長い文字列は正しく強調表示されないため、修正する必要があります。