java string of blanks is removed

Remove the string space

In reading the configuration file is termiBrand = CMDC`01 | Huawei Technologies Co., Ltd`05 there's value can not check out or inside the database are compared to determine a value, a string of spaces need to be removed and then making judgments

str.trim(); //去掉首尾空格
str.replace(" ",""); //去除所有空格,包括首尾、中间
str.replaceAll(" ", ""); //去掉所有空格,包括首尾、中间
// \\s* 可以匹配空格、制表符、换页符等空白字符的其中任意一个。
str.replaceAll("\\s*", ""); //可以替换大部分空白字符, 不限于空格 ;  

JAVA and replace replaceAll are methods commonly used in the replacement character, and their difference contact
differences:
(. 1) is to replace the char and CharSequence parameters, which can support the replacement character, and also supports the replacement string (CharSequence i.e., the sequence of strings meaning, i.e. that the white string);
parameter (2) replaceAll is REGEX, i.e. rule-based alternative expressions, for example, by replaceAll ( "\ d", " *") to a string of all digital characters are replaced by asterisks;
the same point:
are replace all, that is a character or string source string replaced all the specified character or string, If you want to replace the first occurrence, you can use ReplaceFirst (), this method is also based on regular expression alternatives, but the replaceAll (different time), to replace only the first occurrence of the string;

Guess you like

Origin www.cnblogs.com/jasonboren/p/12299289.html