0

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!

0

1 Answer 1

2
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();
Sign up to request clarification or add additional context in comments.

2 Comments

Moreover, you could insert the Select() call in between the original Where() and ToArray(), and thus save the computer a bunch of cpu and memory.
@JoelCoehoorn: probably, but i don't know why OP needs 3 collections, maybe there is a reason

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.