从String java获取某些子字符串

时间:2014-10-07 06:30:28

标签: java string substring

我可以使用以下字符串:

String s = "chapterId=c_1&sectionId=s_24666&isHL=1&cssFileName=haynes";

String s = "chapterId=c_1&sectionId=s_24666";

我需要获取数字(示例中为“24666”)。

String res = s.substring(s.lastIndexOf("s_")+ 2)这会返回数字+字符,直到字符串结尾(第二个例子没问题)。但是我需要在号码结束后停止。我怎样才能做到这一点。?感谢

7 个答案:

答案 0 :(得分:1)

您可以按字符&拆分字符串以获取参数,并使用=拆分每个参数以获取参数名称和参数值。现在查找参数名称"sectionId",并剪切其值的前2个字符以获取数字,如果需要Integer.parseInt(),则可以使用int

请注意,此解决方案足够灵活,可以处理所有参数,而不仅仅是您当前感兴趣的参数:

String s = "chapterId=c_1&sectionId=s_24666&isHL=1&cssFileName=haynes";

String[] params = s.split("&");
for (String param : params) {
    String[] nameValue = param.split("=");

    if ("sectionId".equals(nameValue[0])) {
        int number = Integer.parseInt(nameValue[1].substring(2));
        System.out.println(number); // Prints 24666

        // If you don't care about other parameters, this will skip the rest:
        break;
    }
}

注意:

如果从客户端传递的号码无效,您可能希望将Integer.parseInt()放入try-catch块中:

try {
    int number = Integer.parseInt(nameValue[1].substring(2));
} catch (Exception e) {
    // Invalid parameter value, not the expected format!
}

答案 1 :(得分:1)

您可以使用regExp

    String s = "chapterId=c_1&sectionId=s_24666";
    //OR
    //String s = "chapterId=c_1&sectionId=s_24666&isHL=1&cssFileName=haynes";
    s=s.replaceAll(".*?s_(\\d+).*","$1");
    System.out.println(s);

<强> 输出:

24666

其中,

  1. .*?s_表示s_s_包含在内)
  2. 之前的任何内容
  3. (\\d+)表示用于群组
  4. 的一个或多个数字()
  5. $1表示第1组,它是s_
  6. 之后的数字

    注意:假设您的每个字符串都遵循特定格式,其中包含s_s_之后的数字。

答案 2 :(得分:0)

请尝试以下操作:

  String s = "chapterId=c_1&sectionId=s_24666&isHL=1&cssFileName=haynes";
  String tok[]=s.split("&");
  for(String test:tok){
      if(test.contains("s_")){
          String next[]=test.split("s_");
            System.out.println(next[1]);

      }
  }

输出

24666

或者,如果不需要,则可以删除所有其他单词

String s="chapterId=c_1&sectionId=s_24666&isHL=1&cssFileName=haynes";
s=s.replaceAll(".*s_(\\d+).*","$1");
System.out.println(s);

输出

24666

这里的重点是使用string拆分Regular Expression以进一步将字符串分成几部分并获得所需内容。有关Regular Expressions访问this链接的更多信息。

答案 3 :(得分:0)

试试这个:

我在substring()方法中使用了一个检查 - 如果没有&#34;&amp; isHL&#34;在字符串中(意思是它向你展示的类型2),它只会读取直到字符串结束。否则,它会在&#34;&amp; isHL&#34;之前切断字符串。希望这可以帮助。

代码:

    String s = "chapterId=c_1&sectionId=s_**24666**";
    int endIndex = s.indexOf("&isHL");
    String answer = s.substring(s.lastIndexOf("s_") + 2,    endIndex == -1 ? s.length() : endIndex);

答案 4 :(得分:0)

你可以起诉这个正则表达式:(?<=sectionId=s_)(\\d+)这使用正面观察

demo here

答案 5 :(得分:0)

即使在给定字符串

中出现多次整数,以下代码也能正常工作
   String inputString = "chapterId=c_a&sectionId=s_24666&isHL=1&cssFileName=haynes_45";

    String[] inputParams = inputString.split("&");
    for (String param : inputParams)
    {
        String[] nameValue = param.split("=");

        try {
            int number = Integer.parseInt(getStringInt(nameValue[1]));
            System.out.println(number); 
        }
        catch(IllegalStateException illegalStateException){                
        }                
    }


private String getStringInt(String inputString)
{
    Pattern onlyInt = Pattern.compile("\\d+");
    Matcher matcher = onlyInt.matcher(inputString);
    matcher.find();
    String inputInt = matcher.group();
    return inputInt;
}
  

输出

2466 1 45

答案 6 :(得分:-1)

使用split方法

String []result1 = s.split("&");
String result2 = tempResult[1];
String []result3 = result2.split("s_");

现在要获得你想要的数字

String finalResult = result3[1];

INPUT:

String s = "chapterId=c_1&sectionId=s_24666&isHL=1&cssFileName=haynes";

OUPUT:

24666