- Home /
Split String into groups of characters without using delimiters
The answer on this question by whydoidoit shows how to split a string into individual characters without using any delimiters, using LINQ.
Is it possible to split a string into groups of characters? For example, currently I am creating pairs of characters after splitting them like this :
import System.Linq;
function Foobar()
{
// split load string
var stringSplit : String[] = guiString.Select( function(c) c.ToString() ).ToArray();
var splitLength : int = stringSplit.Length;
// put numbers into pairs, create int array
var mapInfo : int[] = new int[ splitLength / 2 ];
var i : int = 0;
var index : int = 0;
for ( i = 0; i < splitLength; i += 2 )
{
var stringPair : String = stringSplit[ i ] + stringSplit[ i + 1 ];
mapInfo[ index ] = parseInt( stringPair );
index ++;
}
}
Is there a way to split into groups of 2,3,4 etc using LINQ? Thanks.
Answer by perchik · Mar 03, 2014 at 05:00 PM
From this stackoverflow question:
var result = s.Select((x, i) => i)
.Where(i => i % 4 == 0)
.Select(i => s.Substring(i, s.Length - i >= 4 ? 4 : s.Length - i));
Just replace the 4s with whatever size you want.
Thank you for such a quick response. However I really don't understand LINQ (have a hard time reading any of the $$anonymous$$SDN stuff), and am having problems implementing this in uJS.
Test Code :
var stringSplit : String[] = guiString.Select((x, i) => i)
.Where(i => i % 4 == 0)
.Select(i => guiString.Substring(i, guiString.Length - i >= 4 ? 4 : guiString.Length - i));
Errors (7) :
StringSplitTest.js(38,57): BCE0044: expecting ), found ','.
StringSplitTest.js(38,59): BCE0044: expecting ), found 'i'.
StringSplitTest.js(38,60): BCE0043: Unexpected token: ).
StringSplitTest.js(38,65): BCE0043: Unexpected token: i.
2 more Unexpected token for i, 1 more Unexpected token for )
I tried : declaring i as an int first, then in the Select bracket where i is first used, but those didn't work.
it seems to be breaking down after x, not expecting a 2-dimensional declaration (if I understand what is happening correctly). Any suggestions? Thanks again, sorry for the lack of understanding.
Ah, i wasn't paying attention. Forgot that Linq is also on Javascript now. $$anonymous$$y answer is in C#... Let me look at it for a $$anonymous$$ute.
I did write that tutorial on Linq in JS and C# on http://unitygems.com - you need to use functions ins$$anonymous$$d of the lambdas. On iPad so can't type it all but basically
(X,I) => I
Becomes
function(x,I) { return I; }
Clearly without all of the stupid capital letters!
You get the error because Linq methods like Select
or Where
don't return array, but IEnumerable
. In your case, IEnumerable
is returned, and you can't assign it to variable of type String[]
.
To fix this, you either need to return an array, or to change your code to use IEnumerable. In first case, you can just add .ToArray() at the end of your main line of code:
var stringSplit : String[] = guiString.Select( function(x,i) {return i;} )
.Where( function(i) i % 4 == 0 )
.Select( function(i) guiString.Substring(i, guiString.Length - i >= 4 ? 4 : guiString.Length - i) )
.ToArray();
In second case, you can do something like this:
var stringSplit = guiString.Select( function(x,i) {return i;} )
.Where( function(i) i % 4 == 0 )
.Select( function(i) guiString.Substring(i, guiString.Length - i >= 4 ? 4 : guiString.Length - i) );
for (s in stringSplit)
{
Debug.Log(s);
}
Your answer
Follow this Question
Related Questions
LINQ Find String Duplicates in List 1 Answer
String.Split Not Splitting Correctly. Some Duplicates are being deleted. 1 Answer
Grab part of a string by looking for keyword 2 Answers
Error for 1st exactly same name var String 1 Answer
The method Resources.Load does not accept strings after splitting. 0 Answers