-
Jul 26th, 2020, 10:27 AM
#1
Thread Starter
Addicted Member
[SOLVED] Multiple Spaces using Split String Method
Hi,
I have an interesting task I am trying to figure out.
I need to get a whole string even if it has one or two spaces
Example:
"Song of Solomon 2:1-5"
"1 Peter 2:1-5"
I need to be able to get "Song of Solomon" and "1 Peter" as a complete string.
I understand the split somewhat (see code below) but I have never come accross a situation where there can be multiple spaces.
In this code, I can split them out by "spaces", "-" and ":"'s
See output below
CODE:
Code:
Dim txt As String = "Song of Solomon 2:1-5"
Dim strarr As String() = txt.Split(New Char() {" "c, "-"c, ":"c}, StringSplitOptions.RemoveEmptyEntries)
For i As Integer = 0 To strarr.Length - 1
Debug.Print(strarr(i))
Next
OUTPUT
I can get the numbers I need fine. It's just "Book" strings that contains 1 or 2 spaces.
So, how do I account for one or two paces in the "Book" portion of a string?
Each string can be different.
Any ideas are welcomed!
Thanks
Last edited by pixelink; Jul 26th, 2020 at 11:16 AM.
-
Jul 26th, 2020, 10:42 AM
#2
Re: Multiple Spaces using Split String Method
You wouldn't use Split for this. You'd use LastIndexOf to get the index of the last space and then Substring to get the substrings on either side of that.
vb.net Code:
Dim lastSpaceIndex = myString.LastIndexOf(" "c)
Dim prefix = myString.Substring(0, lastSpaceIndex)
Dim suffix = myString.Substring(lastSpaceIndex + 1)
You can then split the suffix on colons and dashes if required.
-
Jul 26th, 2020, 11:06 AM
#3
New Member
Re: Multiple Spaces using Split String Method
First, you don't know if the user might include more than single space. So you have replace all multiple spaces with single space.
Code:
Sub GetStr(str As String)
Dim res As String
Dim lastspace As Integer
str = Regex.Replace(s, " {2,}", " ")
lastspace = str.LastIndexOf(" ")
res = str.SubString(0,lastspace)
MsgBox (res)
End Sub
Last edited by dday9; Jul 26th, 2020 at 03:33 PM.
-
Jul 26th, 2020, 11:08 AM
#4
Thread Starter
Addicted Member
Re: Multiple Spaces using Split String Method
Originally Posted by jmcilhinney
You wouldn't use Split for this. You'd use LastIndexOf to get the index of the last space and then Substring to get the substrings on either side of that.
vb.net Code:
Dim lastSpaceIndex = myString.LastIndexOf(" "c)
Dim prefix = myString.Substring(0, lastSpaceIndex)
Dim suffix = myString.Substring(lastSpaceIndex + 1)
You can then split the suffix on colons and dashes if required.
Would of never thought of that.
I was able to return "Song of Solomon"
I am still fleshing out the rest of the code.
Thanks JMC for the tip
@user0001 - Yes, I will look into this too because it would be a problem esspecially using the string to fetch records from a DB.
Thanks for the tip user001
Last edited by pixelink; Jul 26th, 2020 at 11:20 AM.
-
Jul 26th, 2020, 11:15 AM
#5
Thread Starter
Addicted Member
Re: Multiple Spaces using Split String Method
OKay... this is the fleshed out code that gives me what I need
VB CODE (THAT WORKS)
Code:
Dim txt As String = "Song of Solomon 2:1-10"
Dim lastSpaceIndex1 = txt.LastIndexOf(" "c)
Dim prefix = txt.Substring(0, lastSpaceIndex1)
Debug.Print(prefix)
Dim lastSpaceIndex2 = txt.LastIndexOf(" "c)
Dim suffix = txt.Substring(lastSpaceIndex2 + 1)
Debug.Print(suffix)
Debug.Print("------------------------")
Dim strarr As String() = suffix.Split(New Char() {"-"c, ":"c}, StringSplitOptions.RemoveEmptyEntries)
For i As Integer = 0 To strarr.Length - 1
Debug.Print(strarr(i))
Next
OUTPUT
Song of Solomon
2:1-10
------------------------
2
1
10
This is prototype code.
Now I can actually use it in my app and hook it up to my function
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
|