Detecting newline in string ?

  • Thread starter Thread starter Jm
  • Start date Start date
J

Jm

Hi all

Is there a way to search a string and count the amount of newline or crlf's
that are inside it ? Under vb6 i could search for vbcrlf using Instr and a
loop. Is there a similar method under .net ?
 
JM,

The same method we tested it a year ago in this newsgroup and it is for
strings (what vbcrlf is) the best one.

I hope this helps?

Cor
 
quick &dirty

Dim intBegin, intNumber, intvorige As Integer
Dim blnTest As Boolean = True
intBegin = 0
intvorige = 0
Do While blnTest = True
intBegin = txt.Text.IndexOf(vbCrLf, intvorige)
intvorige = intBegin + 1
If intBegin <> -1 Then
intNumber += 1
Else
blnTest = False
End If
Loop
 
Peter,

As we tested it, is Indexof is twice as slow as Istr in this case, when used
with char indexof is more than twice as fast. So when it are really big
files Instr is better. And in fact there is no reason not to use it, for me
it is only that I am used to indexof and therefore forever use that one.

Cor
 
If myString.IndexOf(vbCrLf) >= 0 Then

'vbcrlf String Exists in myString

End If


--
OHM ( Terry Burns ) * Use the following to email me *

Dim ch() As Char = "ufssz/cvsotAhsfbuTpmvujpotXjui/OFU".ToCharArray()
For i As Int32 = 0 To ch.Length - 1
ch(i) = Convert.ToChar(Convert.ToInt16(ch(i)) - 1)
Next
Process.Start("mailto:" & New String(ch))
--
 
Ignore this, I didnt read the post properly

--
OHM ( Terry Burns ) * Use the following to email me *

Dim ch() As Char = "ufssz/cvsotAhsfbuTpmvujpotXjui/OFU".ToCharArray()
For i As Int32 = 0 To ch.Length - 1
ch(i) = Convert.ToChar(Convert.ToInt16(ch(i)) - 1)
Next
Process.Start("mailto:" & New String(ch))
--
 
Hi Cor thnx for the tip,
I also use indexof because I'm used to it but for the new year I'll try to
change and use Instr :-) Dat is toch al 1 goed voornemen ;-)

Peter
 
Peter,

I would not change how you do it, however for people who use Instr there is
in my opinon as well no reason to change.

:-)

I wait still a while until it is almost new year with making new intends

Cor
 
Jm said:
Is there a way to search a string and count the amount of newline or
crlf's
that are inside it ? Under vb6 i could search for vbcrlf using Instr and a
loop. Is there a similar method under .net ?

Your VB6 code should work without a change.

Alternatively you can use regular expressions:

\\\
Imports System.Text.RegularExpressions
..
..
..
Const s As String = _
"Hello" & ControlChars.NewLine & "World" & ControlChars.Cr & _
"Bla" & ControlChars.Lf & "f oo" & ControlChars.NewLine &
ControlChars.Cr
MsgBox(CStr(Regex.Matches(s, "\r\n").Count))
///
 
Back
Top