java trim() split()用法

trim()作用:

trim()方法返回调用字符串对象的一个副本,但是所有起始和结尾的空格都被删除了,例子如下:String s="    Hello World      ".trim();就是把"Hello World"放入s中。
 
split()作用
 
 
  1. String srcstring="this is a about split test";  
  2. String stringarray[]=srcstring.split(" ");  
  3.  在每个空格字符处进行分解  
  4. for(String stemp:stringarray){  
  5.     System.out.println(stemp);  
  6. }  
  7. String srcstring1=" this  is a about split  test";//有n个空格的话,分成的数组长度为n+1  
  8. //如果字符串中有多个空格时,则两个空格间认为是没有字符,结果字符串数组中该位置为空。  
  9. String stringarray1[]=srcstring1.split(" ");  
  10. for(String stemp:stringarray1){  
  11.     System.out.println(stemp);  
执行结果:
  1. this  
  2. is  
  3. a  
  4. about  
  5. split  
  6. test  
  7.   
  8. 另一个:  
  9.   
  10. this  
  11.   
  12. is  
  13. a  
  14. about  
  15. split  
  16.   
  17. test  
  1. String srcstring="this is a about split test";  
  2. String stringarray[]=srcstring.split(" ",2);  
  3.  在每个空格字符处进行分解  
  4. for(String stemp:stringarray){  
  5.        System.out.println(stemp);  
  6. }  
  7. 输出结果为  
  8. this  
  9. is a about split test  

 看看下面这个

Java代码   收藏代码
  1. String ipstring="59.64.159.224";  
  2. String iparray[]=ipstring.split("\\.");  
  3. for(String stemp:iparray){  
  4.     System.out.println(stemp);  
  5. }  
  6.  \\ 反斜杠 
    \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 
    前一个匹配的结束

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值