Answered by:
The most efficient code to split string variable and stored it into array

Question
-
User1899595498 posted
For example, I have string variable (MyString) as below.
MyString = “First word || second order || third example || fourth item || fifth stuff”
There are separator “||” at MyString variable. I want MyString variable is spited based on separator “||”and stored it into new array variable.
Could you advise me the most efficient code to do this job in VB? MyString is assumed has more than five items.
Thursday, July 21, 2011 7:58 AM
Answers
-
User-366017857 posted
Use this
Dim d1 As String = "First word || second order || third example || fourth item || fifth stuff".Replace("||", ",")
Dim data As String() = d1.Split(","C)- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Thursday, July 21, 2011 8:07 AM -
User1899595498 posted
Thanks Raigad for your idea.
The perfect solution that is always working is below code:
Dim MyInput As String = "First word||second order||third |example|| fourth | item ||fifth stuff" Dim MyArrayString() As String = Split(MyInput, "||", -1, CompareMethod.Text)
That code will split string based on exactly-string "||". So that, one character string "|" will not be categorized as separator.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Thursday, July 21, 2011 6:44 PM
All replies
-
User-366017857 posted
Use this
Dim d1 As String = "First word || second order || third example || fourth item || fifth stuff".Replace("||", ",")
Dim data As String() = d1.Split(","C)- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Thursday, July 21, 2011 8:07 AM -
User1899595498 posted
Your code is not working perfectly, because when I change the input string = "First , word || second , order || third , example || fourth | item || fifth | stuff"
then, your code return string: “First” , “word” , “second” , “order” , “third”…. and so forth. This is not my goal. My goal is to return string that is based on only separator “||”. So that, the returning string should be: “First , word” , “second , order” , “third , example” , “fourth | item” , “fifth | stuff"
Any suggestion to solve this code efficiently?
Thursday, July 21, 2011 9:52 AM -
User-211177096 posted
Use Regex.Split method : http://msdn.microsoft.com/en-us/library/8yttk7sy.aspx
Dim input As String = "First , word || second , order || third , example || fourth | item || fifth | stuff" Dim pattern As String = "\\|\\|" Dim substrings() As String = Regex.Split(input, pattern)
Thursday, July 21, 2011 11:30 AM -
User1899595498 posted
Thanks Raigad for your idea.
The perfect solution that is always working is below code:
Dim MyInput As String = "First word||second order||third |example|| fourth | item ||fifth stuff" Dim MyArrayString() As String = Split(MyInput, "||", -1, CompareMethod.Text)
That code will split string based on exactly-string "||". So that, one character string "|" will not be categorized as separator.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Thursday, July 21, 2011 6:44 PM