2014-06-24 102 views

回答

2

如果你想_remove /text,你可以嘗試

String path="//abc:def/m1/m/123/test"; 
path = path.substring(0,path.lastIndexOf("/")); 
System.out.println(path); 

輸出:

//abc:def/m1/m/123 
+0

simpleXpath = arrSimpleXpath.get(i).toString(); \t \t \t \t \t simpleXpath = simpleXpath.substring(0,simpleXpath.lastIndexOf(「/」)); im從xml通過simpleXpath獲取路徑嘗試過,但沒有工作... – user3769879

+0

@ user3769879你確定'simpleXpath'的值是'// abc:def/m1/m/123/test'? – Baby

+0

是的值是相同的 – user3769879

0

試試這個,

String s = new String("//abc:def/m1/m/123/test"); 
System.out.println(""+s.substring(0,s.lastIndexOf("/"))); 
1

您可以使用substringindexOf當你特別想從刪除

String str="//abc:def/m1/m/123/test"; 
System.out.println(str.substring(0, str.indexOf("/test"))); 

如果你有String喜歡//abc:def/m1/m/123/test/other/extra比將/test後也刪除String。所以如果你想刪除最後一個元素的路徑,我建議你去lastIndexOf('/')

作爲Immer Allein已經建議


如果你想刪除/test你也可以像這樣做,如果你有路徑的詳細內容/test後。 (即//abc:def/m1/m/123/test/other/extra

StringBuilder sb=new StringBuilder(); 
sb.append(str.substring(0, str.indexOf("/test"))); 
sb.append(str.substring(str.indexOf("/test")+5)); 
System.out.println(sb); 

輸出

//abc:def/m1/m/123/other/extra