Reputation: 704
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"
Upvotes: 1
Views: 87
Reputation: 5232
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:
Start at the beginning of the string (^
)
Capture "ADU" + some number of digits ((ADU-\d+)
)
Match anything until the end of the string (.*$
)
Upvotes: 0
Reputation: 566
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) );
}
}
Upvotes: 1
Reputation: 2177
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();
}
Upvotes: 1
Reputation: 5455
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
Upvotes: 2