2012-08-31 86 views
3

目前我使用String.split("")像這樣:有沒有更簡單的方法來拆分/重建一個字符串?

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]; 
} 

我的字符串是在first.second.third...n-1.n格式和我真正需要做的是擺脫first.

回答

8

我會用

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

打印

second.third...n-1.n 
+1

媽的,你是太快了:d – Baz

+0

@Baz時間休息一下。 ;) –

+0

看起來不錯。我已經做了一段時間,因爲我可以解釋'(「。*?\\。」,「」)'? – Phox

4

您可以使用java.util.regex並改爲執行regex。

正則表達式匹配first.^[^.]+[.]

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

我上了一堂課,然後我發誓所有的事情都在窗外。我想我需要多讀一點。 感謝您的快速響應 – Phox

+0

@Phox - 我在Perl(很大程度上)爲了生活而開發。正則表達式很容易,當你定期做(雙關語意) – DVK

+0

是的我已經做了很少的Perl代碼(我認爲它可能是前級或後續類)他們是瘋狂的強大 – Phox

相關問題