java - Deleting whitespaces before a certain characters with regex -
say, have string first line , need convert second line.
"english ,spanish ,eastern japanese " "english,spanish,eastern japanese"
as see, there several whitespaces before commas, each element may have spaces between variables, (like "eastern japanese"). so, need delete multiple whitespaces coming before commas. can trim last occuring whitespaces before using replaceall()
method, no problem.
you can remove excessive spaces replaceall
:
string s = "english ,spanish".replaceall("\\s+,",",");
see ideone demo
or trim()
:
system.out.println("english ,spanish ".replaceall("\\s+,",",").trim());
another demo
and hint: if there spaces trim after ,
, add \\s*
after ,
in pattern:
.replaceall("\\s+,\\s*",",")
Comments
Post a Comment