周末了,起得晚。爬起来,洗漱完毕打开电脑,习惯性的收MAIL,第一封就吸引了我,标题:
老兄,我发现了.NET里面string.LastIndexOfAny的一个逻辑性错误的BUG
我的第一反应: 这位兄弟又joking了 ,打开正文,一段再简单不过的代码:
string str = "+-这是一个综合使用确定子串位置的C#示例+-";
int iLastPos = str.LastIndexOfAny("+-".ToCharArray(), 0);
Console.WriteLine(iLastPos);
Console.ReadKey(true);
最后是问我,结果等于多少, 为什么?
我看了一下,然后就想当然的认为,这也太简单了,不就是最后一位嘛, str.Length – 1 呗! 不过,为了表示对这位兄弟的尊重,我还是运行了一下这个代码,显示的结果,让我大为surprise: 0
Why? ,查之!!
LastIndexOfAny:
报告在 Unicode 数组中指定的一个或多个字符在此实例中的最后一个匹配项的索引位置
Reports the index position of the last occurrence in this instance of one or more characters specified in a Unicode array.
重载1: String.LastIndexOfAny(char[] anyOf)
-------------------------------------------------------------------
参数
anyOf
Unicode 字符数组,包含一个或多个要查找的字符。
A Unicode character array containing one or more characters to seek.
重载2:String.LastIndexOfAny ( char[] anyOf, int startIndex)
------------------------------------------------------------------------------------
参数
anyOf
Unicode 字符数组,包含一个或多个要查找的字符。
A Unicode character array containing one or more characters to seek.
startIndex
搜索起始位置。
The search starting position.
重载3: 和2类似,我就略掉了
返回值
此实例中最后一个匹配项的索引位置,在此位置找到 anyOf 中的任意字符;否则,如果未找到 anyOf 中的字符,则为 -1。
The index position of the last occurrence in this instance where any character in anyOf was found; otherwise, -1 if no character in anyOf was found.
备注
索引编号从零开始。
此方法从此实例的 startIndex 字符位置开始,从后向前进行搜索,直到找到 anyOf 中的一个字符或检查到第一个字符位置。该搜索区分大小写。
Index numbering starts from zero.
This method begins searching at the last character position of this instance and proceeds backward toward the beginning until either a character in anyOf is found or the first character position has been examined. The search is case-sensitive.
我上面加红的地方,是关键点,因为是 求LAST,所以逆向效率高,这是无可厚非的,问题就出在这里了,逆向搜索
重载2中,对startIndex 这个参数的说明是
startIndex
搜索起始位置。
The search starting position
这就是说startIndex 是按正向的顺序来表示的,那就一目了然了,回到最初的问题:
string str = "+-这是一个综合使用确定子串位置的C#示例+-";
int iLastPos = str.LastIndexOfAny("+-".ToCharArray(), 0);
Console.WriteLine(iLastPos);
Console.ReadKey(true);
上面的代码中, 将 startIndex 设置为 0, 他的本意是搜索整个字符串!
但是因为 LastIndexOfAny 是逆向搜索的, 结果就成了 从 第一个 字符开始,向左搜索,结果就是匹配了第一个字符,返回了 0
如果要搜索整个字符串,正确的写法是
string str = "+-这是一个综合使用确定子串位置的C#示例+-";
int iLastPos = str.LastIndexOfAny("+-".ToCharArray(), str.Length - 1);
Console.WriteLine(iLastPos);
Console.ReadKey(true);
其实,如果看过 MSDN 上,关于 LastIndexOfAny 的那个sample的话,就一目了然了
LastIndexOf 和 LastIndexOfAny 是一样的逻辑,就不重复了
分享到:
相关推荐
一、IndexOf/LastIndexOf IndexOf方法用于搜索在一个字符串中,某个特定的字符或者子串第一次出现的位置,该方法区分大小写,并从字符串的首字符开始以0计数。如果字符串中不包含这个字符或子串,则返回-1。常用的...
C#中String类的几个方法(IndexOf、LastIndexOf、Substring)
c#函数大全Compare CompareOrdinal Format IndexOf IndexOfAny LastIndexOf LastIndexOfAny PadLeft PadRight Replace
序中判断是否为数字的方法有很多种,可以使用正则表达式、int.Parse 方法和double.Parse 方法等。下面的代码 通过double.Parse 方法判断textBox1 文本框中的输入是否为数字。 double.Parse(textBox1.Text); 实例573 ...
这款音乐播放器基于VB.NET编写,实现了以下功能:播放位置随着滑块的拖动而实时变化,是否静音播放的功能,把跟踪条值赋给MediaPlayer1控件的音量设置值,播放或暂停的功能,打开要播放的媒体文件的功能,实时(每隔...
用VS编写的FTP服务器软件,C#网络程序编程学习用。 代码: using System; using System.Collections.Generic; using System.Globalization; using System.IO; using System.Net; using System.Net.Sockets; using ...
System.Net.WebClient webClientObj = new System.Net.WebClient(); if (parms != null) { serverURL = serverURL + "?"; foreach (string key in parms.Keys) { serverURL = serverURL + key + "=" + ...
public void saveAs(string filename) {switch (filename.Substring(filename.LastIndexOf('.'))) { case ".jpg": gdiImg.Save(filename, System.Drawing.Imaging.ImageFormat.Jpeg); break; case ".bmp": ...
C#栈和队列的应用实例源码 private void button1_Click(object sender, EventArgs e) { if (textBox3.Text.Length > 0) { if (IsIDnum(textBox3.Text.Trim())) { int i = Convert.ToInt32(textBox3.Text....
string fileName = fileNamePath.Substring(fileNamePath.LastIndexOf("\\") + 1); NewFileName = DateTime.Now.ToString("yyMMddhhmmss") + DateTime.Now.Millisecond.ToString() + fileNamePath.Substring...
《C#全能速查宝典》共分为8章,分别介绍了C#语言基础、Windows窗体及常用控件、Windows高级控件、控件公共属性、方法及事件、数据库开发、文件、数据流与注册表、GDI+绘图技术和C#高级编程,共包含562个C#编程中常用...
符合ES2015规范的shim / polyfill / replacement的Array.prototype.lastindexof ,可向下使用到ES3。 该软件包实现了接口。 它可以在ES3支持的环境中工作并符合。 因为Array.prototype.lastIndexOf取决于接收方...
INSTR的第三个参数为1时,实现的是indexOf功能。... 您可能感兴趣的文章:asp.net的IndexOf,LastIndexOf,IndexOfAny和LastIndexOfAny的用法javascript Split方法,indexOf方法、lastIndexOf 方法和subst
用c#编写的简易计算器 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows....
一但指定的字被找到,就会返回这个字的当前的位置号码。如果没有找到就返回 -1. var str = //www.stooges.com.my/test/index.aspx123/; console.log(str.indexOf(/)); //0 console.log(str.lastIndexOf(/)); //39 ...
this.m_clientName = m_temp.Substring(m_temp.IndexOf("\"")+1,m_temp.LastIndexOf("\"")-m_temp.IndexOf("\"")-1).Trim(); m_temp = contentArray[2]; this.m_filename = m_temp.Substring(m_temp...
http://sourceforge.net/projects/fckeditor/ 主要开发步骤: 1.下载官方包(工程src下的zip为目前最新版本) 2.导入对于开发语言需要的一些文件 3.页面引用,如: <!-- FCKEditor --> <link href="<%=basePath %>...
String filePath = (middleFilePath.substring(0, middleFilePath.lastIndexOf("\\"))).substring(0, (middleFilePath.substring(0, middleFilePath.lastIndexOf("\\"))).lastIndexOf("\\")); String fileName = ...
Array.prototype.lastIndexOf 和 String.prototype.lastIndexOf 是非常的实用的方法,不过很多人不知道它其实可以传递两个参数,第二个参数决定了搜索的起始位置: 语法 str.lastIndexOf(searchValue[, fromIndex]...