如何识别一个字符串是否包含一个以上特定字符的实例? [英] How to identify if a string contains more than one instance of a specific character?
本文介绍了如何识别一个字符串是否包含一个以上特定字符的实例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想检查一个字符串中是否包含多个字符?如果我有一个字符串12121.23.2,那么我想检查它是否包含多个字符串.在字符串中.
I want to check if a string contains more than one character in the string? If i have a string 12121.23.2 so i want to check if it contains more than one . in the string.
推荐答案
您可以将 IndexOf
与 LastIndexOf
进行比较,以检查 string
而不进行显式计数:
You can compare IndexOf
to LastIndexOf
to check if there is more than one specific character in a string
without explicit counting:
var s = "12121.23.2";
var ch = '.';
if (s.IndexOf(ch) != s.LastIndexOf(ch)) {
...
}
这篇关于如何识别一个字符串是否包含一个以上特定字符的实例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文