.NETGURU
Is there a faster string manipulation?
Messages   Related Types
This message was discovered on ASPFriends.com 'aspngspeed' list.
Responses highlighted in red are from those people who are likely to be able to contribute good, authoratitive information to this discussion. They include Microsoft employees, MVP's and others who IMHO contribute well to these kinds of discussions.

Chan, Jasmine
Hi all,
I have a function that takes in a string input and return how
many lines this string will take up. This function can be called many
times throughout my code. Is there a better way of doing this then the
following steps:

1.    split it into an array base upon the space
2.    start concatenating the string together
3.    checked if the concatenated string is greater then my line limit, if
so increment the count and start the next string again.

Here is the pseudo-code:

arr = split(input, " ")
For i=0 to arr.length - 1
tmp = tmp & arr(i)
if len(tmp) > limit then
count= count + 1
tmp = ""
end if
Next
Return count

Thanks in advance for your help.

Jasmine
Reply to this message...
 
    
Dan Wahlin
Since it's possible that you may do multiple string concats depending
upon the array size, using the StringBuilder.Append() method (instead of
string concatenation) should be more efficient especially when larger
strings are involved.

Dan

---------------------------------------------------------------------
Get focused ASP.NET/XML articles and code at
<http://www.XMLforASP.NET> http://www.XMLforASP.NET

<http://www.amazon.com/exec/obidos/ASIN/0672320398/ref=ase_xmlforaspnetd
-20/103-9498093-2544665> XML for ASP.NET Developers
by Dan Wahlin
In bookstores everywhere!
---------------------------------------------------------------------

-----Original Message-----
From: Chan, Jasmine [mailto:Click here to reveal e-mail address]
Sent: Thursday, January 10, 2002 3:14 PM
To: aspngspeed
Subject: [aspngspeed] Is there a faster string manipulation?

Hi all,
I have a function that takes in a string input and return
how many lines this string will take up. This function can be called
many times throughout my code. Is there a better way of doing this then
the following steps:

1. split it into an array base upon the space
2. start concatenating the string together
3. checked if the concatenated string is greater then my line
limit, if so increment the count and start the next string again.

Here is the pseudo-code:

arr = split(input, " ")
For i=0 to arr.length - 1
tmp = tmp & arr(i)
if len(tmp) > limit then
count= count + 1
tmp = ""
end if
Next
Return count

Thanks in advance for your help.

Jasmine

| [aspngspeed] member Click here to reveal e-mail address = YOUR ID
| http://www.asplists.com/asplists/aspngspeed.asp = JOIN/QUIT
| http://www.asplists.com/search = SEARCH Archives
Reply to this message...
 
    
Sterling Bates (VIP)
Here's an alternative that requires no splitting or re-joining:

lines = 1; ' Assume one line of text
i = 1; ' if VB is 1-based instead of 0-based
while (i < length(input))
i = i + limit;
if (i < length(input)) then ' Only go back if we aren't at
the end
while (mid(input,i,1) <> " ") ' Back up to the nearest space
i = i - 1
wend
end if
lines = lines + 1
wend
if (length(input) = 0) then
lines = 0
end if

You may have to tweak it a little but it should work for the most part.
-----Original Message-----
From: Chan, Jasmine [mailto:Click here to reveal e-mail address]
Sent: 10-Jan-02 3:14 PM
To: aspngspeed
Subject: [aspngspeed] Is there a faster string manipulation?

Hi all,
I have a function that takes in a string input and return how
many lines this string will take up. This function can be called many
times throughout my code. Is there a better way of doing this then the
following steps:

1.    split it into an array base upon the space

2.    start concatenating the string together

3.    checked if the concatenated string is greater then my line limit, if
so increment the count and start the next string again.

Here is the pseudo-code:

arr = split(input, " ")
For i=0 to arr.length - 1
tmp = tmp & arr(i)
if len(tmp) > limit then
count= count + 1
tmp = ""
end if
Next
Return count

Thanks in advance for your help.

Jasmine

| [aspngspeed] member Click here to reveal e-mail address = YOUR ID
| http://www.asplists.com/asplists/aspngspeed.asp = JOIN/QUIT
| http://www.asplists.com/search = SEARCH Archives
Reply to this message...
 
    
Mike Amundsen
As a quick test comparision, i concatenated 10,000 int values into a single
long string. This involved the concatenation and the conversion from int to
string.

