Reputation: 121
I am given a string = "SUBTOTAL(9,L7:L17)"
I want to replace all L with 2 in given string but L from SUBTOTAL should not be changed or replacing all L with 2 inside brackets.
I have tried with replaceAll()
in java method but its replacing all L with 2
resulting "SUBTOTA2(9,27:217)"
which is wrong
What i want like this
Result will be like : "SUBTOTAL(9,27:217)"
Upvotes: 1
Views: 52
Reputation: 3534
You can split your string into two substrings based on the first occurence of (
, then replace your character on the second part and recombine the result:
String string = "SUBTOTAL(9,L7:L17)";
int replaceStartIndex = string.indexOf('(');
System.out.println(string.substring(0, replaceStartIndex)
+ string.substring(replaceStartIndex).replaceAll("L", "2"));
Outputs SUBTOTAL(9,27:217)
Upvotes: 2