Ulemj Sahius
Ulemj Sahius

Reputation: 53

Split a string into an array

I want to split a string to an array of sub-strings. The string is delimited by space, but space may appear inside the sub-strings too. And spliced strings must be of the same length.

Example:

"a b aab bb  aaa" -> "a b", "aab", "bb ", "aaa"

I have the following code:

var T = Regex.Split(S, @"(?<=\G.{4})").Select(x => x.Substring(0, 3));

But I need to parameterize this code, split by various length(3, 4, 5 or n) and I don't know how do this. Please help.

If impossible to parameterize Regex, fully linq version ok.

Upvotes: 2

Views: 155

Answers (3)

Mariano
Mariano

Reputation: 6511

A lookbehind (?<=pattern) matches a zero-length string. To split using spaces as delimiters, the match has to actually return a "" (the space has to be in the main pattern, outside the lookbehind).

Regex for length = 3: @"(?<=\G.{3}) " (note the trailing space)

Code for length n:

var n = 3;
var S = "a b aab bb  aaa";
var regex = @"(?<=\G.{" + n + @"}) ";
var T = Regex.Split(S, regex);

Run this code online

Upvotes: 1

Enigmativity
Enigmativity

Reputation: 117009

It seems rather easy with LINQ:

var source = "a b aab bb  aaa";

var results =
    Enumerable
        .Range(0, source.Length / 4 + 1)
        .Select(n => source.Substring(n * 4, 3))
        .ToList();

Or using Microsoft's Reactive Framework's team's Interactive Extensions (NuGet "Ix-Main") and do this:

var results =
    source
        .Buffer(3, 4)
        .Select(x => new string(x.ToArray()))
        .ToList();

Both give you the output you require.

Upvotes: 1

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726479

You can use the same regex, but "parameterize" it by inserting the desired number into the string.

In C# 6.0, you can do it like this:

var n = 5;
var T = Regex.Split(S, $@"(?<=\G.{{{n}}})").Select(x => x.Substring(0, n-1));

Prior to that you could use string.Format:

var n = 5;
var regex = string.Format(@"(?<=\G.{{{0}}})", n);
var T = Regex.Split(S, regex).Select(x => x.Substring(0, n-1));

Upvotes: 2

Related Questions