字符串性能对比

效率(1) : String.indexOf与String.contains效率测试_string contains效率-优快云博客

结论是前者效率高,源码里面conatins是使用indexof 在jdk8中contains直接调用的indexOf(其他版本没有验证),所以要说效率来说肯定是indexOf高,但contains也就多了一层方法栈,so 什么方便用什么,,没必要纠结这个

  1. /**

  2. * Returns true if and only if this string contains the specified

  3. * sequence of char values.

  4. *

  5. * @param s the sequence to search for

  6. * @return true if this string contains {@code s}, false otherwise

  7. * @since 1.5

  8. */

  9. public boolean contains(CharSequence s) {

  10. return indexOf(s.toString()) > -1;

  11. }

java大量对比字符串内容是否存在 contains和正则谁更快_string contains 性能-优快云博客

正则慢

字符串中判断存在的几种模式和效率(string.contains、string.IndexOf、Regex.Match)-优快云博客 

总结就是index优于contains优于regex 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值