在C#中怎么知道一个字符或是一个字符串存在于另一个字符串中
例如

in
i 在 in 字符串中有这个怎么判断或是怎么得知?

解决方案 »

  1.   

    // This example demonstrates the String.Contains() method
    using System;class Sample 
    {
        public static void Main() 
        {
        string s1 = "The quick brown fox jumps over the lazy dog";
        string s2 = "fox";
        bool b;
        b = s1.Contains(s2);
        Console.WriteLine("Is the string, s2, in the string, s1?: {0}", b);
        }
    }
    /*
    This example produces the following results:Is the string, s2, in the string, s1?: True
    */
      

  2.   

       查找字串中指定字符或字串首次出现的位置,返首索引值,如: 
      str1.IndexOf("字"); //查找“字”在str1中的索引值(位置) 
      str1.IndexOf("字串");//查找“字串”的第一个字符在str1中的索引值(位置) 
      str1.IndexOf("字",start,end);//从str1第start+1个字符起,查找end个字符,查找“字”在字符串STR1中的位置[从第一个字符算起]注   意:start+end不能大于str1的长度
      

  3.   

    string a ="i";
    string b ="in";
    if (b.IndexOf(a)> -1)
    {

    }
    else 
    {
    没有
    }