Reputation: 925
Is there a nice way to extract part of a string with linq, example: 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#
Upvotes: 2
Views: 1353
Reputation: 339
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
Upvotes: 0
Reputation: 137128
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.
Upvotes: 1