0

My string have this form XxYxZx

X , Y and Z are characters and the x are numbers which can vary from 0-999.

I want to split this string in this form. How can this be done?

  1. Xx
  2. Yx
  3. Zx

Example:

 Input:  "A155B45C77D89" 
 Output: "A155", "B45", "C77", "D89"
5
  • can you explain exactlay what is your input and what you want output? Commented Sep 2, 2011 at 12:15
  • duplicate link: stackoverflow.com/questions/3243721/… Commented Sep 2, 2011 at 12:20
  • 1
    @Andrew, I believe the question is "how do I split a string on the form XxYxZx into Xx, Yx, Zx". Commented Sep 2, 2011 at 12:41
  • @aioobe I believe in Goblins. But that is as relevant to what the OP's actual (as yet unstated) question is, as speculation. (And questions need a '?') ;) Commented Sep 2, 2011 at 12:45
  • 1
    Hehe.. If the guy says, "I want to do this... but I don't know how." can you seriously not figure out what he is "asking"? Commented Sep 2, 2011 at 12:47

2 Answers 2

7
String myString="A155B45C77D89";
String[] parts = myString.split("(?<=\\d)(?=\\p{Alpha})");
System.out.println(Arrays.toString(parts));

Output:

[A155, B45, C77, D89]

Explanation:

String.split works with regular expressions. The regular expression (?<=\d)(?=\p{Alpha}) says "match all substrings preceeded by a digit, succeeeded by a alphabetic character.

In a string such as "A155B45C77D89", this expression is matched by the empty substrings

A155 B45 C77 D89
    ^   ^   ^
  here  |   |
       here |
            |
        and here
Sign up to request clarification or add additional context in comments.

Comments

1
public static void main(String[] args) {
        Pattern p = Pattern.compile( "([A-Z])([0-9]+)" );
        Matcher m = p.matcher( "X01Y123Z99" );
        while ( m.find() ) {
            System.out.println( m.group( 1 ) + " " + m.group( 2 ) );
        }
    }

prints

X 01
Y 123
Z 99

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.