convert string[] to int[]

后端 未结 7 877
一个人的身影
一个人的身影 2020-12-18 22:38

Which is the fastest method for convert an string\'s array [\"1\",\"2\",\"3\"] in a int\'s array [1,2,3] in c#?

thanks

相关标签:
7条回答
  • 2020-12-18 23:05

    iterate and convert.

    0 讨论(0)
  • 2020-12-18 23:08
    string[] arr1 = {"1","2","3"};
    int[] arr2 = Array.ConvertAll(arr1, s => int.Parse(s));
    

    The use of Array.ConvertAll ensures (unlike LINQ Select/ToArray) that the array is initialized at the right size. You can possible get a shade quicker by unrolling, but not much:

    int[] arr2 = new int[arr1.Length];
    for(int i = 0 ; i < arr1.Length ; i++) {
        arr2[i] = int.Parse(arr[i]);
    }
    

    If you need something faster still (perhaps bulk file/data handling), then writing your own parse might help; the inbuilt one handles a lot of edge-cases - if your data is simpler you really can cut this down a bit.


    For an example of an alternative parser:

        public static unsafe int ParseBasicInt32(string s)
        {
            int len = s == null ? 0 : s.Length;
            switch(s.Length)
            {
                case 0:
                    throw new ArgumentException("s");
                case 1:
                    {
                        char c0 = s[0];
                        if (c0 < '0' || c0 > '9') throw new ArgumentException("s");
                        return c0 - '0';
                    }
                case 2:
                    {
                        char c0 = s[0], c1 = s[1];
                        if (c0 < '0' || c0 > '9' || c1 < '0' || c1 > '9') throw new ArgumentException("s");
                        return ((c0 - '0') * 10) + (c1 - '0');
                    }
                default:
                    fixed(char* chars = s)
                    {
                        int value = 0;
                        for(int i = 0; i < len ; i++)
                        {
                            char c = chars[i];
                            if (c < '0' || c > '9') throw new ArgumentException("s");
                            value = (value * 10) + (c - '0');
                        }
                        return value;
                    }
            }
        }
    
    0 讨论(0)
  • 2020-12-18 23:19
    var values = new string[] { "1", "2", "3" };
    values.Select(x => Int32.Parse(x)).ToArray();
    
    0 讨论(0)
  • 2020-12-18 23:19

    I'd probably do:

    string[] array = new[] { "1", "2" }; // etc.
    int[] ints = array.Select(x => int.Parse(x)).ToArray();
    

    if I could guarantee that the data would be only integers.

    if not:

    string[] array = new[] { "1", "2" }; // etc.
    List<int> temp = new List<int>();
    foreach (string item in array)
    {
        int parsed;
        if (!int.TryParse(item, out parsed))
        {
             continue;
        }
    
        temp.Add(parsed);
    }
    
    int[] ints = temp.ToArray();
    
    0 讨论(0)
  • 2020-12-18 23:23

    I suggest to iterate and int.TryParse()

    Which one is faster in processing and conversion int.Parse(), int.TryParse(), Convert.Int32()

    See Microsoft comparison of Parse , TryParse and ConvertTo

    0 讨论(0)
  • 2020-12-18 23:29

    The is no fast way I know but you can use a "short way":

    var numbers = new[] {"1", "2", "3"};
    
    var result = numbers.Select(s => int.Parse(s));
    int[] resultAsArray = result.ToArray();
    

    And if you use PLink you get to compute the values in parallel.

    0 讨论(0)
提交回复
热议问题