-
Nov 2nd, 2014, 09:12 PM
#1
Thread Starter
Hyperactive Member
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?
-
Nov 2nd, 2014, 09:23 PM
#2
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.
-
Nov 3rd, 2014, 01:57 AM
#3
Re: Split a sentence
Dim Splitter() As String = MyVariable.Split(":".ToCharArrary())
-
Nov 3rd, 2014, 06:35 PM
#4
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
-
Nov 3rd, 2014, 06:40 PM
#5
Re: Split a sentence
Originally Posted by 4x2y
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.
-
Nov 3rd, 2014, 06:51 PM
#6
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
-
Nov 3rd, 2014, 06:56 PM
#7
Re: Split a sentence
Originally Posted by kebo
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.
-
Nov 3rd, 2014, 07:01 PM
#8
Re: Split a sentence
Originally Posted by jmcilhinney
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"
-
Nov 3rd, 2014, 07:18 PM
#9
Re: Split a sentence
Originally Posted by 4x2y
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.
-
Nov 3rd, 2014, 07:26 PM
#10
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|