string help

G

Guest

Hi there,

I have a string containing of a letter and numbers, i.e. "B1234". How can I
saperate the numbers and the letter in two seperate strings?
string1 = B
string2 = 1234

I also have another string which looks like "B323_1234". They shoould be
seperated in to two seperate strings. Like this:
string1 = B323_
string2 = 1234

What is the best way to approach this?
Thank you very much
Chris
 
G

Guest

Got the solution for the second string.

But I still do not knwo how to the first one. Can you just extract the first
character from a string?

Solution for second:

string strDel = "_";
char [] charDel = strDel.ToCharArray();
string [] a_strSplit = strCipidi.Split(charDel);
strFirstHalf = a_strSplit[0];
strSecondHalf = a_strSplit[1];
strFirstHalf += "_";
 
M

Maqsood Ahmed

Hello,
you can achieve it using the Regular Expressions.

System.Text.RegularExpressions.Regex regEx = new
System.Text.RegularExpressions.Regex(@"[\d]+|[\D]+");
string str = "B1234";
System.Text.RegularExpressions.MatchCollection col =
regEx.Matches(str);
string string1 = str.Substring(col[0].Index,col[0].Length);
string string2 = str.Substring(col[1].Index,col[1].Length);
Console.WriteLine(string1);
Console.WriteLine(string2);

HTH :)
Maqsood Ahmed [MCP,C#]
Kolachi Advanced Technologies
http://www.kolachi.net
 
J

Jon Skeet [C# MVP]

chris said:
Got the solution for the second string.

But I still do not knwo how to the first one. Can you just extract the first
character from a string?

If you want it as a character, just use the indexer:

char first = someString[0];

If you want it as a string, you might as well just use Substring.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top