2013-09-16 57 views
1

優雅的方式我想從「https://rumpelstiltskin.com:7071/service/admin/soap尋找得到一個字符串的一部分lefmost,與Java

我在做這樣的回報「https://rumpelstiltskin.com:7071/service/admin」:

String str="https://rumpelstiltskin.com:7071/service/admin/soap"; 
String strOut=""; 
strOut=str.split("/soap")[0]; 
System.out.println(strOut); 

有一個更優雅的方式來做到這一點?

+0

你可以使用'子(INT,INT)',但我不知道這是任何更優雅。 – superEb

+0

最後一個路徑將始終是小寫的肥皂? –

回答

2

怎麼樣以下使用String#replace()

String str = "https://rumpelstiltskin.com:7071/service/admin/soap"; 
String newString = str.replace("/soap", ""); 
System.out.println(newString); 

輸出是(久經考驗的):

https://rumpelstiltskin.com:7071/service/admin 
1

你可以試試這個: -

String str = "https://rumpelstiltskin.com:7071/service/admin/soap"; 
String s = str.replace("/soap", ""); 
2

試試這個

String str="https://rumpelstiltskin.com:7071/service/admin/soap"; 
String newStr = str.substring(0,str.lastIndexOf("/")); 
System.out.println(newStr); 
+2

這將是我的答案,但它不回答我的意見,如果它想檢查URL路徑上的肥皂 –

0
String str="https://rumpelstiltskin.com:7071/service/admin/soap"; 
String strOut= str.substring(0, str.lastIndexOf("/")); 

只有工作,如果,作爲問最後一個塊總是/肥皂

+1

不,它的反過來,即使它不是肥皂也會起作用 –

+0

是的。我應該更清楚我的意思是關於OP的問題。 – Tim

0
String str="https://rumpelstiltskin.com:7071/service/admin/soap"; 
String strOut=str.substring(0, str.lastIndexOf("/soap")); 
System.out.println(strOut); 
相關問題