我有包含零的字符串,不幸的是有很多空格.我想写一个方法,它将摆脱所有的空格,并返回只有1和0的字符串.
我怎样才能做到这一点?
String example= new String("1 011 01 1");
String shouldReturnStringWithoutWhiteSpaces(String given){
String cleanString
//some action which will "transform "1 011 01 1" into "1011011"
return cleanString;
}
Run Code Online (Sandbox Code Playgroud)
你可以做
String cleanString = given.replaceAll("\\s", "");
Run Code Online (Sandbox Code Playgroud)
这将取代所有空格.主要技巧是在运行时\\s变为\s所有白色空间.