Skip to content
Advertisement

How to replace text symbol in Java or Text Editor

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

enter image description here

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"
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement