0

I've a string message that could be like

"TRX00  abcd 20150921    " 

or like (n) times that string, I.e.

"TRX00  abcd 20150921    TRX00  abcd 20150921    TRX00  abcd 20150921    " 

etc..

How can I split that string in (n) strings using "TRX" at the beginning of each substring like a delimiter?

I want to take the delimiter "TRX" in the substring.

I've done the following but it's doesn't work well..

string msg = "TRX00  abcd 20150921    TRX00  abcd 20150921    TRX00  abcd 20150921    ";

string[] _msg;
string pattern = "(TRX)";
string msg2 = "";

if(msg.Contains("TRX"))
{
    _msg = Regex.Split(msg, pattern);

    foreach (string ok in _msg)
    {
        msg2 = ok;
        MessageBox.Show(msg2);
    }
}

The expected result should be an array of substrings like:

{ "TRX00 abcd 20150921 ", "TRX00 abcd 20150921 ", etc.. } 
5
  • 2
    What is the expected result at all? Commented Sep 30, 2015 at 15:13
  • the expected result should be an array of substrings like: { "TRX00 abcd 20150921 ", "TRX00 abcd 20150921 ", etc.. } Commented Sep 30, 2015 at 15:15
  • trim then split on the 4 whitespaces " " that delimit the groups. Commented Sep 30, 2015 at 15:16
  • Why doesn't it work well? Commented Sep 30, 2015 at 15:17
  • the number of chars inside each substring may be different each time.. the only part that is the same is the delimiter "TRX" Commented Sep 30, 2015 at 15:18

3 Answers 3

3

You can use this pattern in your code instead of (TRX):

(?!^)(?=TRX)

It will split on TRX as long as it isn't at the start of the string and returns:

TRX00 abcd 20150921

TRX00 abcd 20150921

TRX00 abcd 20150921

Sign up to request clarification or add additional context in comments.

1 Comment

This regex pattern works like a charm! Thank you @dustmouse
0

Apparently, the desired strings are 19 characters long; you could truncate the beginning as long as the overall length of the input is larger as follows; however this solution is not very robust to a change of the input format, which makes it questionable.

List<String> Messages = new List<String>();
while (msg.length > 19)
{
    var Message = msg.Substring(19);
    Messages.Add(Message);
    msg = msg.Substring(20, msg.Length-1);
}

2 Comments

I count a length of 20, not 19.
the lenght can be different each time, the only part that is the same is the delimiter "TRX"
0

If you do actually treat "TRX" like a delimiter you can split the string using that, something like:

string msg = "TRX00  abcd 20150921    TRX00  abcd 20150921    TRX00  abcd 20150921    ";
string[] strings = msg.Split(new[] { "TRX" }, StringSplitOptions.None)
                      .Select(x => "TRX" + x)
                      .Skip(1)
                      .ToArray();

You can then transform that delimited collection and add the "TRX" back on. Note I skip the first element (Skip(1)) as in this example that would just be am empty string.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.