3

I'm trying to split some user input. The input is of the form a1 b2 c3 d4. For each input (eg; a1), how do I split it into 'a' and '1'?

I'm familiar with the string split function, but what do I specify as the delimiter or is this even possible?

Thanks.

7 Answers 7

3

You could use String#substring()

String a1 = "a1"
String firstLetterStr = a1.substring(0,1);
String secondLetterStr = a1.substirng(1,a1.length());

Similarly,

String c31 = "c31"
String firstLetterStr = c31.substring(0,1);
String secondLetterStr = c31.substirng(1,c31.length());
Sign up to request clarification or add additional context in comments.

3 Comments

Won't work if he chooses 'aa31' or something of the like. Don't know if it's possible, but, just figured I would point it out.
@JasCav: Of course this wouldn't. But let the OP comes and say something like this. I already made a mistake of editing the original question based on a similar comment -- not from OP.
@Adeel - Good point. Sometimes it is actually better to go with the simple answer based on the needs of the developer. I do have to be careful not to overengineer solutions at times.
2

If you want to split the string generically (rather than trying to count characters per the other answers), you can still use String.split(), but you have to utilize regular expressions. (Note: This answer will work when you have strings like a1, a2, aaa333, etc.)

String ALPHA = "\p{Alpha}";
String NUMERIC = "\d";

String test1 = "a1";
String test2 = "aa22";

ArrayList<String> alpha = new ArrayList();
ArrayList<String> numeric = new ArrayList();

alpha.add(test1.split(ALPHA));
numeric.add(test1.split(NUMERIC));
alpha.add(test2.split(ALPHA));
numeric.add(test2.split(NUMERIC));

At this point, the alpha array will have the alpha parts of your strings and the numeric array will have the numeric parts. (Note: I didn't actually compile this to test that it would work, but it should give you the basic idea.)

3 Comments

"\p{Alpha}" gives me an error saying "Invalid escape sequence".
@Triple777er - Try "[a-zA-Z]" for now instead of "\p{Alpha}".
Thanks a lot Jas, I got a basic idea on how to do it now.
2

it really depends how you're going to use the data afterwards, but besides split("") or accessing individual characters by index, one other way to split into individual character is toCharArray() -- which just breaks the string into an array of characters...

Comments

1

Yes, it is possible, you can use split("");

Comments

1

After you split user input into individual tokens using split(" "), you can split each token into characters using split("") (using the empty string as the delimiter).

Comments

0

Split on space into an array of Strings, then pull the individual characters with String.charAt(0) and String.charAt(1)

Comments

0

I would recommend just iterating over the characters in threes.

for(int i = 0; i < str.length(); i += 3) {
     char theLetter = str.charAt(i);
     char theNumber = str.charAt(i + 1);
     // Do something
}

Edit: if it can be more than one letter or digit, use regular expressions:

([a-z]+)(\d+)

Information: http://www.regular-expressions.info/java.html

2 Comments

I figured it was 1 letter/1 digit combinations. It could be, still.
Thanks. It's only for 1 letter 1 digit combination.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.