tags:

views:

241

answers:

7

I want to split a string like "001A" into "001" and "A"

+1  A: 

Are the numbers and letters always going to be separate like that? Or can you potentially have something like 00A1?

jaltiere
This should be a comment to the question, not an answer.
Oded
I would have put it as a comment to the question, but I'm not sure how. I don't see a comment link available to me.
jaltiere
As a new user, you cannot leave comments. Sorry. It will come. Welcome to Stack Overflow.
Kobi
It doesn't make sense that new users can't comment on posts from other users but they can comment on their own posts :s
James
I agree. Until I get permission to do this, I find myself sometimes responding in my own comment to a comment placed somewhere else so I don't get dinged for asking for clarification in an answer. :)
jaltiere
@jaltiere: just say in your answer **not enough rep to post comment**. People will understand :)
James
+3  A: 
Match match = Regex.Match(s, @"^(\d+)(.+)$");
string numeral = match.Groups[1].Value;
string tail = match.Groups[2].Value;
Kobi
`.` matches anything, you should restrict to `\w` or `[a-zA-Z]`
knittl
@knittl - I know, `.` is ok as long as the OP doesn't need to validate it. The question doesn't have enough details so I went with this.
Kobi
the title of the question is »how to split numerics and _alphabets_« ;) but nevermind, in the given example your solution will give correct results
knittl
+2  A: 
string[] data = Regex.Split("001A", "([A-Z])");
data[0] -> "001"
data[1] -> "A"
James
String.Split doesn't take a regex, as far as I know.
Kobi
@Kobi....good spot lol was supposed to be Regex.Split!
James
Did you test it? It should be `Regex.Split("001A", "([A-Z])")`, or the group is removed (as a divider).
Kobi
@Kobi, no not tested. Thanks for the heads up, updated it.
James
this works, but it gives a string array of 3 elements. the last element is empty.
A: 

You could try something like this to retrieve the integers from the string:

StringBuilder sb = new StringBuilder();
Regex regex = new Regex(@"\d*");
MatchCollection matches = regex.Matches(inputString);
for(int i=0; i < matches.count;i++){
    sb.Append(matches[i].value + " ");
}

Then change the regex to match on characters and perform the same loop.

derek
+1  A: 

If your code is as simple|complicated as your 001A sample, your should not be using a Regex but a for-loop.

FireSnake
A: 

And if there's more like 001A002B then you could

    var s = "001A002B";
    var matches = Regex.Matches(s, "[0-9]+|[A-Z]+");
    var numbers_and_alphas = new List<string>();
    foreach (Match match in matches)
    {
        numbers_and_alphas.Add(match.Value);
    }
Jonas Elfström
+1  A: 

This is Java, but it should be translatable to other flavors with little modification.

    String s = "123XYZ456ABC";
    String[] arr = s.split("(?<=\\d)(?=\\D)|(?<=\\D)(?=\\d)");
    System.out.println(Arrays.toString(arr));
    // prints "[123, XYZ, 456, ABC]"

As you can see, this splits a string wherever \d is followed by a \D or vice versa. It uses positive and negative lookarounds to find the places to split.

polygenelubricants