using 'standard' concatenation techniques [ x += i.ToString() ], the job
took about 5 seconds on my laptop.

using the StringBuilder.Append method took less than 1 second.

of course, your mileage may vary<grin>

MCA
Mike Amundsen
Click here to reveal e-mail address
host your .NET apps @ www.EraServer.NET
/join #EraServer.NET at IRC.EraServer.NET

-----Original Message-----
From: Dan Wahlin [mailto:Click here to reveal e-mail address]
Sent: Thursday, January 10, 2002 5:40 PM
To: aspngspeed
Subject: [aspngspeed] RE: Is there a faster string manipulation?

Since it's possible that you may do multiple string concats depending upon
the array size, using the StringBuilder.Append() method (instead of string
concatenation) should be more efficient especially when larger strings are
involved.

Dan

---------------------------------------------------------------------

Get focused ASP.NET/XML articles and code at
http://www.XMLforASP.NET

XML for ASP.NET Developers
by Dan Wahlin
In bookstores everywhere!

---------------------------------------------------------------------

-----Original Message-----
From: Chan, Jasmine [mailto:Click here to reveal e-mail address]
Sent: Thursday, January 10, 2002 3:14 PM
To: aspngspeed
Subject: [aspngspeed] Is there a faster string manipulation?

Hi all,

I have a function that takes in a string input and return how
many lines this string will take up. This function can be called many
times throughout my code. Is there a better way of doing this then the
following steps:

1. split it into an array base upon the space

2. start concatenating the string together

3. checked if the concatenated string is greater then my line limit,
if so increment the count and start the next string again.

Here is the pseudo-code:

arr = split(input, " ")

For i=0 to arr.length - 1

tmp = tmp & arr(i)

if len(tmp) > limit then

count= count + 1

tmp = ""

end if

Next

Return count

Thanks in advance for your help.

Jasmine

| [aspngspeed] member Click here to reveal e-mail address = YOUR ID
| http://www.asplists.com/asplists/aspngspeed.asp = JOIN/QUIT
| http://www.asplists.com/search = SEARCH Archives

| [aspngspeed] member Click here to reveal e-mail address = YOUR ID
| http://www.asplists.com/asplists/aspngspeed.asp = JOIN/QUIT
| http://www.asplists.com/search = SEARCH Archives
Reply to this message...
 
    
Ryan Trudelle-Schwarz (VIP)
Or this seems to work as well (not positive it will, haven't thoroughly
tested yet):

string strTemp = "your string here";

Regex x = new Regex(@"(?>(?:.|\n){1,12}\s)");

Return x.Matches(strTemp).Count;

Let me know if it does work for your lengthy strings, I'm very curious
:)

Hth, Ryan

-----Original Message-----
From: Sterling Bates [mailto:Click here to reveal e-mail address]

Here's an alternative that requires no splitting or re-joining:

lines = 1; ' Assume one line of text

i = 1; ' if VB is 1-based instead of 0-based

while (i < length(input))

i = i + limit;

if (i < length(input)) then ' Only go back if we aren't
at the end

while (mid(input,i,1) <> " ") ' Back up to the nearest space

i = i - 1

wend

end if

lines = lines + 1

wend

if (length(input) = 0) then

lines = 0

end if

You may have to tweak it a little but it should work for the most part.

-----Original Message-----
From: Chan, Jasmine [mailto:Click here to reveal e-mail address]

Hi all,

I have a function that takes in a string input and return
how many lines this string will take up. This function can be called
many times throughout my code. Is there a better way of doing this then
the following steps:

1. split it into an array base upon the space

2. start concatenating the string together

3. checked if the concatenated string is greater then my line
limit, if so increment the count and start the next string again.

Here is the pseudo-code:

arr = split(input, " ")

For i=0 to arr.length - 1

tmp = tmp & arr(i)

if len(tmp) > limit then

count= count + 1

tmp = ""

end if

Next

Return count

Thanks in advance for your help.

Jasmine
Reply to this message...
 
    
Ryan Trudelle-Schwarz (VIP)
Shoulda mentioned also that the 12 in there is the limit so just swap in
whatever you want.

-----Original Message-----
From: Ryan Trudelle-Schwarz [mailto:Click here to reveal e-mail address]

