tajMahal
tajMahal

Reputation: 418

Remove extra comma in comma-delimited string

Suppose if i have this string

Result="INDIA,,USA,SA,NA,,PAK";

from this Result string how can i remove , (cComma) symbol if i have more than one comma i need to replace with sigle comma.How can i do this?

expected Result:

"INDIA,USA,SA,NA,PAK";

Upvotes: 2

Views: 2338

Answers (3)

Kent
Kent

Reputation: 195039

does this help?

Result = Result.replaceAll(",+", ",");

P.S, better follow the java naming convention, name the variable as result (small r).

Upvotes: 2

Niraj Patel
Niraj Patel

Reputation: 2208

Result = Result.replaceAll(",,", ",")

Upvotes: 0

AntonH
AntonH

Reputation: 6437

With replaceAll:

Result = Result.replaceAll(",{2,}", ",");

The ",{2,}" is a regex, used to replace the comma character if it appears at least twice.

Upvotes: 11

Related Questions