ISSUE :
 
Profile photo
Submitted by Elizabeth Davis
on September 2nd, 2011 at 10:54:07 EST

Split alphabet character and number from alphanumeric string C#

I have a alphanumeric string which starts with Alphabet and ends with number. I need to separate/split this string into alphabet and number. I will always have my string starting with alphabet and ending with number. There is not mix of alphabet and number.
For example: CODE1234, MD2233, CA33 etc. I need to split to "CODE" and "1234". I need to separate starting alphabet part and number part. First I thought it will be easy to split this pattern of string but now I am stuck. Please help.


Profile photo
Replied by RaKumar
on September 2nd, 2011 at 15:15:39 EST
You can use Regex (regular expression) to achieve this. First user regular expression to find the first position of numeric part in your string and then user String.Substring() method to split the string.

For example:

string myString = "CODE1234";
string alphabet = String.Empty;
string digit = String.Empty;

Match regexMatch = Regex.Match(myString, "\\d");
if (regexMatch.Success) //Found numeric part in the coverage string
{
int digitStartIndex = regexMatch.Index; //Get the index where the numeric digit found
alphabet = myString.Substring(0, digitStartIndex);
digit = myString.Substring(digitStartIndex);
}
Accepted Answer
Accepted
 Answer

Profile photo
Replied by Elizabeth Davis
on September 8th, 2011 at 10:09:14 EST
Perfect solution !

Profile photo
Replied by Elizabeth Davis
on September 8th, 2011 at 10:09:23 EST
Thanks.

Profile photo
on April 11th, 2012 at 01:45:24 EST
Nice Code

Profile photo
Replied by Rupesh Randhi
on October 25th, 2012 at 06:54:10 EST
It was nice one but please write code for string in the format of code1223abc78234axd

Profile photo
Replied by RaKumar
on October 25th, 2012 at 10:48:03 EST
if you want more flexible splitting solution for string like "code1223abc78234axd", you can do something like the code snippet I provided below. But it is just one approach. You could implement in various ways.


string myString = "code1223abc78234axd";

List<string> digits = new List<string>();
List<string> alphabets = new List<string>();

Match regexMatch = null;

while (myString.Length>0)
{
if ((regexMatch = Regex.Match(myString, "\\d")).Success) //if there are digits
{
if (regexMatch.Index > 0) //if myString is not starting with digit
{
alphabets.Add(myString.Substring(0, regexMatch.Index));
}
else if ((regexMatch = Regex.Match(myString, "\\D")).Success) //if myString is starting with digits but has subsequent alphabets
{
digits.Add(myString.Substring(0, regexMatch.Index));
}
else // if myString only has digits, no more alphabets
{
digits.Add(myString.Substring(0));
break; //no more alphabets
}
myString = myString.Substring(regexMatch.Index);
}
else //there are no digits in myString
{
alphabets.Add(myString);
break; //no more digits
}

}

Profile photo
Replied by Tharun Nani
on December 19th, 2013 at 09:58:39 EST
Can u explain it in "C"....