怎么判断字符串a的内容包含字符串b的内容

在vb.net可以使用InStr(a,b)方法来判断字符串a的内容是否包含字符串b的内容。

InStr(a, b)

如果a中包含有b则InStr(a, b) 返回一个大于0的值,如果不含有b则返回0。可以通过返回值来判断。

 

c#中可以使用 IndexOf语句

 

public class Example
{
   public static void Main()
   {
      string s1 = "ani\u00ADmal";
      string s2 = "animal";

      // Find the index of the soft hyphen.
      Console.WriteLine(s1.IndexOf("\u00AD"));
      Console.WriteLine(s2.IndexOf("\u00AD"));

      // Find the index of the soft hyphen followed by "n".
      Console.WriteLine(s1.IndexOf("\u00ADn"));
      Console.WriteLine(s2.IndexOf("\u00ADn"));

      // Find the index of the soft hyphen followed by "m".
      Console.WriteLine(s1.IndexOf("\u00ADm"));
      Console.WriteLine(s2.IndexOf("\u00ADm"));
   }
}

 

输出结果:

3
-1
-1
-1
3
-1

 

a.IndexOf(b),如果a中含有b字符串则,返回一个大于0的值,这个值是b首个字符在a字符串中的位置(从0开始计数)。如果a字符串不包含有b的话则返回的值为-1.

 

转载于:https://www.cnblogs.com/blackcatcjy/p/3885706.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值