-4

Below is my string

/downloadAPK/D:/coinFiles/Coin-v1.1.8.apk

I want to get the value after 2nd slash(/) which is

D:/coinFiles/Coin-v1.1.8.apk

How should I do this?

Note : I am getting this string in my rest call in variable restOfTheUrl

@RequestMapping(value="/downloadAPK/**", method = RequestMethod.GET)
    public void downloadFile(HttpServletResponse response, HttpServletRequest request) throws IOException {

        String restOfTheUrl = (String) request.getAttribute(
                HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE);
}

I want to get the complete file location

Pavneet_Singh
  • 36,884
  • 5
  • 53
  • 68
Abhishek Patil
  • 1,373
  • 3
  • 30
  • 62
  • 2
    Hover your mouse over downvote arrow and you will see potential reasons to use it. In this case I am guessing it is lack of shown research or even attempt to solve it yourself. – Pshemo Jan 13 '17 at 11:49
  • What did you try so far? – Thomas Jan 13 '17 at 11:49
  • 1
    @PavneetSingh there are 2 problems: "2nd" and "slash", i.e. not "last" and "backslash" ;) – Thomas Jan 13 '17 at 11:50
  • Possible duplicate of [How to split a string in Java](http://stackoverflow.com/questions/3481828/how-to-split-a-string-in-java) – Stefan Lindner Jan 13 '17 at 11:52
  • 2
    To be honest, my downvote are simply because you **asked** for a regex. You know that exist (you use the tag) but didn't bother to try to write one. So I don't see any effort from you here. EDIT : No need to remove the tag, there is a log of every edit ;) – AxelH Jan 13 '17 at 11:54
  • 1
    @PavneetSingh Even OPs example shows that this isn't the case. – Tom Jan 13 '17 at 11:57
  • @Tom so in that case i will post the solution if OP shows his efforts :P – Pavneet_Singh Jan 13 '17 at 11:59
  • 1
    @PavneetSingh Your posted comment ***isn't*** the solution. It won't work. So there is no need to use this as an answer. – Tom Jan 13 '17 at 12:03
  • 1
    @abhi314 you do understand that the effort we expect from you is to **try** before asking. No sending more code that is out of context. – AxelH Jan 13 '17 at 12:04
  • 1
    @Tom please read my comments carefully , i never said it is a right solution , previous comment was about future answer , there is a `will` – Pavneet_Singh Jan 13 '17 at 12:05

3 Answers3

2

Even a better and simple solution \\/.*?\\/(.*)

Regex Demo

\\/.*?\\/(.*) : \\/.*?\\/ match the first two / and content between

(.*) : capture whatever is after first two /

    String s = "/downloadAPK/D:/coinFiles/Coin-v1.1.8.apk";
    String result=s.replaceAll("\\/.*?\\/(.*)", "$1");
    System.out.println(result);

Output :

D:/coinFiles/Coin-v1.1.8.apk

You can use a regex with replceAll if there is always one : in the input

    String s = "/downloadAPK/D:/coinFiles/Coin-v1.1.8.apk";
    String result=s.replaceAll(".*([A-Za-z]:.*)", "$1");
    System.out.println(result);

Output :

D:/coinFiles/Coin-v1.1.8.apk

.*([A-Za-z]:.*) : .* matches any character

([A-Za-z]:.*) : [A-Za-z] match a character like D

() : is a capturing group which is represented as $1

:.* : will capture all after :

Otherwise

    String s = "/downloadAPK/D:/coinFiles/Coin-v1.1.8.apk";

    // find first index of /
    int index =s.indexOf("/");

    // find second index of /        
    index=s.indexOf("/", index+1);

    // fetch substring from second index+1 of /
    System.out.println(s.substring(index+1));

Output :

D:/coinFiles/Coin-v1.1.8.apk
Pavneet_Singh
  • 36,884
  • 5
  • 53
  • 68
1

