none
How to split string to array of 2 char

    Question

  •  

    Hi all,

     

    I am wondering if there is any efficient way to split a string into array of 2 char.

     

    Take for example :

     

    string strA = "1234567890";

     

    string[] split = new string[5];

     

    I would like to split it to

     

    split[0] = "12" 

    split[1] = "34"

     

    ....

     

    Can someone please help?

     

    Thanks

    Friday, November 30, 2007 3:01 AM

Answers

  • Hi,

     

    There are few options..

     

    1) Use regular expression such as \d{2} and it will give 2 integers from string, so it break string and give you desire result..its bit hard I guess to do, but fastest with large string ..need to test on speed

     

    2) Simply, use Mid (substring) function in loop .. seems more idiotic to me Smile, but maybe best method

     

    3) Copy string a Char Array, and then read them in string array through loop Smile.

     

    4) Read string char by char, copy to another string, with a condition/statement that put splitter character (a marker) after every two char read, and then split string..

     

    wow quite a lot to experiment i guess ?

     

     

    Friday, November 30, 2007 6:51 AM
  • Here it is:

    string strA = "1234567890";

     

    string[] split = new string[strA.Length/2 + (strA.Length%2 == 0 ? 0 : 1)];

     

    for (int i = 0; i < split.Length; i++)

    {

        split[ i ] = strA.Substring(i * 2, i * 2 + 2 > strA.Length ? 1 : 2);

    }

    Friday, November 30, 2007 9:26 AM
  • LINQ solution #1:

    Code Block

    int i = 0;

    string input = "1234567890";

     

    string[] split =

    (from char c1 in input.Substring(i++, input.Length - i)

     from char c2 in input.Substring(i++, 1)

     where i % 2 == 0

     select String.Format("{0}{1}", c1, c2)).ToArray();

     

     

     

    LINQ solution #2:

    Code Block

    string[] split =

    (from Match m in Regex.Matches(input, "..")

     select m.Value).ToArray();

     

     

    --

    Regards,

    Daniel Kuppitz

     

    Friday, November 30, 2007 12:37 PM

All replies

  • Hello,
     you can use stringbuilder class, copy function .
     
    cheers.
    Friday, November 30, 2007 3:12 AM
  • Hi,

     

    There are few options..

     

    1) Use regular expression such as \d{2} and it will give 2 integers from string, so it break string and give you desire result..its bit hard I guess to do, but fastest with large string ..need to test on speed

     

    2) Simply, use Mid (substring) function in loop .. seems more idiotic to me Smile, but maybe best method

     

    3) Copy string a Char Array, and then read them in string array through loop Smile.

     

    4) Read string char by char, copy to another string, with a condition/statement that put splitter character (a marker) after every two char read, and then split string..

     

    wow quite a lot to experiment i guess ?

     

     

    Friday, November 30, 2007 6:51 AM
  • Here it is:

    string strA = "1234567890";

     

    string[] split = new string[strA.Length/2 + (strA.Length%2 == 0 ? 0 : 1)];

     

    for (int i = 0; i < split.Length; i++)

    {

        split[ i ] = strA.Substring(i * 2, i * 2 + 2 > strA.Length ? 1 : 2);

    }

    Friday, November 30, 2007 9:26 AM
  •  

    I hope I don't come across as a code facist, but at least add 2 to i in the loop:

     

    string strA = "1234567890";

     

    string[] split = new string[strA.Length/2 + (strA.Length%2 == 0 ? 0 : 1)];

     

    for (int i = 0; i < split.Length; i= i+2)

    {

        split[ i ] = strA.Substring(i, i + 2 > strA.Length ? 1 : 2);

    }

    Friday, November 30, 2007 10:58 AM
  • It's a possible option, but you need to change it fully to work, this way it wont work.

     

    Friday, November 30, 2007 12:09 PM
  • LINQ solution #1:

    Code Block

    int i = 0;

    string input = "1234567890";

     

    string[] split =

    (from char c1 in input.Substring(i++, input.Length - i)

     from char c2 in input.Substring(i++, 1)

     where i % 2 == 0

     select String.Format("{0}{1}", c1, c2)).ToArray();

     

     

     

    LINQ solution #2:

    Code Block

    string[] split =

    (from Match m in Regex.Matches(input, "..")

     select m.Value).ToArray();

     

     

    --

    Regards,

    Daniel Kuppitz

     

    Friday, November 30, 2007 12:37 PM