Reputation: 3
I'm currently using the following code to remove certain characters from a string in an array
myArray[x] = myArray[x].replaceAll("[aeiou]","");
which works fine, but I need to ignore the first character of the string so for example if an array element was Alan it would be stripped to Aln.
I'm not sure if using a replaceAll is the best way about doing it but the only other way I can think of is removing the first character, applying the above regex to the string, appending the character back on and then inserting back into the array, which seems a long winded way of doing it.
Upvotes: 0
Views: 758
Reputation: 3464
What about something like ..
myArray[x] = myArray[x].replaceAll("(^.)[aeiou]", "\\1");
// upd
Negative lookbehind is your solution, like Boris answered.
Upvotes: 1
Reputation: 61128
You can use a negative lookbehind to assert that the pattern is not preceded by the line start marker (^
):
public static void main(String[] args) throws Exception {
final String[] input = {"abe", "bae"};
for(final String s: input) {
System.out.println(s.replaceAll("(?<!^)[aeiou]", ""));
}
}
Output:
ab
b
Upvotes: 1