【发布时间】:2013-11-20 02:35:58
【问题描述】:
我有一条线,从这条线我想在第一个单引号之间得到一个字符串 例如
这是一个要测试的“长”字符串,还有“很多”要测试的字符串
我需要在第一个单引号之间获取字符串值,即 long 作为最终结果
谢谢
【问题讨论】:
我有一条线,从这条线我想在第一个单引号之间得到一个字符串 例如
这是一个要测试的“长”字符串,还有“很多”要测试的字符串
我需要在第一个单引号之间获取字符串值,即 long 作为最终结果
谢谢
【问题讨论】:
如果我理解你的问题,我认为你想要的是使用 split()。
String a = "This is a 'long' string to test and there are 'many' more to come to 'test'";
String[] b = a.split("'");
System.out.println(b[1]);
b 变成一个字符串数组,数组中的第二个元素将是第一组单引号之间的字符串。
【讨论】:
获取子字符串的一个例子
String str="This is a 'long' string to test and there are 'many' more to come to 'test'";
String tempStr=str.substring(str.indexOf('\'')+1);
String finalStr=tempStr.substring(0,tempStr.indexOf('\''));
System.out.println(finalStr);
【讨论】:
你可以使用正则表达式来做到这一点
String test="This is a 'long' string to test"
Pattern p = Pattern.compile("\'.*?\'");
Matcher m = p.matcher(test);
while(m.find()){
System.out.println(test.substring(m.start()+1,test.end()-1));
}
【讨论】: