TareqElgafy TareqElgafy - 1 year ago 73
Java Question

get all occurence of substring between two chracters in java

I have String Path like this


how I can get all the occurence of substring between "/" and "/" to get (Math-Math1-Algerbra)

Thanks in advance

Answer Source

I suppose you need the first 3 directories from a path. You can split on delimiter "/" and build from the result the new string. Obvious you have to pay attention if there aren't enough directories in the path.

    String s = "/Math/Math1/Algerbra/node";
    String aux[] = s.split("/");


Just found something better to join the strings (java 1.8):

    String.join("-", aux[1], aux[2], aux[3]);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download