Remove all chars in java string, except '0' and '1' -
how can remove characters being neither '0' nor '1' in string, final string consists of 0s , 1s? i'm using java 8.
my solution loop (i think it's not efficient way):
string before = "012940124810"; string after = new string(); for(int = 0; < before.length(); i++) { if(before.charat(i) == '0' || before.charat(i) == '1') { after += before.charat(i); } }
you can use regex
, replaceall
method
string out = str.replaceall("[^01]", "");
Comments
Post a Comment