-4

I have a following string

 string input = "A123   B12345 777"

The expected result as an array as below,

    A123   B12345 777

But the actual result is

   A123   B12345 

In the below code, the substring is not getting the last value. The size of the last value is just 3 chars.

    SplitString(input, 7)

    private  IEnumerable<string> SplitString(string str, int size)
    {
        return Enumerable.Range(0, str.Length / size)
            .Select(i => str.Substring(i * size, size));
    }

Thank you

Ullan
  • 1,311
  • 8
  • 21
  • 34
  • 2
    I think you need to format your 'expected result' a bit more clearly. To me, that looks exactly the same as the input string and I would guess the function doesn't need to exist! Use `[`, `,` and `]` to show where you expect the split values to start and end. Also show us what number you are passing for the `size` parameter – Andrew Williamson Aug 16 '23 at 17:54
  • I want to split the string in a specific length (7), and also want yo include the last section if the size is less than 7 chars – Ullan Aug 16 '23 at 17:58
  • 1
    Evaluate `str.Length / size` in your debugger. – CodeCaster Aug 16 '23 at 18:00
  • 1
    And find out what happens if your specified length is more than is available from that start position – Hans Kesting Aug 16 '23 at 18:04
  • 2
    "Needs more focus", on the other hand, is perfectly clear. Analysis is required in order to focus the question on the thing that is actually being wondered about. – Karl Knechtel Aug 25 '23 at 20:25

2 Answers2

2

You got your code from Splitting a string into chunks of a certain size, which only works if the input string is an exact multiple in size of size.

Integer division rounds down, so you need to ceil it. Then you possibly try to take substring longer than the remainder of the string. So:

IEnumerable<string> SplitString(string str, int size) => 
    Enumerable.Range(0, (int)Math.Ceiling(str.Length / (float)size))
        .Select(i => str.Substring(i * size, Math.Min(str.Length - (i * size), size)));

Since .NET 6 you can also use .Chunk() as explained here:

var result = str
   .Chunk(size)
   .Select(x => new string(x));
cigien
  • 57,834
  • 11
  • 73
  • 112
CodeCaster
  • 147,647
  • 23
  • 218
  • 272
  • I found another way to do this using the for loop and and also adding Ceiling to match the size – Ullan Aug 16 '23 at 18:17
  • 1
    if str.length is a multiple of the size this answer will throw an error. For example 14 / 7 is 2 but the +1 will result in trying to pull 3 chunks. You need to use Math.Ceiling(str.length / size) or Math.Floor((str.lenght / size) + 0.5) – R.Laney Aug 16 '23 at 18:20
  • 2
    @R.Laney no, it won't throw, but you're right, it'll return an empty string for the last part in that case. Edit: fixed, thanks. – CodeCaster Aug 16 '23 at 18:22
0

LINQ has a handy Chunk method for grouping things like this.

IEnumerable<string> SplitString(string str, int size)
{
    return str.Chunk(size).Select(c => new string(c));
}
string input = "A123   B12345 777";
var result = SplitString(input, 7);
Console.WriteLine(JsonSerializer.Serialize(result));
// Result: ["A123   ","B12345 ","777"]
StriplingWarrior
  • 151,543
  • 27
  • 246
  • 315