通常情况下,我们判断一个字符串中是否存在某值常常会用string.contains,其实判断一个字符串中存在某值的方法有很多种,最常用的就是前述所说的string.contains,相对来说比较常用的还有string.IndexOf和Regex.Match。直接上代码,后面在说些什么吧,通常情况下功能的实现最重要,作者的话,只对有心者有效。


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions; namespace ExistsInString
{
class Program
{
static void Main(string[] args)
{
string str0 = "|456|";
string str1 = "|444|";
string str2 = "|111|222|333|444|555|666|777|888|999|000|"; //------------------------------------------
//String.Contains方法 if (str2.Contains(str0))
Console.WriteLine("String.Contains->true");
else
Console.WriteLine("String.Contains->false"); if (str2.Contains(str1))
Console.WriteLine("String.Contains->true");
else
Console.WriteLine("String.Contains->false"); //------------------------------------------
//String.IndexOf方法
int val1 = str2.IndexOf(str0);//不存在返回-1
Console.WriteLine("String.IndexOf(no exists)->" + val1);
int val2 = str2.IndexOf(str1);//存在返回str1首字符所在str2中的位置(>=0)
Console.WriteLine("String.IndexOf(exists)->" + val2); //------------------------------------------
//正则匹配方法
if (Regex.Match(str2, "[|]456[|]").Success)
Console.WriteLine("Regex.Match(no exists)->true");
else
Console.WriteLine("Regex.Match(no exists)->false"); if (Regex.Match(str2, "[|]444[|]").Success)
Console.WriteLine("Regex.Match(exists)->true");
else
Console.WriteLine("Regex.Match(exists)->false"); Console.ReadKey(); /*
*如果上述三种方式都处理大量数据,效率如何呢?
*以下循环六组数据说明
*/ int loopCount = (int)10e6;
DateTime lasttime = DateTime.Now;
DateTime nowtime = DateTime.Now; for (int loop = ; loop < ; loop++)
{
Console.WriteLine("\r\nloop " + loop + " >>>>>>>"); //------------------------------------------
//String.Contains方法 //no exists
lasttime = DateTime.Now;
for (int i = ; i < loopCount; i++)
if (str2.Contains(str0)) { };
nowtime = DateTime.Now;
TimeSpan tsStrConNoExists = nowtime - lasttime; //exists
lasttime = DateTime.Now;
for (int i = ; i < loopCount; i++)
if (str2.Contains(str1)) { };
nowtime = DateTime.Now;
TimeSpan tsStrConExists = nowtime - lasttime; //------------------------------------------
//String.IndexOf方法 //no exists
lasttime = DateTime.Now;
for (int i = ; i < loopCount; i++)
if (str2.IndexOf(str0) >= ) { };//上述已经提到不存在返回-1,存在返回一个非负整数,这里为什么不用 == -1 ,而是用了 >= 0 ,这是一个值得深思的问题?
nowtime = DateTime.Now;
TimeSpan tsStrIndNoExists = nowtime - lasttime; //exists
lasttime = DateTime.Now;
for (int i = ; i < loopCount; i++)
if (str2.IndexOf(str1) >= ) { };
nowtime = DateTime.Now;
TimeSpan tsStrIndExists = nowtime - lasttime; //------------------------------------------
//Regex.Match方法 //no exists
Regex Reg0 = new Regex("[|]456[|]");
lasttime = DateTime.Now;
for (int i = ; i < loopCount; i++)
if (Reg0.Match(str2).Success) { };
nowtime = DateTime.Now;
TimeSpan tsStrRegNoExists = nowtime - lasttime; //exists
Regex Reg1 = new Regex("[|]444[|]");
lasttime = DateTime.Now;
for (int i = ; i < loopCount; i++)
if (Reg1.Match(str2).Success) { };
nowtime = DateTime.Now;
TimeSpan tsStrRegExists = nowtime - lasttime; Console.WriteLine("no exists >>>");
Console.WriteLine("tsStrConNoExists = " + tsStrConNoExists.Milliseconds);
Console.WriteLine("tsStrIndNoExists = " + tsStrIndNoExists.Milliseconds);
Console.WriteLine("tsStrRegNoExists = " + tsStrRegNoExists.Milliseconds);
Console.WriteLine("exists >>>");
Console.WriteLine("tsStrConExists = " + tsStrConExists.Milliseconds);
Console.WriteLine("tsStrIndExists = " + tsStrIndExists.Milliseconds);
Console.WriteLine("tsStrRegExists = " + tsStrRegExists.Milliseconds);
} Console.ReadKey();
}
}
}

