C#中查询字符串中是否包含指定字符/串,使用IndexOf还是Contains?
C#中查询字符串中是否包含指定字符/串,使用IndexOf还是Contains?这是一个很常见的命题,以前也没有注意,今天QQ群里有人提起,于是就做了下试验,代码如下:
using System;
using System.Diagnostics; namespace ConsoleApplication1
{
class Program
{
private const int N = 10000000;
private static Stopwatch watch = new Stopwatch();
static void Main(string[] args)
{ string source = "abcdefghijklmnopqrstuvwxyz0123456789C#"
+ "中查询字符串中是否包含指定字符/串,使用IndexOf还是Contains?.uonun";
string target = "a";
Console.WriteLine("目标为第一个字符时:");
TestContains(source, target);
TestIndexOf(source, target);
Console.WriteLine(); Console.WriteLine("目标为中部某个字符时:");
target = "中";
TestContains(source, target);
TestIndexOf(source, target);
Console.WriteLine(); Console.WriteLine("目标为最后一个字符时:");
target = "u";
TestContains(source, target);
TestIndexOf(source, target); Console.WriteLine("执行完毕,按任意键退出...");
Console.ReadKey(); }
private static void TestIndexOf(string source, string target)
{
watch.Reset();
watch.Start();
for (int i = 0;i < N;i++)
{
source.IndexOf(target);
}
watch.Stop();
Console.WriteLine("IndexOf: " + watch.ElapsedMilliseconds.ToString() + "ms");
return;
} private static void TestContains(string source, string target)
{
watch.Reset();
watch.Start();
for (int i = 0;i < N;i++)
{
source.Contains(target);
}
watch.Stop();
Console.WriteLine("Contains: " + watch.ElapsedMilliseconds.ToString() + "ms");
return;
}
}
}
得到的结果是:
目标为第一个字符时:
Contains: 973ms
IndexOf: 1343ms
目标为中部某个字符时:
Contains: 1813ms
IndexOf: 8602ms
目标为最后一个字符时:
Contains: 1433ms
IndexOf: 5094ms
执行完毕,按任意键退出...
可以看出,使用Contains方法的效率比IndexOf的效率高很多。
C#中查询字符串中是否包含指定字符/串,使用IndexOf还是Contains?的更多相关文章
- js查询字符串是否包含指定字符的函数indexOf
今天用到了js的函数indexOf来查看字符串中是否包含指定的字符,最开始没注意看,就拿来用了,结果用的时候才发现,IndexOf的返回值原来是字符在字符串中的index,返回值有0.1等等,如果没有 ...
- jQuery动态添加的元素中处理字符串溢出后在指定字符数后添加省略号
"+[jsonData[i].questitle.lenth>40?jsonData[i].questitle.substring(0,40)+"...":json ...
- 你如何获取浏览器URL中查询字符串中的参数?
测试地址为:http://www.runoob.com/jquery/misc-trim.html?channelid=12333&name=xiaoming&age=23 实例如下: ...
- 请用js写一个函数,实现获取浏览器url中查询字符串中的参数并返回一个数组
<script> console.log(getUrlArr()); function getUrlArr() { var arr = []; var url = "http:/ ...
- 获取浏览器URL中查询字符串中的参数
//http://www.runoob.com/index.html?name=xiaoming&age=23function showWindowHref(){ var sHref = wi ...
- 【错误】jsp查询字符串中空格导致的异常问题!
jsp中查询字符串中空格问题 jsp中查询字符串中参数名的等号右边最好不要出现空格,因为编译器会把他当做是参数值得一部分. 例如: <a href="adjust.jsp?number ...
- SQL中判断字符串中包含字符的方法
通过2个函数CHARINDEX和PATINDEX以及通配符的灵活使用 函数:CHARINDEX和PATINDEX CHARINDEX:查某字符(串)是否包含在其他字符串中,返回字符串中指定表达式的起始 ...
- 如何获取浏览器URL中查询字符串的参数?
如何获取浏览器URL中查询字符串的参数? 想要知道怎样解决这个问题,首先我们先认识一下Location对象. Location对象包含了当前页面与位置(url)相关的信息 URL示例:http://w ...
- 如何获取浏览器URL中查询字符串的参数
首先要知道Location这个对象以及这个对象中的一些属性: href:设置或返回完整的url.如本博客首页返回http://www.cnblogs.com/wymninja/ host:设置或返回主 ...
随机推荐
- L129
Iraq Sees Spike in Water-Borne IllnessesIraqi health officials say that a health crisis stemming fro ...
- oracle 存储过程心得2
1.退出存储过程 return if old_save_time = new_save_time then--没有最新数据,退出 insert into hy_data_handle_mark(id, ...
- ng 实现插入和删除
结果: 代码: <!DOCTYPE html> <html ng-app="myApp"> <head lang="en"> ...
- hashlib摘要算法模块,logging日志,configparser配置文件模块
一.hashlib模块(摘要算法模块) 1.算法介绍 Python的hashlib提供了常见的摘要算法,如MD5,SHA1等等. 什么是摘要算法呢? 摘要算法又称哈希算法.散列算法.它通过一个函数,把 ...
- Django初体验——搭建简易blog
前几天在网上看到了篇采用Django搭建简易博客的视频,好奇心驱使也就点进去学了下,毕竟自己对于Django是无比敬畏的,并不是很了解,来次初体验. 本文的操作环境:ubuntu.python2.7. ...
- POJ1160 Post Office (四边形不等式优化DP)
There is a straight highway with villages alongside the highway. The highway is represented as an in ...
- Javascript的异常捕获机制
這個異常處理機制,之前也只是,但是沒有怎麼用過,用了一次后發現還可以, 和java,python的異常處理機制也很相似 Javascript的异常捕获机制 1.1 基本的try…catch语句 ES3 ...
- 【转】C# Socket编程(5)使用TCP Socket
[转自:https://www.cnblogs.com/IPrograming/archive/2012/10/18/CSharp_Socket_5.html] TCP 协议(Transmission ...
- CODEVS4650 破损的键盘
传送门 题目大意:一个字符串,将[]内的字符提前. 题解:链表,数组元素高效交换 cur表示目前元素插入下标为cur的元素后面. 所以,假设目前把下标为i的元素插到cur后面. 那么,next[i]= ...
- 【LeetCode】129. Sum Root to Leaf Numbers
Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number ...