1

I have a string array of the form [a b, a b, a b,...] and I want to create a new array of just the b values. I have tried a .split(" ") but it keeps coming up with an index outside the bounds of the array error.

for (int sequence = 0; sequence < FileIndex.Length; sequence++)
                    {
                        string[] SplitIndex = FileIndex[sequence].Split(' ');
                        sequence++;
                        WriteLine(SplitIndex[sequence]);
                     }
1
  • 1
    sequence++ inside The for loop is not needed. That will increase the value of sequence twice on each iteration Commented Oct 15, 2019 at 0:41

3 Answers 3

0
for (int sequence = 0; sequence < FileIndex.Length; sequence++)
{
    string[] SplitIndex = FileIndex[sequence].Split(' ');
    sequence++;
    WriteLine(SplitIndex[1]);
}

Once you split, in the SplitIndex you will have 2 elements: "a" and "b". If you want to access "b" you will have to use SplitIndex[1]. You are currently using SplitIndex[sequence]

Also, you have sequence++ twice. One in the for statement, and one inside the loop. You should remove it from the loop.

Sign up to request clarification or add additional context in comments.

3 Comments

This will not work because of the extra sequence++ inside for.
This works fine but it produces 8 leading spaces which I can't get rid of so that I can run a comparison against another variable
@tymtam I have already edited for the sequence++ dublication before you added your comment. CallumMcNeilage I dont know why you have this problem. Perhaps you should tell me what FileIndex[sequence] shows and if it has more than 1 space. You can always use TrimStart() to remove the spaces
0

You could also use LINQ here:

using System.Linq;

...

var stringArray = new string[] { "a b", "a b", "a b" };

var result = stringArray
    .Select(str => str.Split(' ').Last())
    .ToArray();

Console.WriteLine("{ " + string.Join(", ", result) + " }");
// { b, b, b }

Explanation:

  • Select every element in the array with Enumerable.Select()
  • Split each string with String.Split() and take the last element with Enumerable.Last(). You could also just index the second element with [1] here as well.
  • Convert the result to an array with List.ToArray(). Optionally you could just leave the result as an IEnumerable<string> as well.

Comments

0

for way

  1. Remove sequence++;
  2. Change WriteLine(SplitIndex[sqequence]); to WriteLine(SplitIndex[1]);
for (int sequence = 0; sequence < FileIndex.Length; sequence++)
{
  string[] SplitIndex = FileIndex[sequence].Split(' ');
  //sequence++;
  WriteLine(SplitIndex[1]);
}

Linq way

//Note: no error handling
var bees = FileIndex.Select(fi => fi.Split(' ')[1]);

You can add error handling

var bees = test.Select(fi => fi.Split(' ', StringSplitOptions.RemoveEmptyEntries).Skip(1).FirstOrDefault() ?? "'No b!'" );

Test

var test = new string[] { "a a", "a b", "a c", "a ", "" };

var bees = test.Select(fi => fi.Split(' ', StringSplitOptions.RemoveEmptyEntries).Skip(1).FirstOrDefault() ?? "'No b!'" );

Console.WriteLine(string.Join(" ", bees));
a b c 'No b!' 'No b!'

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.