Results 1 to 10 of 10

Thread: Split a sentence

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2012
    Posts
    423

    Split a sentence

    I have a string of data and in the middle i've got a booking reference, I want to split my string at this reference point. it's 9 characters long. I can split a string at a single char but not a word or string?

    how can I do this?

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,302

    Re: Split a sentence

    You can't just pluck code out of the air. You have to know what the rules are first and then implement those rules in code. So, what are the rules? If you were going to give a person a piece of paper containing your text and you wanted them to write down the split parts, what instructions would you give them to follow? If you don't know then how can you possibly write code to do it? Think of the rules first and then try to write code to implement those rules. If you try and fail, THEN you ask us for help, showing us what you've already done and how the result differed from expectation.

  3. #3
    Fanatic Member Toph's Avatar
    Join Date
    Oct 2014
    Posts
    655

    Re: Split a sentence

    Dim Splitter() As String = MyVariable.Split(":".ToCharArrary())

  4. #4
    PowerPoster
    Join Date
    Sep 2006
    Location
    Egypt
    Posts
    2,579

    Re: Split a sentence

    Split function can help when delimiter is two or more chars

    Code:
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim s As String = "A delimiter B delimiter C delimiter D delimiter i delimiter R"
            Dim a() As String = Split(s, "delimiter")
            For Each i As String In a
                Debug.Print(i)
            Next
        End Sub



  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,302

    Re: Split a sentence

    Quote Originally Posted by 4x2y View Post
    Split function can help when delimiter is two or more chars

    Code:
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim s As String = "A delimiter B delimiter C delimiter D delimiter i delimiter R"
            Dim a() As String = Split(s, "delimiter")
            For Each i As String In a
                Debug.Print(i)
            Next
        End Sub
    That function should never be used in VB.NET code unless it's upgraded from VB6 and already uses it. The .NET String.Split method can handle multi-character strings as delimiters.

  6. #6
    Still learning kebo's Avatar
    Join Date
    Apr 2004
    Location
    Gardnerville,nv
    Posts
    3,757

    Re: Split a sentence

    I use the regex.split method to split a sentence using a word.

    Code:
        Dim splittee As String = "this is my dog. she is a nice dog sometimes"
            Dim splitter As String = "dog"
            Dim splits() As String = System.Text.RegularExpressions.Regex.Split(splittee, splitter)
    kevin
    Process control doesn't give you good quality, it gives you consistent quality.
    Good quality comes from consistently doing the right things.

    Vague general questions have vague general answers.
    A $100 donation is required for me to help you if you PM me asking for help. Instructions for donating to one of our local charities will be provided.

    ______________________________
    Last edited by kebo : Now. Reason: superfluous typo's

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,302

    Re: Split a sentence

    Quote Originally Posted by kebo View Post
    I use the regex.split method to split a sentence using a word.

    Code:
        Dim splittee As String = "this is my dog. she is a nice dog sometimes"
            Dim splitter As String = "dog"
            Dim splits() As String = System.Text.RegularExpressions.Regex.Split(splittee, splitter)
    kevin
    There's no point using Regex.Split if you're splitting on a word. The whole point of Regex.Split is that you can split on patterns, so the delimiter might have a different value in each case. It will work but it seems a bit silly when that's exactly what String.Split is for.

  8. #8
    PowerPoster
    Join Date
    Sep 2006
    Location
    Egypt
    Posts
    2,579

    Re: Split a sentence

    Quote Originally Posted by jmcilhinney View Post
    That function should never be used in VB.NET code unless it's upgraded from VB6 and already uses it. The .NET String.Split method can handle multi-character strings as delimiters.
    I cannot figure out how to make NET Split return the same result as Vb6 Split with the given example
    Code:
    "A delimiter B delimiter C delimiter D delimiter i delimiter R"
    VB6 Split return "A B C D i R"

    i tried
    Code:
    Dim a() As String = s.Split("delimiter".ToCharArray, System.StringSplitOptions.RemoveEmptyEntries)
    the result is "A B C D R"



  9. #9
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,302

    Re: Split a sentence

    Quote Originally Posted by 4x2y View Post
    I cannot figure out how to make NET Split return the same result as Vb6 Split with the given example
    Code:
    "A delimiter B delimiter C delimiter D delimiter i delimiter R"
    VB6 Split return "A B C D i R"

    i tried
    Code:
    Dim a() As String = s.Split("delimiter".ToCharArray, System.StringSplitOptions.RemoveEmptyEntries)
    the result is "A B C D R"
    You need to use one of the overloads that takes Strings as the delimiters rather than Chars.
    Code:
    Dim a = s.Split({"delimiter"}, StringSplitOptions.None)
    I think you'll find that that code will behave as Strings.Split does.

  10. #10
    PowerPoster
    Join Date
    Sep 2006
    Location
    Egypt
    Posts
    2,579

    Re: Split a sentence

    Yes,
    Code:
    Dim a = s.Split({"delimiter"}, StringSplitOptions.None)
    return the same result as VB6 Split

    Thanks!



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