简体   繁体   中英

Convert string to int[,]

I have weird requirement in matrix clustering.

My inputs are like

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

and what I want is

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

so It should be array of int[4,4].

What I tried?

I tried many ways for this conversation.

First Of all I convert my string to string[], using below code.

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

And then I tried to convert my string[] to int[], using below code.

var intArray = resultantArray .Select(int.Parse).ToArray();

but I ended up with removed leading zeros .

Is there any way, I can achieve int[4,4] array from string?

Can you try this.

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

        string[] allStrings = param.Split(',');
        var m = allStrings.Count();

        System.Collections.Generic.List<char[]> listOfCharArr = new System.Collections.Generic.List<char[]>();
        System.Collections.Generic.List<int> lengthList = new System.Collections.Generic.List<int>();
        for(int i = 0; i<m ; i++)
        {
            string str = allStrings[i];
            char[] allChars = str.ToCharArray();
            listOfCharArr.Add(allChars);
            lengthList.Add(allChars.Count());                
        }
        int maxLength = lengthList.Max();

        int[,] matrix = new int[m, maxLength] ;

        for(int i = 0; i<m ; i++)
        {
            for (int j = 0; j < maxLength; j++)
            {
                char[] temp = listOfCharArr[i];
                if (j < listOfCharArr[i].Count())
                    matrix[i, j] = Convert.ToInt32(listOfCharArr[i][j].ToString()); // you can use int.tryparse if not sure that input will always be integer
                else
                    matrix[i, j] = -1; // I have used -1 as no value, you can use whatever you require
            }
        }
            string param = "1100,1110,0110,0001";
            string[] resultantArray = param.Split(',').ToArray();

            int n = resultantArray.Count();
            int m = resultantArray[0].Length;
            var matrix = new int[n,m];

            for (int i = 0; i < n; i++)
            {
                for (int j = 0; j < m; j++)
                {
                    matrix[i, j] = Convert.ToInt32(resultantArray[i][j].ToString());
                }
            }

Here is a more dynamic approach

string param = "1100,1110,0110,0001";
int[][] rJagged = param.Split(',').Select(x => x.Select(y => int.Parse(y.ToString())).ToArray()).ToArray();
int[,] result = new int[rJagged.Length, rJagged.Max(x => x.Length)];

for (int i = 0; i < rJagged.Length; i++)
{
    for (int j = 0; j < rJagged[i].Length; j++)
    {
        result[i, j] = rJagged[i][j];
    }
}

This should be the fastest approach

string param = "1100,1110,0110,0001";            
int[,] result = new int[4,4];
int i = 0, j = 0;

for (int x = 0; x < param.Length; x++)
{
    if (param[x] == ',')
    {
        i++;
        j = 0;
    }
    else
    {
        result[i, j] = param[x] - '0';
        j++;
    }
}

I have modified the answers of Vandita and added some code from the answer of Santosh.

Also I checked for the highest length of resultantArray.

 string param = "1100,1110,0110,0001";
 string[] resultantArray = param.Split(',').ToArray();

  var max = resultantArray.OrderByDescending(s => s.Length).First();

  int n = resultantArray.Length;            
  int m = max.Length;

  var matrix = new int[n, m];

  for (int i = 0; i < resultantArray.Count(); i++)
  {
     string str = resultantArray[i];
     char[] allChars = str.ToCharArray();
     for (int j = 0; j < allChars.Count(); j++)
     {
          matrix[i, j] = Convert.ToInt32(allChars[j].ToString()); 
     }
  }

Still, I am looking for better answers..

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM