13

Input:

string param = "1100,1110,0110,0001";

Output:

int[] matrix = new[]
    {
              1,1,0,0,
              1,1,1,0,
              0,1,1,0,
              0,0,0,1
    };

What I did?

First of all I splited string to string[].

string[] resultantArray = param.Split(',');

Created one method, where I passed my string[].

var intArray = toIntArray(resultantArray);

static private int[] toIntArray(string[] strArray)
{        
    int[] intArray = new int[strArray.Length];
    for (int i = 0; i < strArray.Length; i++)
    {
        intArray[i] = int.Parse(strArray[i]);
    }

    return intArray;
}

Issue?

I tried many solutions of SO, but none of them helped me.

Ended up with array without leading zeroes.

Bharat
  • 5,869
  • 4
  • 38
  • 58
  • You want a 1d array with 4x4=16 elements, right? – Willem Van Onsem Mar 31 '17 at 13:40
  • yes, that's it. but with leading zeros. – Bharat Mar 31 '17 at 13:41
  • 3
    but in your code you construct a 1d array with only **four** elements. – Willem Van Onsem Mar 31 '17 at 13:41
  • updated my question. – Bharat Mar 31 '17 at 13:46
  • 2
    Possible duplicate of [C# convert int to string with padding zeros?](http://stackoverflow.com/questions/4325267/c-sharp-convert-int-to-string-with-padding-zeros) – logixologist Mar 31 '17 at 13:52
  • I'm a little confused here. How can you tell if an `int` has leading zeros or not? Wouldn't the value `1` always have leading zeros? can you make an `int` with value `1` that doesn't have leading zeros? I don't understand what you are asking. – jmarkmurphy Mar 31 '17 at 14:28
  • 1
    You're splitting on the comma, which gives you four strings with "1"s and "0"s. For your function to work as it is, you want an array with sixteen strings of "1"s or "0"s: `resultantArray = string.Join("", param.Split(',')).Select(x => x.ToString()).ToArray()` – BurnsBA Mar 31 '17 at 18:52

5 Answers5

19
  • determine all digits: .Where(char.IsDigit)
  • convert the selected char-digits into integer: .Select(x => x-'0') (this is not as pretty as int.Parse or Convert.ToInt32 but it's super fast)

Code:

string param = "1100,1110,0110,0001";
int[] result = param.Where(char.IsDigit).Select(x => x-'0').ToArray();

As CodesInChaos commented, this could lead to an error if there are other type of Digits within your input like e.g. Thai digit characters: '๐' '๑' '๒' '๓' '๔' '๕' '๖' '๗' '๘' '๙' where char.IsDigit == true - if you need to handle such special cases you can allow only 0 and 1 in your result .Where("01".Contains)

fubo
  • 44,811
  • 17
  • 103
  • 137
5

You could also remove the commas and convert the result character-wise as follows using Linq.

string param = "1100,1110,0110,0001";
int[] result = param.Replace(",", "").Select(c => (int)Char.GetNumericValue(c)).ToArray();
Codor
  • 17,447
  • 9
  • 29
  • 56
3

yet another way to do this

static private IEnumerable<int> toIntArray(string[] strArray)
{
    foreach (string str in strArray)
    {
        foreach (char c in str)
        {
            yield return (int)char.GetNumericValue(c);
        }
    }
}
Innat3
  • 3,561
  • 2
  • 11
  • 29
2

What about this?

        string input = "1100,1110,0110,0001";

        var result = input
            .Split(',')
            .Select(e => e.ToCharArray()
                .Select(f => int.Parse(f.ToString())).ToArray())
            .ToArray();
Anemoia
  • 7,928
  • 7
  • 46
  • 71
1
string[] s=param.split(',');
Char[] c;
foreach(string i in s){
 c+=i.ToCharArray()
}
int[] myintarray;
int j=0;
foreach(char i in c){
myintarray[j]=(int)i;
j++
}
A Farmanbar
  • 4,381
  • 5
  • 24
  • 42