Java split()方法实现切割字符串

补充一下知识点:

1、String的split()方法中传的参数支持正则表达式

2、split()方法的返回的结果是String型数组

3、关于正则表达式一切特殊例子:

     字符"|",",","."都得加上转义字符,前面加上"\\",如果是"\",那么就得写成"\\\\"。

     如果一个字符串中有多个分隔符,可以用"|"作为连字符。

     正则表达是\s表示匹配任何空白符,+表示一次或多次。

示例代码:

package splitDemo;

/**
 * 简单测试split方法
 * Created by huangwei on 17-8-3.
 */
public class Split {
    public static void main(String[] args) {
        //切割一个url
        String url = "https://www.baidu.com/s?ie=utf-8&f=3&rsv_bp=1&rsv_idx=1&tn=baidu&wd=idea";
        //split方法切割后的字符串会存入数组中
        String[] words = url.split("[: . / ? = & _]");
        //遍历数组
        for (String word:words){
            System.out.println(word);
        }

        String str = "No man or woman is worth your tears, and the one who is, won't make you cry.";
        //,.转换成空格 ,.需要转义加\\
        String strall = str.replaceAll("\\,|\\."," ");
        //正则表达式\s表示匹配任何空白字符,+表示匹配一次或多次。
        String[] strs= strall.split("\\s+");
        //遍历数组
        for (String s:strs){
            System.out.println(s);
        }
    }
}



评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值