2017-11-29 7 views
0

私は "許容誤差計算機"を作りたいtxtファイルとリネーム番号の文字列を見つけるには

ユーザーは文字列として入力します。たとえば: "D6"今これを.txtファイルで検索し、次の行を読む必要があります。

Dim Findstring = IO.File.ReadAllText("....\Toleranz0.txt") 

がどのように私は、文字列の文字列の後に次の行を見つけることができます。

Picture_txt_with_string_and_tolerances

は、私はこのようなファイルを読みますか?

は多分:

Findstring.contains("D6") 'gives a Boolean 

は、どのように私は、正しいラインを取得していますか?

+3

てみ '代わりにReadAllLines'を使用します。これにより、各行に1つの文字列が配列されます。次に、 'Array.IndexOf'を使って入力の行番号を見つけることができます。 – Blorgbeard

+0

@Bek ...または1行を2回読み込み(ファイルの最後まで)、辞書を作成してください。 – Jimi

+0

次の手順を試してください。 分割してテキストを分割します。これはまた、D6文字以上がある場合に何が起こるかという良い点を持っています。 2.次の行は、 '\ n'と '\ n'の間に含まれる行です。 –

答えて

1

String.Split()を使用してアレイにあなたの文字列を変換して "D6" の後の次のインデックスまたは2つのインデックスを見つける:Blorgbeardにより示唆されるようにここで

Private Sub Funcy() 
    Dim Findstring As String = IO.File.ReadAllText("....\Toleranz0.txt") 
    Dim MyCollection() As String = Findstring.Split() 
    Dim result As String = MyCollection(Array.IndexOf(MyCollection, "D6") + 2) 
    MessageBox.Show(result) 
End Sub 
1

ReadAllLines()を使った例です:

Dim lines() As String = IO.File.ReadAllLines("....\Toleranz0.txt") 
Dim index As Integer = Array.IndexOf(lines, "D6") 
If index <> -1 AndAlso index <= lines.Count - 2 Then 
    Dim targetLine As String = lines(index + 1) 
    ' ... do something with "targetLine" ... 
Else 
    ' either the line was not found, or there was no line after the found line 
End If 
+0

ご協力いただきありがとうございます:-) – Bek

関連する問題