Can I use InStr to find newline codes?

  • Thread starter Thread starter Claud Balls
  • Start date Start date
C

Claud Balls

if I read the following into a variable:

010203
020103
030201

could I use something like instr(variable,/n & "02")
to return the position where 02 starts the line?
 
Dim s As String
s.IndexOf(vbCrLf & "02")
or
s.IndexOf(vbCr & "02")

Untested, but that should do it...
Chris
 
Cor Ligthert said:
In addition to Chris, although I never use it, is there as well a
Visual.Basic Function "Left".

How do you use 'Left' as a replacement for 'InStr'?!
 
Claud Balls said:
if I read the following into a variable:

010203
020103
030201

could I use something like instr(variable,/n & "02")
to return the position where 02 starts the line?

Yes.

If you want to read the strings line-by-line, you can use 'StringReader':

\\\
Imports System.IO
..
..
..
Dim s As String = _
"010203" & ControlChars.NewLine & _
"20103" & ControlChars.NewLine & _
"030201"
Dim sr As New StringReader(s)
Dim Line As String = sr.ReadLine
Do While Not Line Is Nothing
MsgBox(Line)
Line = sr.ReadLine()
Loop
///

Alternatively you can split the string:

\\\
Dim s As String = _
"010203" & ControlChars.NewLine & _
"20103" & ControlChars.NewLine & _
"030201"
For Each Line As String In Split(s, ControlChars.NewLine)
MsgBox(Line)
Next Line
///
 
Herfried,
How do you use 'Left' as a replacement for 'InStr'?!
I think thought that you are smart enough to see how when you look at the
question from the OP again, however to make it easy for you. (I used a
string array, I dont know what is the format of the OP, however it goes with
any other type of course as well).

\\\
Dim str As String() = {"010203", "020103", "30201"}
For i As Integer = 0 To str.Length - 1
If Microsoft.VisualBasic.Left(str(i), 2) = "02" Then
MessageBox.Show("line " & i.ToString & " starts with 02")
exit for
End If
Next
///

Nothing wrong, I miss them myself as well sometimes and than I think it is
better to ask than to misunderstand.

Cor
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top