Phox
Phox

Reputation: 101

is there an easier way to split/rebuild a string?

currently I am using String.split("") like so:

String[] tmp = props.get(i).getFullName().split("\\.");
String name = "";
for(int j = 1; j < tmp.length; j++){
    if(j > 1){
        name = name + "." + tmp[j];
    }
    else
        name = name + tmp[j];
}

my String is in the format of first.second.third...n-1.n and all i really need to do is get rid of first.

Upvotes: 3

Views: 610

Answers (2)

DVK
DVK

Reputation: 129489

You can use java.util.regex and execute a regex instead.

The regex to match first. is ^[^.]+[.]

String s = "first.second.third...n-1.n";
s.replaceAll('^[^.]+[.]', '');

Upvotes: 4

Peter Lawrey
Peter Lawrey

Reputation: 533750

I would use

String s = "first.second.third...n-1.n";
s = s.substring(s.indexOf('.')+1);
// or
s = s.replaceFirst(".*?\\.", "");
System.out.println(s);

prints

second.third...n-1.n

Upvotes: 8

Related Questions