Q: Separating a string into parts?

  • Thread starter Thread starter ECCO
  • Start date Start date
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"
 
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.
 
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.
 
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!
 
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
 
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...
 
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.
 
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
 
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
 
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...
 
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...
 
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...
 
Back
Top