0

文字列を取り、その中の一致の周りに強調表示コードを追加する強調表示アルゴリズムがあります。私が抱えている問題は、検索する文字列として「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 メソッドは失敗しています。検索語で終わる文字列を修正しました (上には表示されていません)。しかし、長い文字列は正しく強調表示されないため、修正する必要があります。

4

2 に答える 2

-1

s1私の理解が正しければ、「一致した文字列」を返す関数を探しています。つまり、 insideを探している場合、 のどの部分が一致したs2か (一致した最初と最後の文字のインデックス) を正確に知りたいと考えています。s2)。これにより、一致を強調表示でき、文字列 (大文字/小文字、合字など) は変更されません。

私は VB.net を持っていません。残念ながら、VBA には VB.net とまったく同じ検索機能がありません。したがって、次のコードは一致の開始と終了を正しく識別していることを理解してください。ただし、upper/ でのみテストされています。小文字の一致。これが問題の解決に役立つことを願っています。

Option Compare Text
Option Explicit

Function startEndIndex(bigString, smallString)
' function that returns start, end index
' of the match
' it keeps shortening the bigString until no match is found
' this is how it takes care of mismatches in number of characters
' because of a match between "similar" strings
Dim i1, i2
Dim shorterString

i2 = 0

' first see if there is a match at all:
i1 = InStr(1, bigString, smallString, vbTextCompare)

If i1 > 0 Then
  ' largest value that i2 can have is end of string:
  i2 = Len(bigString)

  ' can make it shorter - but no shorter than twice the length of the search string
  If i2 > i1 + 2 * Len(smallString) Then i2 = i1 + 2 * Len(smallString)
  shorterString = Mid(bigString, i1, i2 - i1)

  ' keep making the string shorter until there is no match:
  While InStr(1, shorterString, smallString, vbTextCompare) > 0
    i2 = i2 - 1
    shorterString = Mid(bigString, i1, i2 - i1)
  Wend

End If

' return the values as an array:
startEndIndex = Array(i1, endOfString)

End Function


Sub test()
' a simple test routine to see that things work:
Dim a
Dim longString: longString = "This is a very long TaesT of a complicated string"
a = startEndIndex(longString, "very long taest")
If a(0) = 0 And a(1) = 0 Then
MsgBox "no match found"
Else
Dim highlightString As String
highlightString = Left(longString, a(0) - 1) & "*" & Mid(longString, a(0), a(1) - a(0) + 1) & _
  "*" & Mid(longString, a(1) + 1)
  MsgBox "start at " & a(0) & " and end at " & a(1) & vbCrLf & _
  "string matched is '" & Mid(longString, a(0), a(1) - a(0) + 1) & "'" & vbCrLf & _
  "with highlighting: " & highlightString
End If
End Sub
于 2013-11-11T23:23:29.093 に答える