jeyejow
jeyejow

Reputation: 429

c# make all elements in array without space

I have this problem that CANT be solved with a cicle. (else I wouldn't be asking) I have a array of strings, each storing different strings. example of the things inside: "EC > TP > UK" , AN > ZX", etc.

i know the .replace thing you can do with a string. Is there anything like that for string array? this is what i have:

public static string[] dep1 = dep_p.Where((c, i) => i % 2 == 0).ToArray<string>();
        public static string[] dep =

i need that dep is equal to dep1 but the elements don't have blank spaces in between each other, for example

if dep1 one has in, lest say, dep1[1] = "EC > AK > OT", i want dep[1] to be = "EC>AK>OT".

Regardless, thanks!

Upvotes: 0

Views: 555

Answers (1)

Tim Schmelter
Tim Schmelter

Reputation: 460288

dep = dep1.Select(s => s.Replace(" ", "")).ToArray();

If it's a little bit more complicated because the tokens itself could contain spaces like E C > AK > O T and you want to remove them, so still get EC>AK>OT as result:

dep = dep1.Select(
    s => string.Join(">", s.Split(new[] { '>' }, StringSplitOptions.RemoveEmptyEntries)
                           .Select(t => t.Replace(" ", ""))))
   .ToArray();

Upvotes: 2

Related Questions