1

I'm retrieving Strings from the database and storing in into a String variable which is inside the for loop. Few Strings i'm retrieving are in the form of:

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

and few are in the form of

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

I want split string into two substrings i.e https://www.ppltalent.com/test/en/soln-computers-ltd as https://www.ppltalent.com/test/en and /soln-computers-ltd.

It can easily be separated if i would have only /en.

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

But in many of the strings it has /jr , /ch etc.

So how can I split them in two sub-strings?

aioobe
  • 413,195
  • 112
  • 811
  • 826
Little bird
  • 1,106
  • 7
  • 28
  • 58
  • 1
    `String[] parts = string.split("-")` – OPK Mar 26 '15 at 14:51
  • @Jarrod I reopened this, since the main problem I see here is understanding what pattern to split upon. That is not explained (for this context) in that dupulicate. – Duncan Jones Mar 26 '15 at 14:53
  • @JarrodRoberson, I'm not sure this is in fact a duplicate of that question. – aioobe Mar 26 '15 at 14:53
  • same way you split them now either with `.split()` or with `.substring(indexof("condition"))` –  Mar 26 '15 at 14:53
  • 1
    @JarrodRoberson Yes, but `condition` is perhaps not obvious to the OP. – Duncan Jones Mar 26 '15 at 14:54
  • D'oh, now I'm the one doing bad closing. Can someone vote to reopen? I've already used my one reopen vote. I forgot the OP needs to split the string rather than just obtain the last segment. @aioobe ? – Duncan Jones Mar 26 '15 at 15:17

2 Answers2

5
  • You could perhaps use the fact that /en and /ja are both preceeded by /test/. So, something like indexOf("/test/") and then substring.

  • In your examples, it seems like you're interested in the very last part, which could be retrieved by lastIndexOf('/') for instance.

  • Or, using look-arounds you could do

    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
    
aioobe
  • 413,195
  • 112
  • 811
  • 826
  • Doesn't split remove the splitting string? So wouldn't you just get "https://www.ppltalent.com" ? – Diego Martinoia Mar 26 '15 at 15:02
  • In the last suggestion? You're right. However, that's a positive look behind. So it splits on the zero-width string following `/test/../`. – aioobe Mar 26 '15 at 15:03
  • Thanks for reply . I i'm looking to have two sub-strings i.e (a) 1. https://www.ppltalent.com/test/en and 2. /soln-computers-ltd (b) 1.https://www.ppltalent.com/test/ja and 2. /aman-computers-ltd – Little bird Mar 26 '15 at 15:04
  • 1
    @Littlebird, this can be achieved with any of the three solutions I suggest. Let me know if you want me to elaborate on any of the options. – aioobe Mar 26 '15 at 15:05
-1

Split on the last /

String fullUrl = "https:////www.ppltalent.com//test//en//soln-computers-ltd";
String baseUrl = fullUrl.substring(0, fullUrl.lastIndexOf("//"));
String manufacturer = fullUrl.subString(fullUrl.lastIndexOf("//"));
Diego Martinoia
  • 4,592
  • 1
  • 17
  • 36