trim()作用:
trim()方法返回调用字符串对象的一个副本,但是所有起始和结尾的空格都被删除了,例子如下:String s=" Hello World ".trim();就是把"Hello World"放入s中。
split()作用
- String srcstring="this is a about split test";
- String stringarray[]=srcstring.split(" ");
- 在每个空格字符处进行分解
- for(String stemp:stringarray){
- System.out.println(stemp);
- }
- String srcstring1=" this is a about split test";//有n个空格的话,分成的数组长度为n+1
- //如果字符串中有多个空格时,则两个空格间认为是没有字符,结果字符串数组中该位置为空。
- String stringarray1[]=srcstring1.split(" ");
- for(String stemp:stringarray1){
- System.out.println(stemp);
执行结果:
- this
- is
- a
- about
- split
- test
- 另一个:
- this
- is
- a
- about
- split
- test
- String srcstring="this is a about split test";
- String stringarray[]=srcstring.split(" ",2);
- 在每个空格字符处进行分解
- for(String stemp:stringarray){
- System.out.println(stemp);
- }
- 输出结果为
- this
- is a about split test
看看下面这个
- String ipstring="59.64.159.224";
- String iparray[]=ipstring.split("\\.");
- for(String stemp:iparray){
- System.out.println(stemp);
- }
- \\ 反斜杠
\t 间隔 ('\u0009')
\n 换行 ('\u000A')
\r 回车 ('\u000D')
\d 数字 等价于 [0-9]
\D 非数字 等价于 [^0-9]
\s 空白符号 [\t\n\x0B\f\r]
\S 非空白符号 [^\t\n\x0B\f\r]
\w 单独字符 [a-zA-Z_0-9]
\W 非单独字符 [^a-zA-Z_0-9]
\f 换页符
\e Escape
\b 一个单词的边界
\B 一个非单词的边界
\G 前一个匹配的结束