0

I am working with ASP.net. In that in one sql query my output is 21,22,23, which is a string. I want to remove those commas and store them as separate integer values...I want to use an array. Plese help. How to do that ?

3 Answers 3

2

You can convert a string separated by certain characters by using .Split(char):

string test = "21,22,23";
string[] split = test.Split(',');

This will give you an array of strings though. If you want to use them as integers you will want to convert them as well, and depending on your situation you might want to check if it's parseable or not, but you could use LINQ and do something like this:

string test = "21,22,23";
int[] values = test.Split(',').Select(value => Convert.ToInt32(value)).ToArray();
Sign up to request clarification or add additional context in comments.

Comments

0

the String.Split(char[]) function should do this for you. I think in ASP.net it goes:

string values = "21,22,23";
string[] valuesArray = values.split(",");

Comments

0

While a normal String.Split would work, you still won't get integer values. You can try a simple LINQ query like so:

var results = from string s in yourString.Split('s')
              select int.Parse(s);

You can then obviously cast it to a list or array, depending on your needs... A sample for converting it to an array directly in the LINQ query is as follows:

int[] results = (from string s in yourString.Split('s')
                 select int.Parse(s)).ToArray();

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.