2011-01-08 31 views
1

有一個很好的方法來提取使用LINQ,例如一個字符串的一部分: 我提取字符串使用LINQ

string s = "System.Collections.*"; 

string s2 = "System.Collections.Somethingelse.*"; 

我的目標是字符串中提取任何東西沒有最後 '。*'

thankx我使用C#

回答

1

最簡單的方法可能是除非你有使用LINQ學習課程的目的,具體的規定使用String.LastIndexOf隨後String.Substring

int index = s.LastIndexOf('.'); 

string output = s.Substring(0, index); 

0

您可能需要一個正則表達式。 (.*)\.\*

+0

丹尼爾我想你的正則表達式,但什麼都沒有改變。也許我做錯了。 – user282807 2011-01-08 22:43:42

+0

您應該在適當的位置添加\,或者在C#中使用@ infront字符串。 – 2017-10-13 01:37:44

0

隨着正則表達式:

string input="System.Collections.Somethingelse.*"; 
string output=Regex.Matches(input,@"\b.*\b").Value; 

輸出爲:

"System.Collections.Somethingelse" 

(因爲 「*」 不是一個單詞),但一個簡單的

output=input.Replace(".*",""); 

會工作:P