Or this seems to work as well (not positive it will, haven't thoroughly
tested yet):

string strTemp = "your string here";

Regex x = new Regex(@"(?>(?:.|\n){1,12}\s)");

Return x.Matches(strTemp).Count;

Let me know if it does work for your lengthy strings, I'm very curious
:)

Hth, Ryan

-----Original Message-----
From: Sterling Bates [mailto:Click here to reveal e-mail address]

Here's an alternative that requires no splitting or re-joining:

lines = 1; ' Assume one line of text

i = 1; ' if VB is 1-based instead of 0-based

while (i < length(input))

i = i + limit;

if (i < length(input)) then ' Only go back if we aren't
at the end

while (mid(input,i,1) <> " ") ' Back up to the nearest space

i = i - 1

wend

end if

lines = lines + 1

wend

if (length(input) = 0) then

lines = 0

end if

You may have to tweak it a little but it should work for the most part.

-----Original Message-----
From: Chan, Jasmine [mailto:Click here to reveal e-mail address]

Hi all,

I have a function that takes in a string input and return
how many lines this string will take up. This function can be called
many times throughout my code. Is there a better way of doing this then
the following steps:

1. split it into an array base upon the space

2. start concatenating the string together

3. checked if the concatenated string is greater then my line
limit, if so increment the count and start the next string again.

Here is the pseudo-code:

arr = split(input, " ")

For i=0 to arr.length - 1

tmp = tmp & arr(i)

if len(tmp) > limit then

count= count + 1

tmp = ""

end if

Next

Return count

Thanks in advance for your help.

Jasmine
Reply to this message...
 
    
Chan, Jasmine
I tried your code and it works. Here is the code in VB:

Dim x As New Regex("(?>(?:.|\n){1,80}\s)")
intLineCnt = x.Matches(input).Count()

This is much cleaner then splitting the array and then do the concatenation
and then the check. Can you explain what the regular expression is doing?
I tried reading it but it looks like Greek to me. Thanks!

Jasmine

-----Original Message-----
From: Ryan Trudelle-Schwarz [mailto:Click here to reveal e-mail address]
Sent: Thursday, January 10, 2002 6:13 PM
To: aspngspeed
Subject: [aspngspeed] RE: Is there a faster string manipulation?

Shoulda mentioned also that the 12 in there is the limit so just swap in
whatever you want.

-----Original Message-----
From: Ryan Trudelle-Schwarz [mailto:Click here to reveal e-mail address]

Or this seems to work as well (not positive it will, haven't thoroughly
tested yet):

string strTemp = "your string here";
Regex x = new Regex(@"(?>(?:.|\n){1,12}\s)");
Return x.Matches(strTemp).Count;

Let me know if it does work for your lengthy strings, I'm very curious :)

Hth, Ryan

-----Original Message-----
From: Sterling Bates [mailto:Click here to reveal e-mail address]

Here's an alternative that requires no splitting or re-joining:

lines = 1; ' Assume one line of text
i = 1; ' if VB is 1-based instead of 0-based
while (i < length(input))
i = i + limit;
if (i < length(input)) then ' Only go back if we aren't at
the end
while (mid(input,i,1) <> " ") ' Back up to the nearest space
i = i - 1
wend
end if
lines = lines + 1
wend
if (length(input) = 0) then
lines = 0
end if

You may have to tweak it a little but it should work for the most part.
-----Original Message-----
From: Chan, Jasmine [mailto:Click here to reveal e-mail address]
Hi all,
I have a function that takes in a string input and return how
many lines this string will take up. This function can be called many
times throughout my code. Is there a better way of doing this then the
following steps:

1. split it into an array base upon the space
2. start concatenating the string together
3. checked if the concatenated string is greater then my line limit, if
so increment the count and start the next string again.

Here is the pseudo-code:

arr = split(input, " ")
For i=0 to arr.length - 1
tmp = tmp & arr(i)
if len(tmp) > limit then
count= count + 1
tmp = ""
end if
Next
Return count

Thanks in advance for your help.

Jasmine

| [aspngspeed] member Click here to reveal e-mail address = YOUR ID
| http://www.asplists.com/asplists/aspngspeed.asp = JOIN/QUIT
| http://www.asplists.com/search = SEARCH Archives
Reply to this message...
 
    
Ryan Trudelle-Schwarz (VIP)
Sure. Actually this slightly simpler version will work just fine as
well:

