I’m using C# 4.0 and have come across a situation where I have to split a whole string by every four words and store it in a List object.
So suppose my string contains: "USD 1.23 1.12 1.42 EUR 0.2 0.3 0.42 JPY 1.2 1.42 1.53", the result should be:
USD 1.23 1.12 1.42
EUR 0.2 0.3 0.42
JPY 1.2 1.42 1.53
It shall be saved into a List object. I have tried the following
List<string> test = new List<string>(data.Split(' ')); //(not working as it splits on every word)
Of course my answer is not as glamour as the linq ones, but I wish to post this
old schoolmethod.EDIT:
If there is no control on the numbers on the input string (for example, if some money has only 1 or 2 values) then there is no way to substring correctly in blocks of 4 the input string.
We can resort to Regex
this pattern accepts also numbers with comma as decimal separator and money symbols without any numbers (“GPB USD 1,23 1,12 1.42 “
RegEx Expression Language – Quick Reference