0

How can I add Multiple var in this code?

var trimChars = "ab";

sample I want to add:

"ab", "as", "Ab", "As"

is that possible?

further more heres some of my code:

if (e.KeyCode == Keys.Enter)
         {
    string Slb = lb.SelectedText.ToString();
             var trimChars = "ab";
             var trimmed = Slb.TrimStart(trimChars.ToCharArray());
             rtb.SelectedText = trimmed;

             lb.Hide();
         }

thanks in advance and sorry for this newbie question :) .more power!

  • I think you might misunderstand what your code is doing. what are you expecting to happen with the trim? – Keith Nicholas Mar 21 '13 at 02:37
  • i just want to add var for trimstart something like - var trimChars = "ab", "as", "Ab", "As"; but code was not working so i wonder how to do it for multipleway –  Mar 21 '13 at 02:39
  • 1
    if you are trying to "trim" the start if the start has "ab" then TrimStart isn't what you want. Currently if it start with "ba" it will also trim those characters off – Keith Nicholas Mar 21 '13 at 02:44
  • you mean if i set ABCDEFGHIJKLMNOPQRST, all chars will gone? –  Mar 21 '13 at 02:44
  • yes, they will all go – Keith Nicholas Mar 21 '13 at 02:45
  • ahh ok i understand now ... sorry for the noob question but your answer enlighten me . thanks guyz :) –  Mar 21 '13 at 02:46

1 Answers1

0

to remove strings from the start you can use :-

public string RemoveFromStart(string s, IEnumerable<string> strings )
        {
            foreach (var x in strings.Where(s.StartsWith))
            {
                return s.Remove(0, x.Length);
            }
            return s;
        }

and use it like

var x = RemoveFromStart("ablah", new[] { "ab", "as", "Ab", "As" });

or with your code

var trimmed = RemoveFromStart(Slb, new string[] { "ab", "as", "Ab", "As" });
Keith Nicholas
  • 43,549
  • 15
  • 93
  • 156
  • 1. The Non-genetic type 'system.collections.ienumerable' cannot be used with type arguments, 2. Only assignedment, call, increment, decrement and new object expressions can be usedas a statement, 3. identifier expected –  Mar 21 '13 at 03:00
  • i just copy the code before the keyevents args then runs it sir . –  Mar 21 '13 at 03:03
  • hmmmmm what version of .NET? – Keith Nicholas Mar 21 '13 at 03:04
  • The non-generic type 'System.Collections.Inumerable' cannot be used with type arguments . Pointing error to Inumerable –  Mar 21 '13 at 03:09
  • oh, you need using using System.Collections.Generic; at the top – Keith Nicholas Mar 21 '13 at 03:10
  • Error 1 'System.Collections.Generic.IEnumerable' does not contain a definition for 'Where' and no extension method 'Where' accepting a first argument of type 'System.Collections.Generic.IEnumerable' could be found (are you missing a using directive or an assembly reference?) –  Mar 21 '13 at 03:14
  • @KeithNicholas ... sir is there a way i can make all strings unique? i mean for example strings are "as" and "asd" then i type asdfg then assuming i choose asd but whatsthe outcome was dfg instead of fg only ... any suggestion how to do it sir? –  Mar 21 '13 at 04:30
  • making this every string unique? var trimmed = RemoveFromStart(Slb, new string[] { "as", "asd" }); –  Mar 21 '13 at 04:31