1

I have a string array,"stringArray", with the following contents:

"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. 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. How can I store the three digits items? Also, is there a better way to do this then using substring? My desired end result is the following string array:

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

4 Answers 4

2

Keeping with your current pattern you could replace the hard-coded substring with a regex-based replaceAll (or 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. 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:

ADU-30
ADU-30
ADU-30
ADU-60
ADU-60
ADU-60
ADU-301
Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

1

Try using Java Pattern and Matcher 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) );
    }
}

Comments

0

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:

  1. Start at the beginning of the string (^)

  2. Capture "ADU" + some number of digits ((ADU-\d+))

  3. Match anything until the end of the string (.*$)

2 Comments

I am getting an invalid escape sequence error for the specified string pattern. "Valid ones are \b \t \n \f \r \" \' \\".
Sorry, since it's a java string, use two \\ to escape. The first \ escapes the second \, which in turn escapes the d in the regex.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.