Convert a Comma-delimited String into Array of Integers

Convert comma separated string of ints to int array

You should use a foreach loop, like this:

public static IEnumerable<int> StringToIntList(string str) {
if (String.IsNullOrEmpty(str))
yield break;

foreach(var s in str.Split(',')) {
int num;
if (int.TryParse(s, out num))
yield return num;
}
}

Note that like your original post, this will ignore numbers that couldn't be parsed.

If you want to throw an exception if a number couldn't be parsed, you can do it much more simply using LINQ:

return (str ?? "").Split(',').Select<string, int>(int.Parse);

How to convert a string of comma separated numbers to integers?

First split the values with comma using the split() method like this.

arr.split(',')

After that you will get each value separated in an array which will be accessible by giving array index. So arr[0] will include 108 and arr[1] will include 109. Now all that's left to do is parse them individually.

parseInt(arr[0]) 
parseInt(arr[1])

How can I convert a comma delimited string to a list of integers?

Something like this

var results = yearsString.Split(',').Select(x => int.Parse(x.Trim()));

Note : There is no fault tolerance in this approach, if they cant be converted to int this will blow up with an exception

And just for fun here is a an approach using pointers unsafe and fixed

fixed (char* pInput = Input)
{
var len = pInput + Input.Length;
var current = 0;
var results = new List<int>();

for (var p = pInput; p < len; p++)
{
if (*p >= 48 & *p <= 58)
current = current * 10 + *p - 48;
else if (*p == ',')
{
results.Add(current);
current = 0;
}
}

results.Add(current);
return results;
}

Benchmarks

Just because i was bored

----------------------------------------------------------------------------
Mode : Release (64Bit)
Test Framework : .NET Framework 4.7.1 (CLR 4.0.30319.42000)
----------------------------------------------------------------------------
Operating System : Microsoft Windows 10 Pro
Version : 10.0.17134
----------------------------------------------------------------------------
CPU Name : Intel(R) Core(TM) i7-2600 CPU @ 3.40GHz
Description : Intel64 Family 6 Model 42 Stepping 7
Cores (Threads) : 4 (8) : Architecture : x64
Clock Speed : 3401 MHz : Bus Speed : 100 MHz
L2Cache : 1 MB : L3Cache : 8 MB
----------------------------------------------------------------------------

Test 1

--- Standard input --------------------------------------------------------
| Value | Average | Fastest | Cycles | Garbage | Test | Gain |
--- Scale 100 ---------------------------------------------- Time 0.003 ---
| Unsafe | 2.304 µs | 2.101 µs | 9.298 K | 0.000 B | Pass | 90.28 % |
| Linq | 23.711 µs | 22.214 µs | 82.700 K | 0.000 B | Base | 0.00 % |
--- Scale 1,000 -------------------------------------------- Time 0.046 ---
| Unsafe | 21.697 µs | 21.013 µs | 75.490 K | 0.000 B | Pass | 90.41 % |
| Linq | 226.218 µs | 205.332 µs | 768.447 K | 0.000 B | Base | 0.00 % |
--- Scale 10,000 ------------------------------------------- Time 0.250 ---
| Unsafe | 214.526 µs | 200.829 µs | 733.557 K | 0.000 B | Pass | 89.93 % |
| Linq | 2.130 ms | 1.996 ms | 7.257 M | 0.000 B | Base | 0.00 % |
--- Scale 100,000 ------------------------------------------ Time 2.906 ---
| Unsafe | 2.303 ms | 2.063 ms | 7.680 M | 0.000 B | Pass | 90.99 % |
| Linq | 25.571 ms | 22.624 ms | 84.808 M | 0.000 B | Base | 0.00 % |
--- Scale 1,000,000 --------------------------------------- Time 36.594 ---
| Unsafe | 23.061 ms | 21.910 ms | 78.356 M | 0.000 B | Pass | 93.07 % |
| Linq | 332.639 ms | 274.595 ms | 1.055 B | 0.000 B | Base | 0.00 % |
---------------------------------------------------------------------------

Convert comma separated list of integers into an array

You want one line? Use LINQ:

int[] cols = sizes.Split(',').Select(x => int.Parse(x)).ToArray();

Add using System.Linq; at the top of the file to make it work.

Without LINQ you'd need a loop:

var source = sizes.Split(',');
var cols = new int[source.Length];
for(int i = 0; i < source.Length; i++)
{
cols[i] = int.Parse(source[i]);
}

Convert comma delimited string to int array

Not sure I understand, but if you mean you want to convert them back to a comma-separated string, you could do something like this:

string nums = string.Join(",", Array.ConvertAll(s.Split(','), int.Parse));

String with Comma separated numbers to array of integer in javascript

I would recommend this:

var array;
if (string.length === 0) {
array = new Array();
} else {
array = string.replace(/, +/g, ",").split(",").map(Number);
}

Converting comma-separated string of single-quoted numbers to int array

  $arr = explode (",", str_replace("'", "", $str));
foreach ($arr as $elem)
$array[] = trim($elem) ;


Related Topics



Leave a reply



Submit