2010-06-22 36 views
0

如何使用C的C#.PLS strchr函數給我一些指導.....如何找到在C#strchr函數

+2

收藏此鏈接 - http://msdn.microsoft.com/en-gb/library/ms123401.aspx - 它是爲Microsoft開發人員網絡。所有文檔都有MS支持的所有語言。 – ChrisF 2010-06-22 13:04:06

回答

0

string.IndexOf就是答案。

但是,如果你是looing爲

strchr函數returns to the first occurrence of Character to be located in string, or NULL if c is not found

你可以做一些事情爲以下:

創建一個功能

 /// <summary> 
     /// Function: Strchr 
     /// Returns the first occurrence of Character to be located 
     /// in string or null otherwise 
     /// </summary> 
     /// <param name="originalString"></param> 
     /// <param name="charToSearch"></param> 
     /// <returns></returns> 
     private int? Strchr(string originalString,char charToSearch) 
     { 
      int? found = originalString.IndexOf(charToSearch); 
      return found > -1 ? found : null; 
     } 

,並調用它如下:

string test = "hello"; 
int? res = Strchr(test, '3'); //Returns null 
int? res = Strchr(test, 'h'); //Returns 0 

希望這回答你的問題。

4

string.IndexOf

+0

string.IndexOf不是strchr函數的精確替換.... – 2010-06-22 13:11:13

+1

@vishal_niist爲什麼不呢?有什麼不同嗎? – Andrey 2010-06-22 13:16:47

+0

strchr函數返回第一次出現的字符位於字符串中,或​​者如果沒有找到c,則返回NULL .... – 2010-06-22 13:27:24

0

上述答案似乎沒有100%理解strchr()的合同,並且沒有提供真正的直接替換...它通常返回一個指向從該字符(不是索引)開始的字符串的指針。

A「更純」的實施:

警告:不知道這是儘可能純,因爲它可能給人一種串用C不是指針到原來的一樣,++。可能無法獲得真正的substr指針。但我自己並不是C#專家。

/// <summary> 
    /// C++ strchr Equivalent 
    /// </summary> 
    /// Let you fill out the stupid <param ...> tags. 
    private string strchr(string originalString, char charToSearch) 
    { 
     int found = originalString.IndexOf(charToSearch); 
     return found > -1 ? originalString.Substring(found) : null; 
    } 

但我也發現了更好的B/C這個實現,你不能簡單地做C++ PTR數學+ 1的字符串,讓過去終結......

/// <summary> 
    /// C++ strchr Equivalent (with jump past character option) 
    /// </summary> 
    /// <param name="originalString"></param> 
    /// <param name="charToSearch"></param> 
    /// <param name="pastIt"></param> 
    /// <returns>String starting at the search char</returns> 
    private string strchr(string originalString, char charToSearch, bool pastIt) 
    { 
     int found = originalString.IndexOf(charToSearch); 
     return found > -1 ? originalString.Substring(found + (pastIt ? 1 : 0)) : null; 
    } 

    /// <summary> 
    /// C++ strchr Equivalent (Pure) 
    /// </summary> 
    private string strchr(string originalString, char charToSearch) 
    { 
     return strchr(originalString, charToSearch, false); 
    }