Is there an easier way to split / rearrange a string?

I am currently using String.split("")as follows:

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 format first.second.third...n-1.n, and all I really need to do is get rid offirst.

+3
source share
2 answers

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
+8
source

You can use java.util.regexand execute regex instead.

Regular expression matching first.equals^[^.]+[.]

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

All Articles