下面是我的字符串
/downloadAPK/D:/coinFiles/Coin-v1.1.8.apk
我想在第二个斜杠(/)之后得到值,这是
D:/coinFiles/Coin-v1.1.8.apk
我该怎么做?
注意:
我在变量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);
}
我想获得完整的文件位置
答案 0 :(得分:2)
即使是更好更简单的解决方案\\/.*?\\/(.*)
\\/.*?\\/(.*)
:\\/.*?\\/
匹配前两个/
和
(.*)
:捕获前两个/
String s = "/downloadAPK/D:/coinFiles/Coin-v1.1.8.apk";
String result=s.replaceAll("\\/.*?\\/(.*)", "$1");
System.out.println(result);
输出:
D:/coinFiles/Coin-v1.1.8.apk
如果输入中始终有一个replceAll
,则可以使用带:
的正则表达式
String s = "/downloadAPK/D:/coinFiles/Coin-v1.1.8.apk";
String result=s.replaceAll(".*([A-Za-z]:.*)", "$1");
System.out.println(result);
输出:
D:/coinFiles/Coin-v1.1.8.apk
.*([A-Za-z]:.*)
:.*
匹配任何字符
([A-Za-z]:.*)
:[A-Za-z]
匹配D
()
:是一个捕获组,表示为$1
:.*
:将在:
否则
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));
输出:
D:/coinFiles/Coin-v1.1.8.apk
答案 1 :(得分:1)
如果您确定,字符串中始终存在冒号(:),那么您可以使用它。
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));
}
}
输出: 值D:/coinFiles/Coin-v1.1.8.apk
此代码不带冒号(:)
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;
}
}
<强>输出强>
之前的值/下载APK / D:/coinFiles/Coin-v1.1.8.apk
在值D之后:/coinFiles/Coin-v1.1.8.apk
答案 2 :(得分:0)
您可以迭代地找到索引。您也可以编写递归版本,但直到最后一步才执行子字符串;这意味着它不会污染字符串池。
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());
}
}
这是一个StringTokenizer
版本,可以为您处理索引。
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();
}
}