2013-10-18 33 views
0

我想从给定的字符串中提取一个特定的图像路径字符串。如何从给定的字符串中提取文本?

字符串为http:\localhost:9090\SpringMVC\images\integration-icon.png

现在我想只有像

\images\integration-icon.png 

图像之后的路径我想这

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

我怎样才能得到?

+0

** perticular **是什么意思? – SpringLearner

+0

如果您使用的是Java 7,请注意Path类http://docs.oracle.com/javase/7/docs/api/java/nio/file/Path.html – DominikM

+0

为什么您给了我-2? –

回答

0
String string = "http:\localhost:9090\ZenoBusinessStore\images\integration-icon.png"; 
int index = string.indexOf("images\\"); 
String output = string.substring(index); 
+0

不会工作,你会得到错误 - 无效的转义序列(有效的转义序列是\ b \ t \ n \ f \ r \“\'\\)使用转义序列 –

+0

您确定?因为我使用了有效一个 – RamonBoza

+0

尝试并在编译器上运行它 –

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

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

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

ZenoBusinessStore一定是你的项目名称是恒定的。 现在拆分字符串

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

现在数组的第二个元素是你的图片路径。

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

不会工作,你会得到错误 - 无效的转义序列(有效的是\ b \ t \ n \ f \ r \“\'\\)。使用转义序列。 –

0

使用'\\'。这是因为反斜杠用于像\ n这样的转义序列中。使用单个\编译器无法知道。

相关问题