怎么判断字符串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 = "aniu00ADmal";
      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