C# 字符串比较方法返回第一个不匹配的索引
是否存在现有的字符串比较方法,该方法将根据两个字符串之间第一次出现的不匹配字符返回一个值?
即
string A = "1234567890"
string B = "1234567880"
我想返回一个值,该值允许我看到匹配中断的第一次出现是 A[8]
Is there an exsting string comparison method that will return a value based on the first occurance of a non matching character between two strings?
i.e.
string A = "1234567890"
string B = "1234567880"
I would like to get a value back that would allow me to see that the first occurance of a matching break is A[8]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
如果您安装了 .net 4.0,这可能是一种方式:
编辑:
不过,如果您需要该位置:
If you have .net 4.0 installed, this could be a way:
edit:
Though, if you need the position:
如下所示的扩展方法可以完成这项工作:
An extension method along the lines of the below would do the job:
据我所知,但这非常简单:
这是一个简单的序数比较。序数不区分大小写的比较是一个简单的更改,但文化基础很难定义; “Weißbier”与第二个字符串中最后一个 S 上的“WEISSBIERS”不匹配,但这算作位置 8 还是位置 9?
Not that I know of, but it's pretty trivial:
This is a simple ordinal comparison. Ordinal case-insensitive comparison is an easy change, but culture-base is tricky to define; "Weißbier" mismatches "WEISSBIERS" on the final S in the second string, but does that count as position 8 or position 9?
本主题与比较字符串重复并获得彼此不同的第一个位置,其中包含使用 Linq 的更好的单行解决方案
This topic is a duplicate of Comparing strings and get the first place where they vary from eachother, which contains a better, one line, solution using Linq
可以编写字符串扩展,如
Use it like
它不是查找不匹配的优化算法。
如果您只想找到第一个不匹配的地方,
It possible to write string extension like
Use it like
It is not an optimized algorithm for finding the mismatch.
If you are interested only to find the the first mismatch,
我的两分钱。
这里只是一个循环,比较两个字符串,一个名为 ExpectedHexStringParameterValue,另一个名为 defaultHexStringParVal。循环因第一个不匹配而中断。
My two cents.
Here just a loop comparing the two strings one called ExpectedHexStringParameterValue and the other called defaultHexStringParVal. The loop just break with the first mismatch.