If you are sure, always colon(:) will exist in string, then you can use this.

import java.io.*;
public class Test {

   public static void main(String args[]) {
      String str = new String("/downloadAPK/D:/coinFiles/Coin-v1.1.8.apk");
      String subStr1 = new String(":");
      System.out.println("value  "+ str.substring(str.indexOf( subStr1 )-1));
   }
}

output: value D:/coinFiles/Coin-v1.1.8.apk

This code for without colon (:)

public class HelloWorld{

    public static void main(String args[]) {
      String str = new String("/downloadAPK/D:/coinFiles/Coin-v1.1.8.apk");
     System.out.println("before value" + str);
         str = getPattern(str, 2);
      System.out.println("\nAfter value  "+ str);


   }
   public static String getPattern(String str, Integer pos) {
       for(int i = 0; i< pos; i ++) {
       str = str.substring(str.indexOf("/") +1);
              }
       return str;
   }
}

Output

before value/downloadAPK/D:/coinFiles/Coin-v1.1.8.apk

After value D:/coinFiles/Coin-v1.1.8.apk

shas
  • 703
  • 2
  • 8
  • 31
  • 2
    Please. explain your solution. Note : you should watch you variable name, don't start with an uppercase – AxelH Jan 13 '17 at 12:13
  • @shas Thank you for you answer, unfortunately " : " will not always be in the link, currently I am testing in my local machine which is windows but the test server is on Linux. – Abhishek Patil Jan 13 '17 at 12:16
0

You can iteratively find the index. You could also write a recursive version, but this does not perform the substring until the final step; which means it will not pollute the String pool.

public class StringUtil {
    public static void main(String[] args) {
        String path = "/downloadAPK/D:/coinFiles/Coin-v1.1.8.apk";
        System.out.println(substring2(path, "/", 2)); // D:/coinFiles/Coin-v1.1.8.apk

        String test = "this---is---a---test";
        System.out.println(substring2(test, "---", 3)); // test
    }

    public static String substring2(String src, String str, int offset) {
        if (offset <= 0) {
            return src;
        }
        int index = -1, pos = 0;
        while (pos++ < offset) {
            index = src.indexOf(str, index + 1);
        }
        return src.substring(index + str.length(), src.length());
    }
}

Here is a StringTokenizer version which handles indexing for you.

import java.util.Enumeration;
import java.util.StringTokenizer;

public class StringUtil {
    public static void main(String[] args) {
        String path = "/downloadAPK/D:/coinFiles/Coin-v1.1.8.apk";
        System.out.println(substring2(path, "/", 1)); // D:/coinFiles/Coin-v1.1.8.apk

        String test = "this---is---a---test";
        System.out.println(substring2(test, "---", 3)); // test
    }

    public static String substring2(String src, String delim, int offset) {
        StringTokenizer tokenizer = new StringTokenizer(src, delim);
        while (offset-- > 0 && tokenizer.hasMoreTokens()) {
            tokenizer.nextToken();
        }
        return join(tokenizer, delim);
    }

    public static <T> String join(Enumeration<T> enumeration, String delim) {
        StringBuffer buff = new StringBuffer();
        while (enumeration.hasMoreElements()) {
            buff.append(enumeration.nextElement());
            if (enumeration.hasMoreElements()) {
                buff.append(delim);
            }
        }
        return buff.toString();
    }
}
Mr. Polywhirl
  • 42,981
  • 12
  • 84
  • 132
  • It is worth mentioning that this loop can overflow. For instance if at some point `src.indexOf(str, index + 1);` will return -1 we will start searching for `str` from beginning again. – Pshemo Jan 13 '17 at 12:18
  • @Pshemo I added a `StringTokenizer` response to handle safe index/bound checking. – Mr. Polywhirl Jan 13 '17 at 12:24
  • This is becoming complex knowing that this is coming from a request with a value always beginning with the same String ;) – AxelH Jan 13 '17 at 12:25