Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 8th, 2012  |  syntax: None  |  size: 0.92 KB  |  hits: 17  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. remove quote from list<string> using linq
  2. List<string> strings = new List<string>();
  3. strings.Add("'Value1");
  4. strings.Add("'Values2 This 2nd ' should stay");
  5.        
  6. strings.ForEach(s => s = s.TrimStart('''));
  7.        
  8. List<string> strings = new List<string>();
  9. strings.Add("'Value1");
  10. strings.Add("'Values2 This 2nd ' should stay");
  11.  
  12. Console.WriteLine("Before:");
  13. foreach (string s in strings) {
  14.     Console.WriteLine(s);
  15. }
  16.  
  17. strings.ForEach(s => s = s.TrimStart('''));
  18.  
  19. Console.WriteLine();
  20. Console.WriteLine("After:");
  21. foreach (string s in strings) {
  22.     Console.WriteLine(s);
  23. }
  24. Console.ReadKey();
  25.        
  26. Before:
  27. 'Value1
  28. 'Values2 This 2nd ' should stay
  29.  
  30. After:
  31. 'Value1
  32. 'Values2 This 2nd ' should stay
  33.        
  34. strings = strings.Select(x => x.StartsWith("'") ? x.Substring(1) : x).ToList();
  35.        
  36. strings.Select(s => s.StartsWith("'") ? s.Substring(1) : s);
  37.        
  38. var result = strings.Select(s => s.TrimStart('''));
  39.        
  40. strings.Add("'Value1".TrimStart('''));