473,395 Members | 1,885 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Detecting newline in string ?

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
8 11377
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
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
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
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
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
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
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
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Kevin Frey | last post by:
Hello, Can anyone suggest a way, that does not involve writing a temporary file, that would permit me to discover whether writing: output_file_stream << '\n'; will write either a LF (eg....
5
by: Vamsi | last post by:
Hi, I am trying a basic opearation of splitting a multiline value to an array of single lines(Actually making Address into AddressLine1, AddressLine2). I used Environment.NewLine in split, I...
5
by: Kaka | last post by:
Hi, I am a learner for C# and .Net. I want to know how to put a line break to a string so that when it show in the textbox the text will be displayed in saperated lines. e.g.: String s; s...
5
by: Claud Balls | last post by:
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?
18
by: Fuzzyman | last post by:
Hello all, I'm trying to detect line endings used in text files. I *might* be decoding the files into unicode first (which may be encoded using multi-byte encodings) - which is why I'm not...
4
by: Peter Kirk | last post by:
Hi I would like to ask a little bit about the value Environment.Newline: what is it and what is the point of it? Ok, I can see in the docs that it represents "newline" for the current platform -...
16
by: junky_fellow | last post by:
Is there any efficcient way of removing the newline character from the buffer read by fgets() ? Is there any library function that is similar to fgets() but also tells how many bytes it read...
11
by: rossum | last post by:
I want to declare a const multi-line string inside a method, and I am having some problems using Environment.NewLine. I started out with: class foo { public void PrintStuff() { const...
5
by: alex21 | last post by:
I am trying to write a function for determining the data type of columns in a delimited file. However my function is not detecting a newline and exiting the loop after the end of the first line. ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.