By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,963 Members | 1,279 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,963 IT Pros & Developers. It's quick & easy.

Detecting newline in string ?

P: n/a
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 ?
Nov 21 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a
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

"Jm" <ja*****@ihug.com.au>

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 ?

Nov 21 '05 #2

P: n/a
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
"Cor Ligthert" <no************@planet.nl> wrote in message
news:uc**************@TK2MSFTNGP15.phx.gbl...
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

"Jm" <ja*****@ihug.com.au>

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 ?


Nov 21 '05 #3

P: n/a
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

"Peter Proost" <pp*****@nospam.hotmail.com>
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
"Cor Ligthert" <no************@planet.nl> wrote in message
news:uc**************@TK2MSFTNGP15.phx.gbl...
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

"Jm" <ja*****@ihug.com.au>
>
> 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 ?
>
>



Nov 21 '05 #4

P: n/a
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))
--
"Peter Proost" <pp*****@nospam.hotmail.com> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
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
"Cor Ligthert" <no************@planet.nl> wrote in message
news:uc**************@TK2MSFTNGP15.phx.gbl...
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

"Jm" <ja*****@ihug.com.au>
>
> 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 ?
>
>



Nov 21 '05 #5

P: n/a
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))
--
"OHM ( Terry Burns )" <me@mine.com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
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))
--
"Peter Proost" <pp*****@nospam.hotmail.com> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
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
"Cor Ligthert" <no************@planet.nl> wrote in message
news:uc**************@TK2MSFTNGP15.phx.gbl...
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

"Jm" <ja*****@ihug.com.au>

>
> 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 ?
>
>



Nov 21 '05 #6

P: n/a
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

"Cor Ligthert" <no************@planet.nl> wrote in message
news:eJ**************@TK2MSFTNGP12.phx.gbl...
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

"Peter Proost" <pp*****@nospam.hotmail.com>
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
"Cor Ligthert" <no************@planet.nl> wrote in message
news:uc**************@TK2MSFTNGP15.phx.gbl...
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

"Jm" <ja*****@ihug.com.au>

>
> 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 ?
>
>



Nov 21 '05 #7

P: n/a
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

"Peter Proost" <pp*****@nospam.hotmail.com>
...
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

"Cor Ligthert" <no************@planet.nl> wrote in message
news:eJ**************@TK2MSFTNGP12.phx.gbl...
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

"Peter Proost" <pp*****@nospam.hotmail.com>
> 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
>
>
> "Cor Ligthert" <no************@planet.nl> wrote in message
> news:uc**************@TK2MSFTNGP15.phx.gbl...
>> 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
>>
>> "Jm" <ja*****@ihug.com.au>
>>
>> >
>> > 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 ?
>> >
>> >
>>
>>
>
>



Nov 21 '05 #8

P: n/a
"Jm" <ja*****@ihug.com.au> schrieb:
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))
///

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

Nov 21 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.