-9

我有一条线,从这条线我想在第一个单引号之间得到一个字符串,例如

这是一个要测试的“长”字符串,还有“很多”要来“测试”

我需要在第一个单引号之间获取字符串值,即作为最终结果

谢谢

4

3 回答 3

6

如果我正确理解您的问题,我认为您想要的是使用 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 变成一个字符串数组,数组中的第二个元素将是第一组单引号之间的字符串。

于 2013-11-08T06:46:43.897 回答
0

获取子字符串的一个示例

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);
于 2013-11-08T06:48:44.890 回答
0

你可以使用正则表达式来做到这一点

    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));
    }
于 2013-11-08T06:51:01.423 回答