"(?:.|\n){1,12}\s"

It goes like so:

The (?: ) grouping means don't remember this. Not required but could
boost performance a tiny bit (might also hurt it, not sure). It would
also work with a normal "remember the match to this bit" grouping: ( ).

The inside is an option between . and \n. \n of course is the new line
character, and . is everything else.

The second part is a quantifier saying, match a string of between 1 and
12 of the preceeding pattern (grouping). There is an implied greediness
to this in that specified as such, it will match "as many as possible".
You can also do the opposite (not for this case but in others) which is
the non-greedy approach which means "match as few as possible", the
change for that is to add a question mark after the quantifier.

The last is the "\s" which tells it that it must be fallowed by a white
space character. White spaces are defined as: tab, vertical tab,
carriage return, new line and form feed and a space (I really wish
they'd include chr 160 in here but alas, that's just a wish).

So in english that pattern is saying:

"Give me between one and twelve of any character that is followed by a
white space character."

The bit I removed is a grouping construct that basically says: Once
you've parsed this bit ignore it. I'd thought it might be necessary for
preventing matches from overlapping but tested and it works fine without
(which makes sense but it was a safety precaution ;)

Microsoft put together a great reference on regexps available in the
.net sdk or online at:

http://msdn.microsoft.com/library//en-us/cpgenref/html/cpconregularexpre
ssionslanguageelements.asp
<http://msdn.microsoft.com/library/en-us/cpgenref/html/cpconregularexpre
ssionslanguageelements.asp>

hth, Ryan

-----Original Message-----
From: Chan, Jasmine [mailto:Click here to reveal e-mail address]

I tried your code and it works. Here is the code in VB:

Dim x As New Regex("(?>(?:.|\n){1,80}\s)")

intLineCnt = x.Matches(input).Count()

This is much cleaner then splitting the array and then do the
concatenation and then the check. Can you explain what the regular
expression is doing? I tried reading it but it looks like Greek to me.
Thanks!

Jasmine

-----Original Message-----
From: Ryan Trudelle-Schwarz [mailto:Click here to reveal e-mail address]

Shoulda mentioned also that the 12 in there is the limit so just swap in
whatever you want.

-----Original Message-----
From: Ryan Trudelle-Schwarz [mailto:Click here to reveal e-mail address]

Or this seems to work as well (not positive it will, haven't thoroughly
tested yet):

string strTemp = "your string here";

Regex x = new Regex(@"(?>(?:.|\n){1,12}\s)");

Return x.Matches(strTemp).Count;

Let me know if it does work for your lengthy strings, I'm very curious
:)

Hth, Ryan

-----Original Message-----
From: Sterling Bates [mailto:Click here to reveal e-mail address]

Here's an alternative that requires no splitting or re-joining:

lines = 1; ' Assume one line of text

i = 1; ' if VB is 1-based instead of 0-based

while (i < length(input))

i = i + limit;

if (i < length(input)) then ' Only go back if we aren't
at the end

while (mid(input,i,1) <> " ") ' Back up to the nearest space

i = i - 1

wend

end if

lines = lines + 1

wend

if (length(input) = 0) then

lines = 0

end if

You may have to tweak it a little but it should work for the most part.

-----Original Message-----
From: Chan, Jasmine [mailto:Click here to reveal e-mail address]

Hi all,

I have a function that takes in a string input and return
how many lines this string will take up. This function can be called
many times throughout my code. Is there a better way of doing this then
the following steps:

1. split it into an array base upon the space

2. start concatenating the string together

3. checked if the concatenated string is greater then my line
limit, if so increment the count and start the next string again.

Here is the pseudo-code:

arr = split(input, " ")

For i=0 to arr.length - 1

tmp = tmp & arr(i)

if len(tmp) > limit then

count= count + 1

tmp = ""

end if

Next

Return count

Thanks in advance for your help.

Jasmine
Reply to this message...
 
    
Dan Wahlin
Thanks for the great explanation. :-)

Dan

---------------------------------------------------------------------
Get focused ASP.NET/XML articles and code at
<http://www.XMLforASP.NET> http://www.XMLforASP.NET

