简体   繁体   English

JAVA 提取变量 substring

[英]JAVA extract variable substring

I have a string array,"stringArray", with the following contents:我有一个字符串数组,“stringArray”,内容如下:

"ADU-30"
"ADU-30 plus a cam"
"ADU-30 plus internal cam"
"ADU-60"
"ADU-60 plus a cam"
"ADU-60 plus internal cam"
"ADU-301"

My goal is to be able to extract the "ADU-" portion including the numeric digits to the right of the hyphen.我的目标是能够提取“ADU-”部分,包括连字符右侧的数字。 Currently, I can extract the ones with just two numeric digits to the right of the hyphen as follows:目前,我可以提取连字符右侧只有两个数字的数字,如下所示:

for (int i = 0; i < stringArray.length(); i++) {
   stringArray[i] = stringArray[i].substring(0,6);
}

However, when I change the substring arguments from substring(0,6) to substring(0,7) , it crashes on the item with just two digits to the right of the hyphen.但是,当我将 substring arguments 从substring(0,6)更改为substring(0,7)时,它在连字符右侧只有两位数字的项目上崩溃。 How can I store the three digits items?如何存储三位数项目? Also, is there a better way to do this then using substring?另外,有没有比使用 ZE83AED3DDF4667DEC0DAAAACB2BB3BE0BZ 更好的方法来做到这一点? My desired end result is the following string array:我想要的最终结果是以下字符串数组:

"ADU-30"
"ADU-60"
"ADU-301"

Keeping with your current pattern you could replace the hard-coded substring with a regex-based replaceAll (or replaceFirst ):与您当前的模式保持一致,您可以用基于正则表达式的replaceAll (或 replaceFirst )替换硬编码的replaceFirst

for (int i = 0; i < stringArray.length; i++) {
  stringArray[i] = stringArray[i].replaceAll("(ADU-\\d+).*", "$1");
}

This says to replace the whole string with the part, or group, that matches ADU-\\d+ , which is the string "ADU-" followed by 1 or more digits.这表示将整个字符串替换为与ADU-\\d+匹配的部分或组,即字符串"ADU-"后跟 1 个或多个数字。 The pattern after the capture group, ".*" just says to match zero or more charcters of any kind, which takes care of the, possibly empty, remainder of the string.捕获组之后的模式".*"只是表示匹配零个或多个任何类型的字符,它负责处理字符串的剩余部分,可能是空的。

Test:测试:

String[] stringArray = {
        "ADU-30",
        "ADU-30 plus a cam",
        "ADU-30 plus internal cam",
        "ADU-60",
        "ADU-60 plus a cam",
        "ADU-60 plus internal cam",
        "ADU-301"
        };

for (int i = 0; i < stringArray.length; i++) {
   stringArray[i] = stringArray[i].replaceAll("(ADU-\\d+).*", "$1");
}

for(String str : stringArray)
    System.out.println(str);

Output: Output:

ADU-30
ADU-30
ADU-30
ADU-60
ADU-60
ADU-60
ADU-301

A "brute force" approach would be something like this: “蛮力”方法是这样的:

for (int i = 0; i < stringArray.length; i++) {
        String s = stringArray[i];
        StringBuffer sb = new StringBuffer();
        for (int j = 0; j < s.length(); j++) {
                char c = s.charAt(j);
                if (c == ' ')
                        break;
                sb.append(c);
        }
        stringArray[i] = sb.toString();
}

Try using Java Pattern and Matcher class.尝试使用 Java 模式和匹配器 class。

String[] stringArray = new String[] {"ADU-6022 plus a cam",
                                    "ADU-30",
                                    "ADU-30 plus a cam",
                                    "ADU-30 plus internal cam",
                                    "ADU-60",
                                    "ADU-60 plus a cam",
                                    "ADU-60 plus internal cam",
                                    "ADU-301"};
String regex = "(ADU-\\d+)";
Pattern p = Pattern.compile(regex);
Matcher m;

for (int i = 0; i < stringArray.length; i++) {
    m = p.matcher(stringArray[i]);
    while (m.find()) {
        System.out.println(m.group(1) );
    }
}

Use regex to extract the first group.使用正则表达式提取第一组。

  // String to be scanned to find the pattern.
  String line = "ADU-30 plus a cam";
  String pattern = "^(ADU-\\d+).*$";

  // Create a Pattern object
  Pattern r = Pattern.compile(pattern);

  // Now create matcher object.
  Matcher m = r.matcher(line);

  if (m.find()) {
     System.out.println("Found value: " + m.group(0) );
  } else {
     System.out.println("NO MATCH");
  }

This pattern ^(ADU-\d+).*$ says:这种模式^(ADU-\d+).*$说:

  1. Start at the beginning of the string ( ^ )从字符串的开头( ^ )开始

  2. Capture "ADU" + some number of digits ( (ADU-\d+) )捕获“ADU”+一些数字( (ADU-\d+)

  3. Match anything until the end of the string ( .*$ )匹配任何内容,直到字符串结尾( .*$

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM