1

I have string contains three numbers which seperated by comma or by space.

For example: "3,3,3" or "3 3 3".

The string can contains also spaces between the numbers or at the start\end of the string.

I want to insert them into array.

I did:

this.ang[0] = Convert.ToDouble(ang.Trim().Split(new char[] { ',', ' ' }, StringSplitOptions.RemoveEmptyEntries)[0]);
this.ang[1] = Convert.ToDouble(ang.Trim().Split(new char[] { ',', ' ' }, StringSplitOptions.RemoveEmptyEntries)[1]);
this.ang[2] = Convert.ToDouble(ang.Trim().Split(new char[] { ',', ' ' }, StringSplitOptions.RemoveEmptyEntries)[2]);

How can I insert the data into the array with less code lines?

Thanks!

4
  • Move your conversion logic to a method and use a for or foreach loop to populate the array. Commented May 26, 2016 at 13:49
  • @Sylverac Any way doing it without loop? Commented May 26, 2016 at 13:52
  • Thanks @JohnnyMopp this solve my issue Commented May 26, 2016 at 13:54
  • @Programmer Yeah just do each assignment manually. i.e. ang[0] = MyConversionMethod(inputString);, ang[1] = MyConversionMethod(inputString);, etc Commented May 26, 2016 at 13:55

4 Answers 4

1

The String.Split() method already returns an array, so you could perform your split and use LINQ's Select() method to to parse each value as a double and store these values in an array :

// This will store the double value for each of your elements into an array
this.ang = ang.Trim().Split(new char[] { ',', ' ' }, StringSplitOptions.RemoveEmptyEntries)
                     .Select(x => Convert.ToDouble(x))
                     .ToArray();

If you can't explicitly set your this.ang array, then you could store the previous result in a variable and use it to set the individual values as necessary.

Example

You can see a working example of this in action here.

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

Comments

1

You try using Linq:

 String ang = "3,3,3"; 

 double[] result = ang
   .Split(new char[] { ',', ' ' }, StringSplitOptions.RemoveEmptyEntries)
   .Select(item => Convert.ToDouble(item))
   .ToArray();

Comments

0

The already suggested answers suppose that you can re-assign the entire array ang. If you can't, you have to pass through a for iteration:

var splitString = ang
    .Trim()
    .Split(new char[] { ',', ' ' }, StringSplitOptions.RemoveEmptyEntries)

for(int i = 0; i < 3; i++)
{
    this.ang[i] = Convert.ToDouble(splitString[i]);
}

2 Comments

Magic value 3 in i < 3 looks ugly; i < this.ang.Length?
Sure. Maybe in the future ang will have 5 or 10,000 doubles, so maybe you can generalize it even more making a constant like public const int ANG_LENGHT = 3;
0
var ang = "3,3,3"; // or "3 3 3";
var res = ang.Trim().Split(new char[] { ',', ' ' }, StringSplitOptions.RemoveEmptyEntries)
                    .Select(x => Convert.ToDouble(x))
                    .ToList();

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.