How to split the string that have the 3 character separator

Eekhay 101 Reputation points
2023-10-07T08:20:26.06+00:00

Dear All,

I was trying to split the string that contains the separators that have 3 characters. The current code uses substring, but if one or more strings have increased or decreased the number of characters will cause the result to not same as expected. Is there a way to split the string but not change the result regardless of whether the string length has changed?

String: (L)AAA ARX4 NN4-320088-RZZZ-00(S)01CE06233100041887(P)BBCCD881101606

tbxNumber1.Text = Curr_Number.Substring(54, 14)
tbxNumber2.Text = Curr_Number.Substring(3, 27)
tbxNumber3.Text = Curr_Number.Substring(33, 18)

VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,821 questions
0 comments No comments
{count} votes

Accepted answer
  1. Viorel 121.3K Reputation points
    2023-10-07T08:42:08.1+00:00

    Check an example:

    Dim Curr_Number = "(L)AAA ARX4 NN4-320088-RZZZ-00(S)01CE06233100041887(P)BBCCD881101606"
    
    tbxNumber1.Text = Regex.Match(Curr_Number, "(?<=\(P\)).*?(?=\(|$)").Value
    tbxNumber2.Text = Regex.Match(Curr_Number, "(?<=\(L\)).*?(?=\(|$)").Value
    tbxNumber3.Text = Regex.Match(Curr_Number, "(?<=\(S\)).*?(?=\(|$)").Value
    
    2 people found this answer helpful.

2 additional answers

Sort by: Most helpful
  1. WayneAKing 4,926 Reputation points
    2023-10-08T07:01:32.0366667+00:00

    There are some unexplained details of your task which need to be clarified before a robust solution can be suggested.

    (1) Are the separator strings always the same?

    (L) (S) (P)

    Or are they sometimes different?

    (2) Are the three separator strings always present in the string being parsed?

    Or may there only be one or two? Or more than three?

    (3) Do the separator characters always occur in the same order?

    Or can they occur in any arbitrary order?

    If you are not comfortable with the intricacies and syntax of Regex expressions, and assuming that there will always be the same three separator strings, and that they will always occur in the same order, then you can use the IndexOf method as in the following code example.

    Dim xP As Integer = Curr_Number.IndexOf("(P)")
    Dim xL As Integer = Curr_Number.IndexOf("(L)")
    Dim xS As Integer = Curr_Number.IndexOf("(S)")
    
    tbxNumber1.Text = Curr_Number.Substring(xP + 3)
    tbxNumber2.Text = Curr_Number.Substring(xL + 3, xS - 3)
    tbxNumber3.Text = Curr_Number.Substring(xS + 3, xP - 3 - xS)
    
    
    1 person found this answer helpful.

  2. WayneAKing 4,926 Reputation points
    2023-10-08T23:59:14.9033333+00:00

    Also try this:

    Dim seps() = {"(P)", "(L)", "(S)"}
    Dim subs() = Curr_Number.Split(seps, StringSplitOptions.RemoveEmptyEntries)
     
    tbxNumber1.Text = subs(2)
    tbxNumber2.Text = subs(0)
    tbxNumber3.Text = subs(1)
    
    
    1 person found this answer helpful.

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.