<http://www.amazon.com/exec/obidos/ASIN/0672320398/ref=ase_xmlforaspnetd
-20/103-9498093-2544665> XML for ASP.NET Developers
by Dan Wahlin
In bookstores everywhere!
---------------------------------------------------------------------

-----Original Message-----
From: Ryan Trudelle-Schwarz [mailto:Click here to reveal e-mail address]
Sent: Thursday, January 10, 2002 6:30 PM
To: aspngspeed
Subject: [aspngspeed] RE: Is there a faster string manipulation?

Sure. Actually this slightly simpler version will work just fine as
well:
"(?:.|\n){1,12}\s"

It goes like so:
The (?: ) grouping means don't remember this. Not required but could
boost performance a tiny bit (might also hurt it, not sure). It would
also work with a normal "remember the match to this bit" grouping: ( ).

The inside is an option between . and \n. \n of course is the new line
character, and . is everything else.

The second part is a quantifier saying, match a string of between 1 and
12 of the preceeding pattern (grouping). There is an implied greediness
to this in that specified as such, it will match "as many as possible".
You can also do the opposite (not for this case but in others) which is
the non-greedy approach which means "match as few as possible", the
change for that is to add a question mark after the quantifier.

The last is the "\s" which tells it that it must be fallowed by a white
space character. White spaces are defined as: tab, vertical tab,
carriage return, new line and form feed and a space (I really wish
they'd include chr 160 in here but alas, that's just a wish).

So in english that pattern is saying:
"Give me between one and twelve of any character that is followed by a
white space character."

The bit I removed is a grouping construct that basically says: Once
you've parsed this bit ignore it. I'd thought it might be necessary for
preventing matches from overlapping but tested and it works fine without
(which makes sense but it was a safety precaution ;)

Microsoft put together a great reference on regexps available in the
.net sdk or online at:
http://msdn.microsoft.com/library//en-us/cpgenref/html/cpconregularexpre
ssionslanguageelements.asp
<http://msdn.microsoft.com/library/en-us/cpgenref/html/cpconregularexpre
ssionslanguageelements.asp>

hth, Ryan

-----Original Message-----
From: Chan, Jasmine [mailto:Click here to reveal e-mail address]

I tried your code and it works. Here is the code in VB:

Dim x As New Regex("(?>(?:.|\n){1,80}\s)")
intLineCnt = x.Matches(input).Count()

This is much cleaner then splitting the array and then do the
concatenation and then the check. Can you explain what the regular
expression is doing? I tried reading it but it looks like Greek to me.
Thanks!

Jasmine

-----Original Message-----
From: Ryan Trudelle-Schwarz [mailto:Click here to reveal e-mail address]

Shoulda mentioned also that the 12 in there is the limit so just swap in
whatever you want.

-----Original Message-----
From: Ryan Trudelle-Schwarz [mailto:Click here to reveal e-mail address]

Or this seems to work as well (not positive it will, haven't thoroughly
tested yet):

string strTemp = "your string here";
Regex x = new Regex(@"(?>(?:.|\n){1,12}\s)");
Return x.Matches(strTemp).Count;

Let me know if it does work for your lengthy strings, I'm very curious
:)

Hth, Ryan

-----Original Message-----
From: Sterling Bates [mailto:Click here to reveal e-mail address]

Here's an alternative that requires no splitting or re-joining:

lines = 1; ' Assume one line of text
i = 1; ' if VB is 1-based instead of 0-based
while (i < length(input))
i = i + limit;
if (i < length(input)) then ' Only go back if we aren't
at the end
while (mid(input,i,1) <> " ") ' Back up to the nearest space
i = i - 1
wend
end if
lines = lines + 1
wend
if (length(input) = 0) then
lines = 0
end if

You may have to tweak it a little but it should work for the most part.
-----Original Message-----
From: Chan, Jasmine [mailto:Click here to reveal e-mail address]
Hi all,
I have a function that takes in a string input and return
how many lines this string will take up. This function can be called
many times throughout my code. Is there a better way of doing this then
the following steps:

1. split it into an array base upon the space
2. start concatenating the string together
3. checked if the concatenated string is greater then my line
limit, if so increment the count and start the next string again.

Here is the pseudo-code:

arr = split(input, " ")
For i=0 to arr.length - 1
tmp = tmp & arr(i)
if len(tmp) > limit then
count= count + 1
tmp = ""
end if
Next
Return count

