Reputation: 155
A have the following input string:
string input =
"Ta005000000000000000000Tb001700000000000000000Sa005000000000000000000" +
"Sb002500000000000000000F 00000000000000000I 00000000000000000N" +
" 00000000000000000FS 00000000000000000IS 00000000000000000NS" +
" 00000000000000000";
I need to separate this string into parts, however, the content varies greatly.
Have to let this string into a list like:
[0] "Ta005000000000000000000"
[1] "Tb001700000000000000000"
[2] "Sa005000000000000000000"
[3] "Sb002500000000000000000"
[4] "00000000000000000I"
[5] "00000000000000000N"
[6] "0000000000000000FS"
[7] "0000000000000000IS"
[8] "0000000000000000NS"
[9] "000000000000000000"
The only thing that I know in this case is that the max lenght of the string is 23. So, in this example, I need to separate the 'T' or 'S' of the fisrt part of the string return. Or, if you have no occurrence of these characters, separated by space (it happens on the last part of my string return). I did so:
var linq = test.Split(new[] { 'T', 'S', ' ', '{', '}' }, StringSplitOptions.RemoveEmptyEntries).ToList();
My "test" is a StrintBuilder containing those return characters. By doing this, I can separate my list, but I lost a very important information for me in this case, which is the 'T' and 'S'.
Well, do not know if it was clear, but it seems to be something so simple and is giving me a huge headache.
Obs: Other problem is that, for example: "0000000000000000FS", in this part of string I need to maintain the "FS" together.
Thank you for your attention,
Upvotes: 3
Views: 239
Reputation: 17603
Split removes the splitting character. Just replace these as showed below to insert spaces and then split at the space character:
var linq = myRates.Replace("T"," T").Replace("S"," S").Split(new[] { ' ', '{', '}' }, StringSplitOptions.RemoveEmptyEntries).ToList();
EDIT
This rule is very complicated. Maybe this solves your problem.
string input =
"Ta005000000000000000000Tb001700000000000000000Sa005000000000000000000" +
"Sb002500000000000000000F 00000000000000000I 00000000000000000" +
"N 00000000000000000FS 00000000000000000IS 00000000000000000" +
"NS 00000000000000000";
First step: split at ' '
string[] spaceSplit = input.Split(' ', StringSplitOptions.RemoveEmptyEntries);
Now spaceSplit
looks like this:
[0] "Ta005000000000000000000Tb001700000000000000000Sa005000000000000000000Sb002500000000000000000F"
[1] "00000000000000000I"
[2] "00000000000000000N"
[3] "0000000000000000FS"
[4] "0000000000000000IS"
[5] "0000000000000000NS"
[6] "000000000000000000"
Now split each line if it is longer than 23 characters by 'T' and 'S'
List<string> temp = new List<string>();
foreach(string s in spaceSplit)
if (s.Length>23)
temp.AddRange(s.Replace("T", " T").Replace("S", " S").Split(' '));
else
temp.Add(s);
temp.ToArray()
yields
[0] "Ta005000000000000000000"
[1] "Tb001700000000000000000"
[2] "Sa005000000000000000000"
[3] "Sb002500000000000000000F"
[4] "00000000000000000I"
[5] "00000000000000000N"
[6] "0000000000000000FS"
[7] "0000000000000000IS"
[8] "0000000000000000NS"
[9] "000000000000000000"
var linq = (from s in temp select s.Substring(0,23)).ToList();
et voilà, linq
is the array you want. But for other input combinations this "algorithm" might break.
Upvotes: 6
Reputation: 2206
Use a Regular Expression to do this:
string[] parts = Regex.Split(test, @"(?<=[TS\s\{\})");
I'm not sure if the curly braces are formatted correctly.
Upvotes: 1
Reputation: 1911
Something like this?
string[] separatedString = Regex.Split(s, @"(?=[TS ])") ;
then you just have to remove the "empty" elements if you want
Upvotes: 3