如何使用java获取行中单引号内的字符串

如何使用java获取行中单引号内的字符串,java,string,Java,String,我有一行,从这行我想得到一个字符串之间的第一个单引号存在 比如说 这是一个要测试的“长”字符串,还有“更多”要测试 我需要得到第一个单引号之间的字符串值,即long,作为最终结果 谢谢如果我正确理解你的问题,我想你想要的是使用split() b成为字符串数组,数组中的第二个元素将是第一组单引号之间的字符串。获取子字符串的一个示例 String str="This is a 'long' string to test and there are 'many' more to come to 'te

我有一行,从这行我想得到一个字符串之间的第一个单引号存在 比如说

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

我需要得到第一个单引号之间的字符串值,即long,作为最终结果


谢谢

如果我正确理解你的问题,我想你想要的是使用split()


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));
    }
重复
    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));
    }