Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use regex to remove unwanted commas

Tags:

java

regex

I am creating an app in which I have generated string like this:

 hihibjj,,,,,,,ghjjjj, , ,[email protected],,,,[email protected], [email protected], [email protected]

What I want is to delete unwanted commas, only single comma separated values should be present.

Code that I have tried

for (int i = 0; i < temp.length; i++) {
  for (int j = 0; j < emailSeperated.size(); j++) {
    if (temp[i].trim().equals(emailSeperated.get(j).trim())) {
      strEmailValue = strEmailValue.replace(temp[i], "").trim();
      Log.e("strEmail trimmed value", strEmailValue);
    } else if (temp[i].trim().equals(emailSeperated.get(j).trim())) {
      strEmailValue = strEmailValue.replaceAll(temp[i] + ",", "").trim();
    }
  }
} 
like image 370
Anuj Avatar asked Mar 18 '23 13:03

Anuj


1 Answers

Just find: ,,+ and replace with: ,

like image 157
Toto Avatar answered Mar 31 '23 22:03

Toto