Saturday, December 29, 2007

C#: Get an array of integeres from a delimited string

public int[] GetIntArrayFromDelimitedString(string str, char delimiter)
{
int[] response = new int[str.Split(delimiter).Length];
for (int i = 0; i < response.Length; i++)
response[i] = Convert.ToInt32(str.Split(delimiter)[i]);
return response;
}

2 comments:

  1. Great stuff! Thanks...

    ReplyDelete
  2. Optimized Code can be :
    public int[] GetIntArrayFromDelimitedString(string str, char delimiter)
    {
    int[] response = new int[str.Split(delimiter).Length];
    string[] arr = str.Split(delimiter);

    for (int i = 0; i < response.Length; i++)
    {
    response[i] = Int32.Parse(arr[i]);
    }
    return response;
    }

    ReplyDelete

Please use your common sense before making a comment, and I truly appreciate your constructive criticisms.