0

I have this String p="V755D888B154" and i want to split it to have this form

  • V
  • 755
  • D
  • 888
  • B
  • 154

How can i do it ? thanks in advance

2
  • 2
    Are you always going to have three letters and three numbers between 100 and 999? Regex may be one way to go. Java substring may be another if the character positions are fixed. Commented Sep 1, 2011 at 22:07
  • The letters are fixed but the numbers aren't , and yes they are between 100 and 999 Commented Sep 1, 2011 at 22:11

3 Answers 3

1

You can use String.split. Example:

String[] numbers = p.split("[a-zA-Z]+");
String[] letters = p.split("[0-9]+");

numbers or letters can have empty string, but you can check it manually.

Sign up to request clarification or add additional context in comments.

1 Comment

That really worked it separated the string and the numbers like i've wanted . thank you and also so many thanks and respect for the other answers ! i have so much respect for what you do here trying helps us , we the newbies :)
0

If your string contains only numbers and strings this snippets workes

    String string = "V755D888B154";
    Pattern p = Pattern.compile("\\d+|\\D+");
    Matcher matcher = p.matcher(string);
    while(matcher.find()) {
        Integer i = null;
        String s = null;
        try {
            i = Integer.parseInt(matcher.group());
        }
        catch (NumberFormatException nfe) {
            s = matcher.group();
        }
        if (i != null) System.out.println("NUMBER: " + i);
        if (s != null) System.out.println("STRING: " + s);
    }

main fail is checking if given String (matcher.group()) consist Integer or not

Comments

0

In your comment you say the letters are fixed, so you if you're just trying to pull out the numbers you could always do something like this. I'll leave it up to you if you think this is a kluge.

String p="V755D888B154";

Integer vPart = Integer.valueOf(p.substring(1,4));
Integer dPart = Integer.valueOf(p.substring(5,8));
Integer bPart = Integer.valueOf(p.substring(9,12));

System.out.println(bPart);

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.