2

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#

3 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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

2 Comments

Daniel I tried your regex but nothing changed. Maybe i did it wrong.
you should add additional \ to where appropriate, or if in C# use @ infront the string.
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.