Results 1 to 5 of 5

Thread: Trimming question

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Mar 2002
    Location
    Seattle, WA USA
    Posts
    216

    Trimming question

    Here's a quick string trimming question. I have the following strings:

    -1 HELLO HELLO2
    56 'TESTING YOU'
    0 AGAIN

    How do I trim out only the first number up until the space. Also, how do I trim out all of the text right after the first space?

    Thanks!

    -- Ethan --
    VB6, VB.NET, C#, SQL, XML, ADO.NET, ASP.NET, HTML.
    MCP & A+ Certified. Looking for a job in the Seattle, WA area.

  2. #2
    Fanatic Member Mr.No's Avatar
    Join Date
    Sep 2002
    Location
    Mauritius
    Posts
    651
    Try this

    VB Code:
    1. Dim strList As String = "-1 HELLO HELLO2"
    2.        Dim words() As String
    3.       ' Split text using space as separator
    4.        words = strList.Split(" ")
    5.        MessageBox.Show(words(0))

    Or you could use try this one
    VB Code:
    1. Dim strList as string = ""-1 HELLO HELLO2"
    2.         MessageBox.Show(strList.Substring(0, strList.IndexOf(" ")))
    Using VB.NET 2003/.NET 1.1/C# 2.0
    http://del.icio.us/rajoo
    Blow your mind, smoke gunpowder
    Ashes to ashes, dust to dust
    If God won't have you, the devil will. - Author unknown
    Don't follow me, I'm lost too ...

  3. #3
    l33t! MrPolite's Avatar
    Join Date
    Sep 2001
    Posts
    4,428
    I dont have vs.net installed right now, but doesnt the split functino get a parameter that specifies the number of elements in the returning array?
    that way you would get an array with two elements, the first one being teh number and the second the rest of the message

    I might be totaly wrong though just guessing
    rate my posts if they help ya!
    Extract thumbnail without reading the whole image file: (C# - VB)
    Apply texture to bitmaps: (C# - VB)
    Extended console library: (VB)
    Save JPEG with a certain quality (image compression): (C# - VB )
    VB.NET to C# conversion tips!!

  4. #4
    Fanatic Member Mr.No's Avatar
    Join Date
    Sep 2002
    Location
    Mauritius
    Posts
    651
    Thanks for your suggestion MrPolite. I tried the code below and it won't do what ethanwa is looking for.


    VB Code:
    1. Dim strList As String = "-1 HELLO HELLO2"
    2.        Dim words() As String
    3.       ' Split text using space as separator
    4.        words = strList.Split(" ", 1)
    5.        MessageBox.Show(words(0))
    6. --------------------------------------------------------------------------------

    The above code will display "-1 HELLO HELLO2". So, it seems that the second parameter specifies the number of elements in the array.
    Using VB.NET 2003/.NET 1.1/C# 2.0
    http://del.icio.us/rajoo
    Blow your mind, smoke gunpowder
    Ashes to ashes, dust to dust
    If God won't have you, the devil will. - Author unknown
    Don't follow me, I'm lost too ...

  5. #5
    l33t! MrPolite's Avatar
    Join Date
    Sep 2001
    Posts
    4,428
    Originally posted by Mr.No
    Thanks for your suggestion MrPolite. I tried the code below and it won't do what ethanwa is looking for.


    VB Code:
    1. Dim strList As String = "-1 HELLO HELLO2"
    2.        Dim words() As String
    3.       ' Split text using space as separator
    4.        words = strList.Split(" ", 1)
    5.        MessageBox.Show(words(0))
    6. --------------------------------------------------------------------------------

    The above code will display "-1 HELLO HELLO2". So, it seems that the second parameter specifies the number of elements in the array.
    try passing "2" as the argument. when you pass "1" it means that the function will only return one element. 2 means it will try to split the string in only two parts. (it might not split it at all if there isnt a space in your string)


    VB Code:
    1. Dim strList As String = "-1 HELLO HELLO2"
    2.        Dim words() As String
    3.       ' Split text using space as separator
    4.        words = strList.Split(" ", [b][size=4][color=red]2[/color][/size][/b])
    5.        MessageBox.Show(words(0))
    rate my posts if they help ya!
    Extract thumbnail without reading the whole image file: (C# - VB)
    Apply texture to bitmaps: (C# - VB)
    Extended console library: (VB)
    Save JPEG with a certain quality (image compression): (C# - VB )
    VB.NET to C# conversion tips!!

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