Thanks in advance for your help.

Jasmine
| [aspngspeed] member Click here to reveal e-mail address = YOUR ID
| http://www.asplists.com/asplists/aspngspeed.asp = JOIN/QUIT
| http://www.asplists.com/search = SEARCH Archives
Reply to this message...
 
    
Steven A Smith (VIP)
*cough*
Add it to http://regexlib/
*cough*
----- Original Message -----
From: Ryan Trudelle-Schwarz
To: aspngspeed
Sent: Thursday, January 10, 2002 6:06 PM
Subject: [aspngspeed] RE: Is there a faster string manipulation?

Or this seems to work as well (not positive it will, haven't thoroughly tested yet):

string strTemp = "your string here";

Regex x = new Regex(@"(?>(?:.|\n){1,12}\s)");

Return x.Matches(strTemp).Count;

Let me know if it does work for your lengthy strings, I'm very curious :)

Hth, Ryan

-----Original Message-----
From: Sterling Bates [mailto:Click here to reveal e-mail address]

Here's an alternative that requires no splitting or re-joining:

lines = 1; ' Assume one line of text

i = 1; ' if VB is 1-based instead of 0-based

while (i < length(input))

i = i + limit;

if (i < length(input)) then ' Only go back if we aren't at the end

while (mid(input,i,1) <> " ") ' Back up to the nearest space

i = i - 1

wend

end if

lines = lines + 1

wend

if (length(input) = 0) then

lines = 0

end if

You may have to tweak it a little but it should work for the most part.

-----Original Message-----
From: Chan, Jasmine [mailto:Click here to reveal e-mail address]

Hi all,

I have a function that takes in a string input and return how many lines this string will take up. This function can be called many times throughout my code. Is there a better way of doing this then the following steps:

1. split it into an array base upon the space

2. start concatenating the string together

3. checked if the concatenated string is greater then my line limit, if so increment the count and start the next string again.

Here is the pseudo-code:

arr = split(input, " ")

For i=0 to arr.length - 1

tmp = tmp & arr(i)

if len(tmp) > limit then

count= count + 1

tmp = ""

end if

Next

Return count

Thanks in advance for your help.

Jasmine

| [aspngspeed] member Click here to reveal e-mail address = YOUR ID
| http://www.asplists.com/asplists/aspngspeed.asp = JOIN/QUIT
| http://www.asplists.com/search = SEARCH Archives
Reply to this message...
 
    
Chan, Jasmine
Thanks for the explanation! :)

Jasmine

-----Original Message-----
From: Ryan Trudelle-Schwarz [mailto:Click here to reveal e-mail address]
Sent: Thursday, January 10, 2002 8:30 PM
To: aspngspeed
Subject: [aspngspeed] RE: Is there a faster string manipulation?

Sure. Actually this slightly simpler version will work just fine as well:
"(?:.|\n){1,12}\s"

It goes like so:
The (?: ) grouping means don't remember this. Not required but could boost
performance a tiny bit (might also hurt it, not sure). It would also work
with a normal "remember the match to this bit" grouping: ( ).

The inside is an option between . and \n. \n of course is the new line
character, and . is everything else.

The second part is a quantifier saying, match a string of between 1 and 12
of the preceeding pattern (grouping). There is an implied greediness to this
in that specified as such, it will match "as many as possible". You can also
do the opposite (not for this case but in others) which is the non-greedy
approach which means "match as few as possible", the change for that is to
add a question mark after the quantifier.