输入结果:

String.Contains->false
String.Contains->true
String.IndexOf(no exists)->-1
String.IndexOf(exists)->12
Regex.Match(no exists)->false
Regex.Match(exists)->true

loop 1 >>>>>>>
no exists >>>
tsStrConNoExists = 796
tsStrIndNoExists = 687
tsStrRegNoExists = 171
exists >>>
tsStrConExists = 484
tsStrIndExists = 234
tsStrRegExists = 796

loop 2 >>>>>>>
no exists >>>
tsStrConNoExists = 46
tsStrIndNoExists = 671
tsStrRegNoExists = 234
exists >>>
tsStrConExists = 546
tsStrIndExists = 437
tsStrRegExists = 734

loop 3 >>>>>>>
no exists >>>
tsStrConNoExists = 62
tsStrIndNoExists = 875
tsStrRegNoExists = 171
exists >>>
tsStrConExists = 609
tsStrIndExists = 562
tsStrRegExists = 781

loop 4 >>>>>>>
no exists >>>
tsStrConNoExists = 78
tsStrIndNoExists = 921
tsStrRegNoExists = 218
exists >>>
tsStrConExists = 609
tsStrIndExists = 640
tsStrRegExists = 828

loop 5 >>>>>>>
no exists >>>
tsStrConNoExists = 156
tsStrIndNoExists = 268
tsStrRegNoExists = 265
exists >>>
tsStrConExists = 609
tsStrIndExists = 578
tsStrRegExists = 890

loop 6 >>>>>>>
no exists >>>
tsStrConNoExists = 109
tsStrIndNoExists = 46
tsStrRegNoExists = 546
exists >>>
tsStrConExists = 625
tsStrIndExists = 609
tsStrRegExists = 953


测试结果中不难发现,如果strA中不包括strB,使用strA.Contains(strB)更优;反之,如果strA中包括strB,使用strA.IndexOf(strB)更优。(Regex.Match在此方法中貌似没有体现出任何优势,它更适用于模糊匹配)

具体要使用string.Contains,或是string.IndexOf要看形势。

之前有看过string下很多方法实现的代码(微软的,非他人),string.Contains是基于string.IndexOf上的一个方法,使用string.Contains的时候,会调用

string.IndexOf,按原理,使用string.IndexOf的效率是要高于string.Contains的,但是这个测试结果让我大跌眼镜,应该是我在上述代码中使用的判断语句造成的这种非理想的测试结果,按照个人的意愿,还是希望多使用string.IndexOf。


其实一次微小的改变在当前可能影响不了什么,但是在日积月累中,它的优势就显而易见了。想要快速变得比他人更强,不需要多么费劲,只需要每天多做一点点(千分之一)

一年之后:(1 + 0.001)365 =  1.44倍

十年之后(1 + 0.001)3650 =  38.4倍

