2013-02-14 12 views
5

Ho una lista di stringhe in questo modo:C# Get sottostringa con il modello specifico da stringa

List<string> list = new List<string>(); 
list.Add("Item 1: #item1#"); 
list.Add("Item 2: #item2#"); 
list.Add("Item 3: #item3#"); 

Come posso ottenere e aggiungere le stringhe # BENE1 #, # # item2 ecc in una nuova lista?

io sono in grado di ottenere la stringa completa se contiene un "#" in questo modo solo:

foreach (var item in list) 
{ 
    if(item.Contains("#")) 
    { 
     //Add item to new list 
    } 
} 
+0

qualcosa con queste funzioni: substring (FirstIndexOf ('#'), LastIndexOf ('#')); – Karl

+1

Cosa restituire se la stringa non contiene la parte '# articolo #'? –

risposta

8

si potrebbe avere uno sguardo a Regex.Match. Se conosci un po 'di espressioni regolari (nel tuo caso sarebbe un modello abbastanza semplice: "#[^#]+#"), puoi usarlo per estrarre tutti gli elementi che iniziano e finiscono con '#' con un numero qualsiasi di altri caratteri diverso da '#' in mezzo.

Esempio:

Match match = Regex.Match("Item 3: #item3#", "#[^#]+#"); 
if (match.Success) { 
    Console.WriteLine(match.Captures[0].Value); // Will output "#item3#" 
} 
0

ne dite di questo:

List<string> substring_list = new List<string>(); 
foreach (string item in list) 
{ 
    int first = item.IndexOf("#"); 
    int second = item.IndexOf("#", first); 
    substring_list.Add(item.Substring(first, second - first); 
} 
0

Si potrebbe fare che semplicemente usando:

List<string> list2 = new List<string>(); 
    list.ForEach(x => list2.Add(x.Substring(x.IndexOf("#"), x.Length - x.IndexOf("#")))); 
0

provare questo.

var itemList = new List<string>(); 
foreach(var text in list){ 
string item = text.Split(':')[1]; 
itemList.Add(item); 


} 
1

LINQ farebbe il lavoro bene:

var newList = list.Select(s => '#' + s.Split('#')[1] + '#').ToList(); 

O se preferite espressioni di query:

var newList = (from s in list 
       select '#' + s.Split('#')[1] + '#').ToList(); 

In alternativa, è possibile utilizzare le espressioni regolari come suggerito con Botz3000 e combinare quelle con LINQ:

var newList = new List(
    from match in list.Select(s => Regex.Match(s, "#[^#]+#")) 
    where match.Success 
    select match.Captures[0].Value 
); 
1

Il codice risolverà il tuo problema. Ma se la stringa non contiene#item#, verrà utilizzata la stringa originale.

var inputList = new List<string> 
    { 
     "Item 1: #item1#", 
     "Item 2: #item2#", 
     "Item 3: #item3#", 
     "Item 4: item4" 
    }; 

var outputList = inputList 
    .Select(item => 
     { 
      int startPos = item.IndexOf('#'); 
      if (startPos < 0) 
       return item; 

      int endPos = item.IndexOf('#', startPos + 1); 
      if (endPos < 0) 
       return item; 
      return item.Substring(startPos, endPos - startPos + 1); 
     }) 
    .ToList(); 
2

Ecco un altro modo di utilizzare un regex con LINQ. (Non sei sicuro che i tuoi requisiti esatti facciano riferimento alla regex, quindi ora potresti avere due problemi.)

var list = new List<string>() 
{ 
    "Item 1: #item1#", 
    "Item 2: #item2#", 
    "Item 3: #item3#", 
    "Item 4: #item4#", 
    "Item 5: #item5#", 
}; 

var pattern = @"#[A-za-z0-9]*#"; 

list.Select (x => Regex.Match (x, pattern)) 
    .Where (x => x.Success) 
    .Select (x => x.Value) 
    .ToList() 
    .ForEach (Console.WriteLine); 

uscita:

# voce1 #

# item2 #

# item3 #

# item4 012.352.951,646 mila

# item5 #

Problemi correlati