extraer cadena con linq

Is there a nice way to extract part of a string with linq, example: I have

string  s = "System.Collections.*";


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

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

thankx I am using C#

preguntado el 08 de enero de 11 a las 22:01

3 Respuestas

The simplest way might be to use String.LastIndexOf seguido por 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.

Respondido el 09 de enero de 11 a las 01:01

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

Respondido el 09 de enero de 11 a las 01:01

Daniel I tried your regex but nothing changed. Maybe i did it wrong. - user282807

you should add additional \ to where appropriate, or if in C# use @ infront the string. - Kugan Kumar

With the regex:

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

la salida es:


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


would have worked :P

Respondido el 14 de diciembre de 12 a las 14:12

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.