Q: Separating a string into parts?

E

ECCO

SITUATION:

I have a string with the following pattern:

" [#######] XXXXX | XXXXXXXXXXXX | XXXXXXXXX "

Where # is a number, six characters in length always and X is text of
variable length.

EXAMPLES
#1) " [384922] Shade Stick | Maxwell House | Fine "
#2) " [999201] It something that is green | Re | Do So Me "

OBJECTIVE

I need to separate these sections and assign them to labels (Using
Example #1 from Above):

Label1.text = "384922"
Label2.text = "Shade Stick"
Label3.text = "Maxwell House"
Label4.text = "Fine"
 
F

Family Tree Mike

ECCO said:
SITUATION:

I have a string with the following pattern:

" [#######] XXXXX | XXXXXXXXXXXX | XXXXXXXXX "

Where # is a number, six characters in length always and X is text of
variable length.

EXAMPLES
#1) " [384922] Shade Stick | Maxwell House | Fine "
#2) " [999201] It something that is green | Re | Do So Me "

OBJECTIVE

I need to separate these sections and assign them to labels (Using
Example #1 from Above):

Label1.text = "384922"
Label2.text = "Shade Stick"
Label3.text = "Maxwell House"
Label4.text = "Fine"

Use the following to get an array of four strings:

pattern.Split(New Char() {" "c, "|"c}, _
StringSplitOptions.RemoveEmptyEntries)

Then just fill your labels with the array of strings.
 
S

Scott M.

That will leave the brakets around the first item in the returned array. It
should be:

Dim stringParts() As String = theString.Split(New Char() {"["c, "]"c, " "c,
"|"c}, StringSplitOptions.RemoveEmptyEntries)

-Scott

Family Tree Mike said:
ECCO said:
SITUATION:

I have a string with the following pattern:

" [#######] XXXXX | XXXXXXXXXXXX | XXXXXXXXX "

Where # is a number, six characters in length always and X is text of
variable length.

EXAMPLES
#1) " [384922] Shade Stick | Maxwell House | Fine "
#2) " [999201] It something that is green | Re | Do So Me "

OBJECTIVE

I need to separate these sections and assign them to labels (Using
Example #1 from Above):

Label1.text = "384922"
Label2.text = "Shade Stick"
Label3.text = "Maxwell House"
Label4.text = "Fine"

Use the following to get an array of four strings:

pattern.Split(New Char() {" "c, "|"c}, _
StringSplitOptions.RemoveEmptyEntries)

Then just fill your labels with the array of strings.
 
F

Family Tree Mike

Scott said:
That will leave the brakets around the first item in the returned array. It
should be:

Dim stringParts() As String = theString.Split(New Char() {"["c, "]"c, " "c,
"|"c}, StringSplitOptions.RemoveEmptyEntries)

-Scott

Doh! Absolutely right, thanks!
 
G

Göran Andersson

ECCO said:
SITUATION:

I have a string with the following pattern:

" [#######] XXXXX | XXXXXXXXXXXX | XXXXXXXXX "

Where # is a number, six characters in length always and X is text of
variable length.

EXAMPLES
#1) " [384922] Shade Stick | Maxwell House | Fine "
#2) " [999201] It something that is green | Re | Do So Me "

OBJECTIVE

I need to separate these sections and assign them to labels (Using
Example #1 from Above):

Label1.text = "384922"
Label2.text = "Shade Stick"
Label3.text = "Maxwell House"
Label4.text = "Fine"

Use a regular expression to define the pattern:

Dim s As String = " [384922] Shade Stick | Maxwell House | Fine "

Dim groups As GroupCollection = _
Regex.Match(s, "^ \[(\d{6})\] (.+?) \| (.+?) \| (.+) $").Groups

Label1.Text = groups(1).Value
Label2.Text = groups(2).Value
Label3.Text = groups(3).Value
Label4.Text = groups(4).Value
 
G

Göran Andersson

Family said:
ECCO said:
SITUATION:

I have a string with the following pattern:

" [#######] XXXXX | XXXXXXXXXXXX | XXXXXXXXX "

Where # is a number, six characters in length always and X is text of
variable length.

EXAMPLES
#1) " [384922] Shade Stick | Maxwell House | Fine "
#2) " [999201] It something that is green | Re | Do So Me "

OBJECTIVE

I need to separate these sections and assign them to labels (Using
Example #1 from Above):

Label1.text = "384922"
Label2.text = "Shade Stick"
Label3.text = "Maxwell House"
Label4.text = "Fine"

Use the following to get an array of four strings:

pattern.Split(New Char() {" "c, "|"c}, _
StringSplitOptions.RemoveEmptyEntries)

Then just fill your labels with the array of strings.

Theat will not give you four strings, it will give you six strings for
the first example and ten strings for the second example...
 
S

Scott M.

Hmmm. Forgot about the fact that the stirngs themselves have spaces in
them.

Dim stringParts() As String = theString.Split(New Char() {"["c, "]"c, "|"c},
StringSplitOptions.RemoveEmptyEntries)
lblPart1 = stringPart(0).Trim
lblPart2 = stringPart(1).Trim
lblPart3 = stringPart(2).Trim
lblPart4 = stringPart(3).Trim

Does the trick!

-Scott



Scott M. said:
That will leave the brakets around the first item in the returned array.
It should be:

Dim stringParts() As String = theString.Split(New Char() {"["c, "]"c, "
"c, "|"c}, StringSplitOptions.RemoveEmptyEntries)

-Scott

Family Tree Mike said:
ECCO said:
SITUATION:

I have a string with the following pattern:

" [#######] XXXXX | XXXXXXXXXXXX | XXXXXXXXX "

Where # is a number, six characters in length always and X is text of
variable length.

EXAMPLES
#1) " [384922] Shade Stick | Maxwell House | Fine "
#2) " [999201] It something that is green | Re | Do So Me "

OBJECTIVE

I need to separate these sections and assign them to labels (Using
Example #1 from Above):

Label1.text = "384922"
Label2.text = "Shade Stick"
Label3.text = "Maxwell House"
Label4.text = "Fine"

Use the following to get an array of four strings:

pattern.Split(New Char() {" "c, "|"c}, _
StringSplitOptions.RemoveEmptyEntries)

Then just fill your labels with the array of strings.
 
A

Armin Zingler

ECCO said:
SITUATION:

I have a string with the following pattern:

" [#######] XXXXX | XXXXXXXXXXXX | XXXXXXXXX "

Where # is a number, six characters in length always and X is text of
variable length.

EXAMPLES
#1) " [384922] Shade Stick | Maxwell House | Fine "
#2) " [999201] It something that is green | Re | Do So Me "

OBJECTIVE

I need to separate these sections and assign them to labels (Using
Example #1 from Above):

Label1.text = "384922"
Label2.text = "Shade Stick"
Label3.text = "Maxwell House"
Label4.text = "Fine"

Public Class Main
Shared Sub Main()
Dim s = " [384922] Shade Stick | Maxwell House | Fine "
Dim items = Split(s)

Array.ForEach(items, New Action(Of String)(AddressOf Debug.Print))

End Sub
Shared Function Split(ByVal s As String) As String()

Dim Result(3) As String
Dim Items = s.Split("|"c)

Result(0) = Items(0).Substring(2, 6)
Result(1) = Items(0).Substring(10).TrimEnd
Result(2) = Items(1).Trim
Result(3) = Items(2).Trim

Return Result

End Function
End Class
 
S

Scott M.

Or,

Dim stringParts() As String = theString.Split(New Char() {"["c, "]"c, " "c,
"|"c}, StringSplitOptions.RemoveEmptyEntries)

and then use Trim() on the string array parts.

-Scott

Armin Zingler said:
ECCO said:
SITUATION:

I have a string with the following pattern:

" [#######] XXXXX | XXXXXXXXXXXX | XXXXXXXXX "

Where # is a number, six characters in length always and X is text of
variable length.

EXAMPLES
#1) " [384922] Shade Stick | Maxwell House | Fine "
#2) " [999201] It something that is green | Re | Do So Me "

OBJECTIVE

I need to separate these sections and assign them to labels (Using
Example #1 from Above):

Label1.text = "384922"
Label2.text = "Shade Stick"
Label3.text = "Maxwell House"
Label4.text = "Fine"

Public Class Main
Shared Sub Main()
Dim s = " [384922] Shade Stick | Maxwell House | Fine "
Dim items = Split(s)

Array.ForEach(items, New Action(Of String)(AddressOf Debug.Print))

End Sub
Shared Function Split(ByVal s As String) As String()

Dim Result(3) As String
Dim Items = s.Split("|"c)

Result(0) = Items(0).Substring(2, 6)
Result(1) = Items(0).Substring(10).TrimEnd
Result(2) = Items(1).Trim
Result(3) = Items(2).Trim

Return Result

End Function
End Class
 
G

Göran Andersson

Scott said:
Or,

Dim stringParts() As String = theString.Split(New Char() {"["c, "]"c, " "c,
"|"c}, StringSplitOptions.RemoveEmptyEntries)

and then use Trim() on the string array parts.

-Scott

Still doesn't split the string correctly...
 
S

Scott M.

Does when I try it.

-Scott


Göran Andersson said:
Scott said:
Or,

Dim stringParts() As String = theString.Split(New Char() {"["c, "]"c, "
"c, "|"c}, StringSplitOptions.RemoveEmptyEntries)

and then use Trim() on the string array parts.

-Scott

Still doesn't split the string correctly...
 
S

Scott M.

Oops, posted the older version.

This does the trick:

Dim stringParts() As String = theString.Split(New Char() {"["c, "]"c, "|"c},
StringSplitOptions.RemoveEmptyEntries)
lblPart1 = stringPart(0).Trim
lblPart2 = stringPart(1).Trim
lblPart3 = stringPart(2).Trim
lblPart4 = stringPart(3).Trim

-Scott



Göran Andersson said:
Scott said:
Or,

Dim stringParts() As String = theString.Split(New Char() {"["c, "]"c, "
"c, "|"c}, StringSplitOptions.RemoveEmptyEntries)

and then use Trim() on the string array parts.

-Scott

Still doesn't split the string correctly...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top