I am trying to split a string in C# the following way:
Incoming string is in the form
string str = "[message details in here][another message here]/n/n[anothermessage here]"
And I am trying to split it into an array of strings in the form
string[0] = "[message details in here]"
string[1] = "[another message here]"
string[2] = "[anothermessage here]"
I was trying to do it in a way such as this
string[] split = Regex.Split(str, @"\[[^[]+\]");
But it does not work correctly this way, I am just getting an empty array or strings
Any help would be appreciated!
Split is used to break a delimited string into substrings. You can use either a character array or a string array to specify zero or more delimiting characters or strings. If no delimiting characters are specified, the string is split at white-space characters.
String splitting is the process of breaking up a text string in a systematic way so that the individual parts of the text can be processed. For example, a timestamp could be broken up into hours, minutes, and seconds so that those values can be used in the numeric analysis.
Apr 07, 2007. The C function strtok() is a string tokenization function that takes two arguments: an initial string to be parsed and a const -qualified character delimiter. It returns a pointer to the first character of a token or to a null pointer if there is no token.
Use the Regex.Matches
method instead:
string[] result =
Regex.Matches(str, @"\[.*?\]").Cast<Match>().Select(m => m.Value).ToArray();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With