简体   繁体   中英

How to Extract text from given string?

I want to extract a perticular image path string from a given string .

The String is http:\\localhost:9090\\SpringMVC\\images\\integration-icon.png

Now i want to get only the path after images like

\images\integration-icon.png

i tried this

Pattern pattern = Pattern.compile("SpringMVC");
Matcher matcher = pattern.matcher(str);
System.out.println("Checking");
if (matcher.find()) {
    System.out.println(matcher.group(1));
}

how can i get ?

String filename = filepath.substring(filepath.lastIndexOf("\\") + 1);

或(没试过,看起来有点奇怪)

String filename = filepath.substring(filepath.lastIndexOf("\\", "images\\".length()) + 1);
String string = "http:\localhost:9090\ZenoBusinessStore\images\integration-icon.png";
int index = string.indexOf("images\\");
String output = string.substring(index);
String text = "http:\localhost:9090\SpringMVC\images\integration-icon.png"
String subText = text.subString(text.indexOf("\images"), text.length());
System.out.println(subText);
String in = "http:\\localhost:9090\\ZenoBusinessStore\\images\\integration-icon.png";
String op = in.replace("http:\\localhost:9090\\ZenoBusinessStore", "");
System.out.println(op);

ZenoBusinessStore must be the name of your project which is constant. Now split the string

String s = "http:\localhost:9090\ZenoBusinessStore\images\integration-icon.png";
String ary = s.split("ZenoBusinessStore");

Now the 2nd element of the array is your image path.

 System.out.println(ary[1]);

Use '\\\\'. It's because backslash is used in escape sequence like '\\n'. With a single \\ the compiler have no way to know.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM