I want to replace the symbol after the second comma. I know how to group until the second comma.
I tried below. But I don’t know how to replace from “/” to “?” after the second comma
I expected below. GROUP/COMPANY,USER/PASSWORD,BIRTHDAY:”2010/01/01″,ADDRESS:”US/ALASKA” ↓ GROUP/COMPANY,USER/PASSWORD,BIRTHDAY:”2010?01?01″,ADDRESS:”US?ALASKA”
Advertisement
Answer
In Java
Note that the logic of below code is “after finding the first numeric value”, which is the same as “after the second comma” for these examples. If you need to change this behaviour, you should change the condition in which mustReplace
is set to true
, meaning, start replacing starting from here to the end.
String t ="GROUP/COMPANY,USER/PASSWORD,BIRTHDAY:"2010/01/01",ADDRESS:"US/ALASKA""; char[] textArr = t.toCharArray(); boolean mustReplace = false; int i=0; for (char c:textArr) { if (!mustReplace && Character.isDigit(c)) mustReplace=true; if (mustReplace && c=='/') textArr[i] ='?'; i++; } t = new String(textArr); System.out.println(t);
Output:
GROUP/COMPANY,USER/PASSWORD,BIRTHDAY:"2010?01?01",ADDRESS:"US?ALASKA"