Reputation: 1
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 ?
Upvotes: 0
Views: 179
Reputation: 5043
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();
Upvotes: 0
Reputation: 3194
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();
Upvotes: 2
Reputation: 6872
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(",");
Upvotes: 0