Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to extract uppercase substrings from a String in Java?

I need a piece of code with which I can extract the substrings that are in uppercase from a string in Java. For example:

"a:[AAAA|0.1;BBBBBBB|-1.90824;CC|0.0]"

I need to extract CC BBBBBBB and AAAA

like image 704
Hossein Avatar asked Dec 22 '22 03:12

Hossein


1 Answers

You can do it with String[] split(String regex). The only problem can be with empty strings, but it's easy to filter them out:

String str = "a:[AAAA|0.1;BBBBBBB|-1.90824;CC|0.0]";
String[] substrings = str.split("[^A-Z]+");
for (String s : substrings)
{
    if (!s.isEmpty())
    {
        System.out.println(s);
    }
}

Output:

AAAA
BBBBBBB
CC
like image 180
Adam Stelmaszczyk Avatar answered Dec 24 '22 00:12

Adam Stelmaszczyk