|
-
Mar 4th, 2005, 06:15 AM
#1
Thread Starter
New Member
Split method => what to do with more than one space
Hi,
I'm able of splitting a string lik "Hallo, my name is.." with the "split method"
But what to do if there are more than one space between the words????
split(string, " ") only works if there's exactly one space between them... I've been searching for a couple of houres now
Anyone who can help?
-
Mar 4th, 2005, 06:31 AM
#2
Hyperactive Member
Re: Split method => what to do with more than one space
What about doing a replace on the string first?
VB Code:
Dim splitStrings() As String
Dim myString As String = "Hello World! My name is Lil Ms Squirrel"
myString = myString.Replace(" ", " ") 'replace any double spaces with a single space
splitStrings = myString.Split(" ")
Be who you are and say what you feel, because those who mind don't matter and those who matter don't mind.
Dr. Seuss 
-
Mar 4th, 2005, 09:47 AM
#3
PowerPoster
Re: Split method => what to do with more than one space
[QUOTE=Wirloff]Hi,
split(string, " ") only works if there's exactly one space between them...
QUOTE]
Hi,
What do you mean ? It still works and puts the second space of a pair on it's own into one of the array elements
Taxes
The more I learn about VB.NET the more I like dBaseIII Plus
The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.
-
Mar 4th, 2005, 10:00 AM
#4
Re: Split method => what to do with more than one space
 Originally Posted by Lil Ms Squirrel
What about doing a replace on the string first?
VB Code:
Dim splitStrings() As String
Dim myString As String = "Hello World! My name is Lil Ms Squirrel"
myString = myString.Replace(" ", " ") 'replace any double spaces with a single space
splitStrings = myString.Split(" ")
then squish that into 1 line of code
VB Code:
Dim splitStrings() As String = "Hello World! My name is Lil Ms Squirrel".Replace(" ", " ").Split(" "c)
-
Mar 4th, 2005, 10:27 AM
#5
Re: Split method => what to do with more than one space
That's ok but it gets a bit messy when you get text with loads of spaces and not just 2.
If you had a string containing 10 consecutive spaces then you'd have to call replace 9 times!! Not very efficient!
Ignore multiple spaces completely, no replace required...
(look at the string this time!)
VB Code:
Dim splitStrings(), temp() As String
Dim myString As String = [B]"Hello World! My name is Lil Ms Squirrel"[/B]
temp = myString.Split(" "c)
Dim i, j, upper As Integer
upper = temp.Length - 1
splitStrings = New String(upper) {} 'create another string array with same no. of elements
'copy all the actual words to the new array (don't copy any empty strings)
j = -1
For i = 0 To upper
If temp(i).Length > 0 Then 'not empty?
j += 1
splitStrings(j) = temp(i)
End If
Next i
temp = Nothing 'scrap the teporary array that has loads of empty strings in it
If j >= 0 Then
ReDim Preserve splitStrings(j) 'shrink the clean data array to fit the number of actual words found
End If
[B]'do something with splitstrings here[/B]
Last edited by wossname; Mar 4th, 2005 at 10:32 AM.
I don't live here any more.
-
Mar 4th, 2005, 10:58 AM
#6
Re: Split method => what to do with more than one space
woss you could just do this though
VB Code:
Dim myString As String = "Hello World! My name is Lil Ms Squirrel"
Do Until myString.IndexOf(" ") = -1
myString = myString.Replace(" ", " ")
Loop
because im bored i did a speed test on them and they are neck and neck, but this code even finished slightly faster on a few high loop runs...
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|