1

文字列が見つかるまでファイルの先頭でファイルを検索するコードを書いています。

動作するコードは次のとおりです。

    Private Function ReadTextFileUpto(ByVal txtFile As String, ByVal searchString As String) As String
        Dim result As String = String.Empty
        Dim sb As New System.Text.StringBuilder()
        Dim previousLength As Integer = 0
        Using reader As New IO.StreamReader(txtFile)
            'Read the file 1 char at a time and append it to sb
            While reader.Peek >= 0
                sb.Append(Convert.ToChar(reader.Read))
                'store the current length of sb
                previousLength = sb.Length
                'attemp to replace the search string with an empty string. If the length changed after the replacement
                'then the following conditions must be true:
                ' 1. we have found the search string in sb.
                ' 2. the search string was at the end of sb.
                sb.Replace(searchString, String.Empty)
                If sb.Length < previousLength Then
                    'since we have found the search string,  exit the loop and return the string currently in sb.
                    result = sb.ToString()
                    Exit While
                End If
            End While
        End Using
        Return result
    End Function

ファイル内の開始位置を選択して検索を開始できるように、このコードを編集したいと思います。

これが私が取り組んでいるコードです:

    Public Function ReadTextFileUpto(ByVal txtFile As String, ByVal searchString As String, integerStartPosition As Integer) As String
    Dim result As String = String.Empty
    Dim sb As New System.Text.StringBuilder()
    Dim previousLength As Integer = 0
    Using reader As New IO.StreamReader(txtFile)
        'Read the file 1 char at a time and append it to sb
        While reader.Peek >= integerStartPosition
            sb.Append(Convert.ToChar(reader.Read))
            'store the current length of sb
            previousLength = sb.Length
            'attemp to replace the search string with an empty string. If the length changed after the replacement
            'then the following conditions must be true:
            ' 1. we have found the search string in sb.
            ' 2. the search string was at the end of sb.
            sb.Replace(searchString, String.Empty)
            If sb.Length < previousLength Then
                'since we have found the search string,  exit the loop and return the string currently in sb.
                result = sb.ToString()
                Exit While
            End If
        End While
    End Using
    Return result
End Function

これを機能させるために助けてもらえますか?

最終コード

Public Function ReadTextFileUpto(ByVal txtFile As String, ByVal searchString As String, integerStartPosition As Integer) As String
    Dim result As String = String.Empty
    Dim sb As New System.Text.StringBuilder()
    Dim tmpString As String = ""
    Dim previousLength As Integer = -1
    Dim integerCurrentPosition As Integer 
    Using reader As New IO.StreamReader(txtFile)
        While reader.Peek >= 0

            integerCurrentPosition += 1

            tmpString = Convert.ToChar(reader.Read)

            If integerCurrentPosition > integerStartPosition Then
                sb.Append(tmpString)
                previousLength = sb.Length
                sb.Replace(searchString, String.Empty)
                If sb.Length < previousLength Then
                    result = sb.ToString()
                    Exit While
                End If
            End If

        End While
    End Using
    Return result
End Function
4

1 に答える 1

0

whileループ内に位置チェックを配置する必要があります。これを行う簡単な例を次に示します。

Private Function ReadTextFileUpto(ByVal txtFile As String, ByVal searchString As String) As String
            Dim result As String = String.Empty
            Dim sb As New System.Text.StringBuilder()
            Dim previousLength As Integer = 0
            Dim currentPosition as Integer = 0

            Using reader As New IO.StreamReader(txtFile)
                'Read the file 1 char at a time and append it to sb
                While reader.Peek >= 0
                    If currentPosition < integerStartPosition Then
                       Continue While
                    End if 

                    sb.Append(Convert.ToChar(reader.Read))
                    'store the current length of sb
                    previousLength = sb.Length
                    'attemp to replace the search string with an empty string. If the length changed after the replacement
                    'then the following conditions must be true:
                    ' 1. we have found the search string in sb.
                    ' 2. the search string was at the end of sb.
                    sb.Replace(searchString, String.Empty)
                    If sb.Length < previousLength Then
                        'since we have found the search string,  exit the loop and return the string currently in sb.
                        result = sb.ToString()
                        Exit While
                    End If

                    currentPosition = currentPosition + 1
                End While
            End Using
            Return result
        End Function

アップデート:

または、StreamReader.Readメソッド(Char []、Int32、Int32)メソッドを試して、開始位置を指定してストリームを配列に読み込むことができます。

インデックスから始めて、現在のストリームから最大カウント文字をバッファに読み込みます。

詳細については、チュートリアルを確認してください

StreamReader

幸運を!

于 2012-09-06T06:01:17.410 に答える