Click here to Skip to main content
16,012,468 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
string A="1234567890";
how to split string A to get below values??
string b=13579;
string c=24680;
Posted

Why not simply loop the characters? Something like
C#
string a="1234567890";
string b= "";
string c = "";

for (int counter = 0; counter < a.Length; counter++) {
   if (counter % 2 == 0) {
      b += a.ToArray()[counter];
   } else {
      c += a.ToArray()[counter];
   }
}

Of course it would be better to use a stringbuilder and to convert the original string to an array only once and so on, but to get the idea...
 
Share this answer
 
Comments
kiran gowda8 21-Jul-15 3:45am    
thank you Mika Wendelius
Wendelius 21-Jul-15 5:24am    
You're welcome :)
A simple iteration could do the job:
C#
static void mysplit(string input, out string even, out string odd)
{
  even = odd = string.Empty;
  for (int n = 0; n < input.Length; ++n)
  {
    if (n % 2 == 0)
      even += input[n];
    else
      odd += input[n];
  }
}
 
Share this answer
 
Comments
kiran gowda8 21-Jul-15 3:45am    
thank you CPallini
CPallini 21-Jul-15 4:06am    
You are welcome.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900