【Java】关于split()函数匹配正则表达式你一定要注意的!!!

问题描述:

Java中的String类中有一个方法是split(),可以根据指定分割符号,将源字符串分割成为一个字符串数组。

6921. 按分隔符拆分字符串这道题目中,需要根据 .,|$#@ 这些分割符号来分割源字符串,看下面的测试:

public static void main(String[] args) {
        // .,|$#@

        String word1="one.two.three";
        String separator1=".";
        String[] str1 = word1.split(separator1);
        System.out.println("one.two.three:"+str1.length);

        String word2="one,two,three";
        String separator2=",";
        String[] str2 = word2.split(separator2);
        System.out.println("one,two,three:"+str2.length);

        String word3="one|two|three";
        String separator3="|";
        String[] str3 = word3.split(separator3);
        System.out.println("one|two|three:"+str3.length);

        String word4="one$two$three";
        String separator4="$";
        String[] str4 = word4.split(separator4);
        System.out.println("one$two$three:"+str4.length);

        String word5="one#two#three";
        String separator5="#";
        String[] str5 = word5.split(separator5);
        System.out.println("one#two#three:"+str5.length);

        String word6="one@two@three";
        String separator6="@";
        String[] str6 = word6.split(separator6);
        System.out.println("one@two@three:"+str6.length);
    }
// 结果:
one.two.three:0
one,two,three:3
one|two|three:13
one$two$three:1
one#two#three:3
one@two@three3
  • 发现结果不是想象中的应该为3

问题原因:
split()函数中传入的这个参数是作为正则表达式的。因为. | $是正则表达式中的元字符,而我这里是把它作为普通字符使用的。而实际传入split后. | $被当做元字符处理了,含义也就变了。

问题解决:

在使用split()函数的时候,如果要进行分割的符号为这些元字符.$|()[{^?*+\\,则要加上"\\"进行转义,否则会出现错误。

public static void main(String[] args) {
        // .,|$#@

        String word1="one.two.three";
        String separator1=".";
        String[] str1 = word1.split("\\"+separator1);
        System.out.println("one.two.three:"+str1.length);

        String word2="one,two,three";
        String separator2=",";
        String[] str2 = word2.split(separator2);
        System.out.println("one,two,three:"+str2.length);

        String word3="one|two|three";
        String separator3="|";
        String[] str3 = word3.split("\\"+separator3);
        System.out.println("one|two|three:"+str3.length);

        String word4="one$two$three";
        String separator4="$";
        String[] str4 = word4.split("\\"+separator4);
        System.out.println("one$two$three:"+str4.length);

        String word5="one#two#three";
        String separator5="#";
        String[] str5 = word5.split(separator5);
        System.out.println("one#two#three:"+str5.length);

        String word6="one@two@three";
        String separator6="@";
        String[] str6 = word6.split(separator6);
        System.out.println("one@two@three:"+str6.length);
    }
// 结果:
one.two.three:3
one,two,three:3
one|two|three:3
one$two$three:3
one#two#three:3
one@two@three3
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值