【问题标题】:How to get a string present within single quotes in a line using java [closed]如何使用java在单引号内获取字符串[关闭]
【发布时间】:2013-11-20 02:35:58
【问题描述】:

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

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

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

谢谢

【问题讨论】:

标签: java string


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    获取子字符串的一个例子

    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);
    

    【讨论】:

      【解决方案3】:

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

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

      【讨论】:

        猜你喜欢
        • 2012-07-07
        • 1970-01-01
        • 2015-11-04
        • 2018-09-04
        • 1970-01-01
        • 2021-08-07
        • 1970-01-01
        • 2017-06-13
        • 2013-10-27
        相关资源
        最近更新 更多