开发者

extract string with linq

Is there a nice way to extract part of a string with linq, exampl开发者_如何学Goe: I have

string  s = "System.Collections.*";

or

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

my goal is to extract anything in the string without the last '.*'

thankx I am using C#


The simplest way might be to use String.LastIndexOf followed by String.Substring

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

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

Unless you have a specific requirement to use LINQ for learning purposes of course.


You might want a regex instead. (.*)\.\*


With the regex:

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

output is:

"System.Collections.Somethingelse"

(because "*" is not a word) although a simple

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

would have worked :P

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