Results 1 to 6 of 6

Thread: Split method => what to do with more than one space

  1. #1

    Thread Starter
    New Member
    Join Date
    Mar 2005
    Posts
    4

    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?

  2. #2
    Hyperactive Member Lil Ms Squirrel's Avatar
    Join Date
    Nov 2004
    Location
    planet squirrel
    Posts
    494

    Re: Split method => what to do with more than one space

    What about doing a replace on the string first?
    VB Code:
    1. Dim splitStrings() As String
    2.         Dim myString As String = "Hello  World! My  name is Lil  Ms Squirrel"
    3.         myString = myString.Replace("  ", " ") 'replace any double spaces with a single space
    4.         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

  3. #3
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,949

    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.

  4. #4
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: Split method => what to do with more than one space

    Quote Originally Posted by Lil Ms Squirrel
    What about doing a replace on the string first?
    VB Code:
    1. Dim splitStrings() As String
    2.         Dim myString As String = "Hello  World! My  name is Lil  Ms Squirrel"
    3.         myString = myString.Replace("  ", " ") 'replace any double spaces with a single space
    4.         splitStrings = myString.Split(" ")

    then squish that into 1 line of code
    VB Code:
    1. Dim splitStrings() As String = "Hello  World! My  name is Lil  Ms Squirrel".Replace("  ", " ").Split(" "c)

  5. #5
    type Woss is new Grumpy; wossname's Avatar
    Join Date
    Aug 2002
    Location
    #!/bin/bash
    Posts
    5,682

    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:
    1. Dim splitStrings(), temp() As String
    2.         Dim myString As String = [B]"Hello    World!     My   name                is Lil         Ms Squirrel"[/B]
    3.         temp = myString.Split(" "c)
    4.  
    5.         Dim i, j, upper As Integer
    6.         upper = temp.Length - 1
    7.         splitStrings = New String(upper) {}       'create another string array with same no. of elements
    8.  
    9.         'copy all the actual words to the new array (don't copy any empty strings)
    10.         j = -1
    11.         For i = 0 To upper
    12.             If temp(i).Length > 0 Then        'not empty?
    13.                 j += 1
    14.                 splitStrings(j) = temp(i)
    15.             End If
    16.         Next i
    17.  
    18.         temp = Nothing     'scrap the teporary array that has loads of empty strings in it
    19.  
    20.         If j >= 0 Then
    21.             ReDim Preserve splitStrings(j)        'shrink the clean data array to fit the number of actual words found
    22.         End If
    23.  
    24.         [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.

  6. #6
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: Split method => what to do with more than one space

    woss you could just do this though
    VB Code:
    1. Dim myString As String = "Hello    World!     My   name                is Lil         Ms Squirrel"
    2.             Do Until myString.IndexOf("  ") = -1
    3.                 myString = myString.Replace("  ", " ")
    4.             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
  •  



Click Here to Expand Forum to Full Width