简体   繁体   中英

Splitting of a String into a multiple element list in Java

I have a String "Student1Student2Student3" for example, is there anyway i can turn this String into a list with three elements eg ["Student1", "Student2", "Student3"]?

The above string is just an example, "Student1" could be a random name of variable length. Thanks in advance.

Based on your comment

I think in the final code it would be ideal to split on a lowercase letter followed by an uppercase letter.

it looks like you are looking for split("(?<=[az])(?=[AZ])") .

Look-around mechanisms are zero-length which means they don't include in matched (in delimiter) characters they ware suppose to test. This will allow us to test lower-case character from left and upper-case from right but not consume it while splitting (since they will not be included in match).

You can use the regex ([a-zA-Z]+[0-9]+) to match specific parts of the string (for example in the string Student1Student2Student3 , the first match would be Student1 , and then Student2 .

Here's a hint, use the Pattern class and the Matcher class

:) hopefully it will be enough to help you out.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM