E.g. Input string str = "1111222233334444";
return value ArrayList A where A[0] = "1111", A[1] = "2222", etc.
Use of Linq or reg ex would be good.
E.g. Input string str = "1111222233334444";
return value ArrayList A where A[0] = "1111", A[1] = "2222", etc.
Use of Linq or reg ex would be good.
Why not loops? Here's something that would do it quite well:
string str = "111122223333444455";
int chunkSize = 4;
int stringLength = str.Length;
for (int i = 0; i < stringLength ; i += chunkSize)
{
if (i + chunkSize > stringLength) chunkSize = stringLength - i;
Console.WriteLine(str.Substring(i, chunkSize));
}
Console.ReadLine();
I don't know how you'd deal with case where the string is not factor of 4, but not saying you're idea is not possible, just wondering the motivation for it if a simple for loop does it very well? Obviously the above could be cleaned and even put in as an extension method.
Or as mentioned in comments, you know it's /4 then
str = "1111222233334444";
for (int i = 0; i < stringLength; i += chunkSize)
{Console.WriteLine(str.Substring(i, chunkSize));}
static IEnumerable<string> Split(string str, int chunkSize)
{
return Enumerable.Range(0, str.Length / chunkSize).Select(i => str.Substring(i * chunkSize, chunkSize));
}
It's not pretty and it's not fast, but it works, it's a one-liner and it's LINQy:
List<string> a = text.ToCharArray().Select((c, i) => new { Char = c, Index = i }).GroupBy(o => o.Index / 4).Select(g => new String(g.Select(o => o.Char).ToArray())).ToList();
Using regular expressions and Linq:
List<string> groups = (from Match m in Regex.Matches(str, @"\d{4}")
select m.Value).ToList();
I find this to be more readable, but it's just a personal opinion. It can also be a one-liner : ).
In a combination of dove+Konstatin's answers...
static IEnumerable<string> Chunk(string str, int chunkSize) {
for (int i = 0; i < str.Length; i += chunkSize)
yield return str.Substring(i, chunkSize);
}
Or, if you want to support strings that aren't a whole number of chunks...
static IEnumerable<string> Chunk(string str, int chunkSize) {
for (int i = 0; i < str.Length; i += chunkSize)
yield return str.Substring(i, Math.Min(chunkSize, str.Length-i));
}
How's this for a one-liner?
List<string> result = new List<string>(Regex.Split(target, @"(?<=\G.{4})"));
With this regex it doesn't matter if the last chunk is less than four characters, because it only ever looks at the characters behind it. I'm sure this isn't the most efficient solution, but I just had to toss it out there. :D
How is it done if I want all possible combinations instead of chunks of only 4. For example, if I am given an string which is 1111 (no less than length 4) or 255255255255(no more than length 12).
I want to split up these numbers in every possible combination and create all possible IP addresses from them. For example,
for a given string "1902426"
The result should be
"1.90.24.26" "1.90.242.6" "19.0.24.26" "19.0.242.6" "190.2.4.26" "190.2.42.6" "190.24.2.6"
Ofcourse you cannot have anything with a leading "0", like "19.02.42.26"
public static IEnumerable<IEnumerable<T>> SplitEvery<T>(this IEnumerable<T> values, int n)
{
var ls = values.Take(n);
var rs = values.Skip(n);
return ls.Any() ?
Cons(ls, SplitEvery(rs, n)) :
Enumerable.Empty<IEnumerable<T>>();
}
public static IEnumerable<T> Cons<T>(T x, IEnumerable<T> xs)
{
yield return x;
foreach (var xi in xs)
yield return xi;
}