The last is the "\s" which tells it that it must be fallowed by a white
space character. White spaces are defined as: tab, vertical tab, carriage
return, new line and form feed and a space (I really wish they'd include chr
160 in here but alas, that's just a wish).

So in english that pattern is saying:
"Give me between one and twelve of any character that is followed by a
white space character."

The bit I removed is a grouping construct that basically says: Once you've
parsed this bit ignore it. I'd thought it might be necessary for preventing
matches from overlapping but tested and it works fine without (which makes
sense but it was a safety precaution ;)

Microsoft put together a great reference on regexps available in the .net
sdk or online at:
http://msdn.microsoft.com/library//en-us/cpgenref/html/cpconregularexpressio
nslanguageelements.asp
<http://msdn.microsoft.com/library/en-us/cpgenref/html/cpconregularexpressio
nslanguageelements.asp>

hth, Ryan

-----Original Message-----
From: Chan, Jasmine [mailto:Click here to reveal e-mail address]

I tried your code and it works. Here is the code in VB:

Dim x As New Regex("(?>(?:.|\n){1,80}\s)")
intLineCnt = x.Matches(input).Count()

This is much cleaner then splitting the array and then do the concatenation
and then the check. Can you explain what the regular expression is doing?
I tried reading it but it looks like Greek to me. Thanks!

Jasmine

-----Original Message-----
From: Ryan Trudelle-Schwarz [mailto:Click here to reveal e-mail address]

Shoulda mentioned also that the 12 in there is the limit so just swap in
whatever you want.

-----Original Message-----
From: Ryan Trudelle-Schwarz [mailto:Click here to reveal e-mail address]

Or this seems to work as well (not positive it will, haven't thoroughly
tested yet):

string strTemp = "your string here";
Regex x = new Regex(@"(?>(?:.|\n){1,12}\s)");
Return x.Matches(strTemp).Count;

Let me know if it does work for your lengthy strings, I'm very curious :)

Hth, Ryan

-----Original Message-----
From: Sterling Bates [mailto:Click here to reveal e-mail address]

Here's an alternative that requires no splitting or re-joining:

lines = 1; ' Assume one line of text
i = 1; ' if VB is 1-based instead of 0-based
while (i < length(input))
i = i + limit;
if (i < length(input)) then ' Only go back if we aren't at
the end
while (mid(input,i,1) <> " ") ' Back up to the nearest space
i = i - 1
wend
end if
lines = lines + 1
wend
if (length(input) = 0) then
lines = 0
end if

You may have to tweak it a little but it should work for the most part.
-----Original Message-----
From: Chan, Jasmine [mailto:Click here to reveal e-mail address]
Hi all,
I have a function that takes in a string input and return how
many lines this string will take up. This function can be called many
times throughout my code. Is there a better way of doing this then the
following steps:

1. split it into an array base upon the space
2. start concatenating the string together
3. checked if the concatenated string is greater then my line limit, if
so increment the count and start the next string again.

Here is the pseudo-code:

arr = split(input, " ")
For i=0 to arr.length - 1
tmp = tmp & arr(i)
if len(tmp) > limit then
count= count + 1
tmp = ""
end if
Next
Return count

Thanks in advance for your help.

Jasmine
| [aspngspeed] member Click here to reveal e-mail address = YOUR ID
| http://www.asplists.com/asplists/aspngspeed.asp = JOIN/QUIT
| http://www.asplists.com/search = SEARCH Archives
Reply to this message...
 
 
System.Text.RegularExpressions.Regex
System.Text.StringBuilder




ExamGuru IT Solutions - .Net Guru is owned and operated by ExamGuru, Inc., the man behind .Net Guru. If you're in the market for bespoke software or software consultancy, why not get him and his highly trained team to help? - www.examguru.net/ITCertification
Ad


Need Dot Net Interview Questions?
Ask ExamGuru, Inc. for advice and help on Passing .Net Interviews
.Net Projects
Best-of-breed application framework for .NET projects, developed by ExamGuru, Inc. and ExamGuru IT
Free .net Help
Commission ExamGuru, Inc. and his team for your next bespoke software project
FogBUGZ
The only bug tracking system carefully crafted with one goal in mind: helping teams create great software.
Awesome Tools
If you don't know about these, you're missing out... IT Certification Questions
IT Interview Questions
Free Oracle 10g Training
MCSE Boortcamp
Cisco Study Guides
Cheap Study Guides
Exact Questions
Dot Net Interview Questions
Oracle OCP
Cheap Travel
Designer Perfumes - Wholesale Prices
Free Programming Tutorials
 
ExamGuru IT Solutions - .Net Guru is owned and operated by ExamGuru, Inc., the man behind .Net Guru. If you're in the market for bespoke software or software consultancy, why not get him and his highly trained team to help? - www.examguru.net/ITCertification
 Copyright © ExamGuru, Inc. 2001-2006
Contact Us - Terms of Use - Privacy Policy - www.dot-net-guru.com - www.examguru.net - www.oraclesource.net - www.itinterviews.net - www.examguru.net/ITCertification