1

So given data for example:

a898            //result string1=a  string2=898
b832            //string1=b string2=832
c3232           //string1=c string2=3232
d938202         //string1=d string2=938202

I'm attempting to get the letter and put it in a String, and then have the rest of the numbers in another String. I am using charAt(0) to get the letter, but am having issues finding a way to split the numbers given that they aren't all of the same length.

One way I used, when there weren't 26 diff options, in another program was replacing a with a|, b with b|, and so on and then splitting on the |. It led to very ugly code and I was hoping for another option. Thanks for your time!

2 Answers 2

5

You can use like

    String str = "d938202";
    String string1 = str.substring(0, 1);
    String string2 = str.substring(1);
Sign up to request clarification or add additional context in comments.

3 Comments

str.substring(1, str.length()); should give you same results as str.substring(1); if I remember correctly.
awww beautiful. Thank you @Ashiquizzaman, I truly appreciate it
@Pshemo, Yes both gives same results. Thanks.
2

You can also use something like:

String[] arr = "d938202".split("(?<=[a-z])");
//arr[0] = "d"
//arr[1] = "938202"

split uses regular expression (Regex) syntax which allows us to describe place to split on. In this case thanks to look-around mechanisms we can describe this place as one which have alphabetic character (a-z) before it.

Comments

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.