字符串中判断存在的几种模式和效率(string.contains、string.IndexOf、Regex.Match)的更多相关文章

  1. js中判断数据类型的四种方法总结

    js中判断数据类型的四种方法 前言 在js中,我们经常需要判断数据的类型,那么哪些方法可以用来判断数据的类型呢?哪种方法判断数据类型最准确呢? 我们来一个个分析: 1.typeof typeof是一个 ...

  2. iOS xib中TableView创建的2种模式

    在xcode 5.0中 用xib编辑tableview有2种模式,见下图 其中,dynamic prototype 动态原型 表示tableview会询问它指定的 data source获取数据,如果 ...

  3. pycharm中脚本执行的3种模式(unittest框架、pytest框架、普通模式)

    背景知识,某次使用HTMLTestRunner的时候,发现一直都无法导出报告,后来查询资料发现了一些坑,现在整理一下来龙去脉. 一:pycharm默认的是pytest框架去执行unittest框架的测 ...

  4. 浏览器中的user-agent的几种模式

    服务器一般会根据访问的浏览器进行识别,针对不同浏览器才用不同的网站样式及结构,也是通过这个信息判断用户使用的平台模式(手机,pc或平板) 识别为手机一般有这几个关键字: "Windows P ...

  5. [Swift]LeetCode387. 字符串中的第一个唯一字符 | First Unique Character in a String

    Given a string, find the first non-repeating character in it and return it's index. If it doesn't ex ...

  6. LeetCode 387: 字符串中的第一个唯一字符 First Unique Character in a String

    题目: 给定一个字符串,找到它的第一个不重复的字符,并返回它的索引.如果不存在,则返回 -1. Given a string, find the first non-repeating charact ...

  7. LeetCode 1047. 删除字符串中的所有相邻重复项(Remove All Adjacent Duplicates In String)

    1047. 删除字符串中的所有相邻重复项 1047. Remove All Adjacent Duplicates In String 题目描述 LeetCode1047. Remove All Ad ...

  8. 判断字符串中是否存在的几种方案:string.indexof、string.contains、list.contains、list.any几种方式效率对比

    我们在做项目时,可能会遇到这样的需求,比如判断,1,2,3,33,22,123, 中是否存在,3,. var str=",1,2,3,33,22,123,"; 一般有几种方式: 1 ...

  9. iOS中图片动画的三种模式及基本的代码实现

    -(void)play { //第一种图片动画模式 头尾方式 //头尾方式 [UIView beginAnimations:nil context:nil];//动画开始 [UIView setAni ...

随机推荐

  1. Spring 配置 Spring JPA 发生错误的解决方法

    今天在项目的applicationContext.xml中配JPA时 <?xml version="1.0" encoding="UTF-8"?> ...

  2. easyui datagrid中关联combox

    datagrid中列上关联combobox{ field: 'SysCode', title: '系统代码', width: 150, align: 'left', editor: { type: ' ...

  3. tail queue代码阅读

    tail queue是bdb中用的最多的数据结构. 定义在 src/dbinc/queue.h: 注: TRACEBUF,QMD_TRACE_HEAD等是为了 queue代码的debug, 这里移除出 ...

  4. asp.net中实现文件下载功能

    //TransmitFile实现下载    protected void Button1_Click(object sender, EventArgs e)      {         /*     ...

  5. Online Judge中基本的输入输出

    一.利用c语言处理输入和输出 1.输入: 在C语言中输入中stdin指针表示标准的输入,scanf默认读取的就是stdin指向的输入,在acm中我们可能需要不断的测试,最好将stdin重定向到某一个文 ...

  6. java jinfo命令详解

    jinfo (configuration info): 功能:输出Java进程的系统信息与jvm参数. 摘要: jinfo [ option ] pid jinfo [ option ] execut ...

  7. 手Q兴趣号的价值在哪里

    拥有5.21亿月活跃用户,如果不做点什么东西出来,实在是浪费至极.如此庞大的用户量,如果能够将内容贡献出来,那将是恐怖的,QQ空间产品就是很好的佐证. QQ群让个体用户能够连接在一起,单个的用户关系链 ...

  8. 可在广域网部署运行的QQ高仿版 -- GG叽叽V3.2,增加离线消息、离线文件功能(源码)

    (几句题外话:虽然就如何将GG发展为一个有商业价值的产品,我还没有很清晰明确的思路,但是从GG发布以来,通过GG认识了一些朋友,也接了一些小单子,赚了一点小钱.有了一点甜头,目前和2.3个好朋友一起做 ...

  9. 可在广域网部署运行的QQ高仿版 -- GG叽叽V1.8(源码)

    距离的GG 1.0发布已经三周了,这三周内,我利用业余时间为GG增加了视频聊天的功能.个人觉得进展有些缓慢,主要是因为大多数时间都花在了UI上.由于本人不会PS,所以图片素材都是从网上一个一个搜下来的 ...

  10. UWP的一种下拉刷新实现

    简介 我们最近实现了一个在UWP中使用的下拉刷新功能,以满足用户的需求,因为这是下拉刷新是一种常见的操作方式,而UWP本身并不提供这一机制. 通过下拉刷新这一机制,可以让移动端的界面设计变得更加简单, ...