I need to take a string and split it into an array based on the type of charcter not matching they proceeding it.
So if you have “asd fds 1.4#3” this would split into array as follows
stringArray[0] = "asd";
stringArray[1] = " ";
stringArray[2] = "fds";
stringArray[3] = " ";
stringArray[4] = "1";
stringArray[5] = ".";
stringArray[6] = "4";
stringArray[7] = "#";
stringArray[8] = "3";
Any recomendations on the best way to acheive this? Of course I could create a loop based on .ToCharArray() but was looking for a better way to achieve this.
Thank you
Using a combination of Regular Expressions and link you can do the following.
Add additional capture groups to capture other differences. The last capture
(.+?)is a non greedy everything else. So every item in this capture will be considered different (including the same item twice)Update – new revision of regex
This now uses non capturing groups so that
\1can be used in the final capture. This means that the same character will be grouped if its in then catch all group.e.g. before the string “asd fsd” would create 4 strings (each space would be considered different) now the result is 3 strings as 2 adjacent spaces are combined