java - How to remove duplicates from string (not array) without using StringBuilder? -
i working program , need remove repeated characters string given me user. referenced other questions, using stringbuilder
remove duplicates. however, there way remove duplicates without turning string array, using stringbuilder
, set
?
i haven't learnt yet, don't understand them well. help?
for example, if user types in happyrolling
result should hapyroling
.
it seems example want remove repeated characters (not words).
you can use regex find repeats , remove them:
str = str.replaceall("(.)\\1+", "$1");
this regex captures every character matches when followed same character using reference captured group. replacement captured character, example "xx" replaced "x"
Comments
Post a Comment