2011-03-17 70 views

回答

4

如果你有字符串:

string sample = "If you know what's good for you, you'll shut the door!"; 

而且要找到它是在一個句子,你可以使用indexOf方法。

int index = sample.IndexOf("shut the door"); 
// index will be 42 

非-1答案表示該字符串已定位。 -1表示它不存在於字符串中。請注意,搜索字符串(「關上門」)區分大小寫。

0
if (string1.indexOf(string2) >= 0) 
    ... 
0

空格沒什麼特別的,它們只是字符,所以你可以找到像這樣的字符串,就像你會找到任何其他字符串在你的句子中,例如,如果你需要的位置使用「indexOf」,或者只是「Contains 「如果你需要知道它是否存在。

E.g.

string sentence = "foo bar baz"; 
    string phrase = "bar baz"; 

    Console.WriteLine(sentence.Contains(phrase)); // True 
0

下面是一些C#代碼使用開始串和結束串點,以找到一個子,但你可以作爲一個基礎使用和修改(即刪除不需要端線)只是找到你的字符串... 2個版本,一個只是找到一個子字符串的第一個實例,另一個返回一個子字符串的所有起始位置和實際字符串的字典。

public Dictionary<int, string> GetSubstringDic(string start, string end, string source, bool includeStartEnd, bool caseInsensitive) 
{ 
    int startIndex = -1; 
    int endIndex = -1; 
    int length = -1; 
    int sourceLength = source.Length; 
    Dictionary<int, string> result = new Dictionary<int, string>(); 

    try 
    { 
     //if just want to find string, case insensitive 
     if (caseInsensitive) 
     { 
      source = source.ToLower(); 
      start = start.ToLower(); 
      end = end.ToLower(); 
     } 

     //does start string exist 
     startIndex = source.IndexOf(start); 
     if (startIndex != -1) 
     { 
      //start to check for each instance of matches for the length of the source string 
      while (startIndex < sourceLength && startIndex > -1) 
      { 
       //does end string exist? 
       endIndex = source.IndexOf(end, startIndex + 1); 
       if (endIndex != -1) 
       { 
        //if we want to get length of string including the start and end strings 
        if (includeStartEnd) 
        { 
         //make sure to include the end string 
         length = (endIndex + end.Length) - startIndex; 
        } 
        else 
        { 
         //change start index to not include the start string 
         startIndex = startIndex + start.Length; 
         length = endIndex - startIndex; 
        } 
        //add to dictionary 
        result.Add(startIndex, source.Substring(startIndex, length)); 
        //move start position up 
        startIndex = source.IndexOf(start, endIndex + 1); 
       } 
       else 
       { 
        //no end so break out of while; 
        break; 
       } 
      } 
     } 
    } 
    catch (Exception ex) 
    { 
     //Notify of Error 
     result = new Dictionary<int, string>(); 
     StringBuilder g_Error = new StringBuilder(); 
     g_Error.AppendLine("GetSubstringDic: " + ex.Message.ToString()); 
     g_Error.AppendLine(ex.StackTrace.ToString()); 
    } 
    return result; 
} 

public string GetSubstring(string start, string end, string source, bool includeStartEnd, bool caseInsensitive) 
{ 
    int startIndex = -1; 
    int endIndex = -1; 
    int length = -1; 
    int sourceLength = source.Length; 
    string result = string.Empty; 

    try 
    { 
     if (caseInsensitive) 
     { 
      source = source.ToLower(); 
      start = start.ToLower(); 
      end = end.ToLower(); 
     } 

     startIndex = source.IndexOf(start); 
     if (startIndex != -1) 
     { 
      endIndex = source.IndexOf(end, startIndex + 1); 
      if (endIndex != -1) 
      { 
       if (includeStartEnd) 
       { 
        length = (endIndex + end.Length) - startIndex; 
       } 
       else 
       { 
        startIndex = startIndex + start.Length; 
        length = endIndex - startIndex; 
       } 
       result = source.Substring(startIndex, length); 
      } 
     } 
    } 
    catch (Exception ex) 
    { 
     //Notify of Error 
     result = string.Empty; 
     StringBuilder g_Error = new StringBuilder(); 
     g_Error.AppendLine("GetSubstring: " + ex.Message.ToString()); 
     g_Error.AppendLine(ex.StackTrace.ToString()); 
    } 
    return result; 
} 
0

您可能想要確保檢查忽略兩個短語的情況。

string theSentence = "I really want you to shut the door."; 
string thePhrase = "Shut The Door"; 

bool phraseIsPresent = theSentence.ToUpper().Contains(thePhrase.ToUpper()); 
int phraseStartsAt = theSentence.IndexOf(
    thePhrase, 
    StringComparison.InvariantCultureIgnoreCase); 

Console.WriteLine("Is the phrase present? " + phraseIsPresent); 
Console.WriteLine("The phrase starts at character: " + phraseStartsAt); 

此輸出:

Is the phrase present? True 
The phrase starts at character: 21 
相關問題