如何检查字符串是否包含子字符串的单个出现? [英] How to check if string contains single occurence of substring?

查看:37
本文介绍了如何检查字符串是否包含子字符串的单个出现?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个:

string strings = "a b c d d e";

我需要类似于 string.Contains()的东西,但是我不仅需要知道是否存在字符串(如果在字母上方),还需要知道如果仅出现一次,则也要.

And I need something similar to string.Contains(), but I need to know not only whether a string is present(in case above a letter), but also if it is present only ONE SINGLE time.

我该如何实现?

推荐答案

您可以使用 LastIndexOf(String) IndexOf(String)并验证返回的值是否是平等的.当然,还要检查是否找到了String(即返回的值不是-1).

You can use LastIndexOf(String) and IndexOf(String) and verify that the values returned are equal. Of course also check if the String is found at all(i.e the returned value is not -1).

这篇关于如何检查字符串是否包含子字符串的单个出现?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