簡體   English   中英

如何將字符串分為兩部分

[英]How to split string in two parts

我正在從數據庫中檢索字符串,並將其存儲在for循環內的String變量中。 我要檢索的字符串很少有以下形式:

https://www.ppltalent.com/test/en/soln-computers-ltd

很少有形式

https://www.ppltalent.com/test/ja/aman-computers-ltd

我想將字符串分成兩個子字符串,即https://www.ppltalent.com/test/en/soln-computers-ltd作為https://www.ppltalent.com/test/en/soln-computers-ltd

如果我只有/en它可以很容易地分開。

String[] parts = stringPart.split("/en");
System.out.println("Divided String  : "+ parts[1]);

但是在許多字符串中都有/jr/ch等。

那么如何將它們分成兩個子字符串?

  • 您可能會使用/en/ja都在/test/的事實。 因此,類似indexOf("/test/")然后是substring

  • 在您的示例中,似乎您對最后一部分感興趣,例如可以通過lastIndexOf('/')進行檢索。

  • 或者,您可以使用環顧四周

     String s1 = "https://www.ppltalent.com/test/en/soln-computers-ltd"; String[] parts = s1.split("(?<=/test/../)"); System.out.println(parts[0]); // https://www.ppltalent.com/test/er/ System.out.println(parts[1]); // soln-computers-ltd 

在最后分割/

String fullUrl = "https:////www.ppltalent.com//test//en//soln-computers-ltd";
String baseUrl = fullUrl.substring(0, fullUrl.lastIndexOf("//"));
String manufacturer = fullUrl.subString(fullUrl.lastIndexOf("//"));

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM