4

I have the below string array.

 string[] sentence = new string[] { "The quick brown", "fox jumps over", "the lazy dog." };

I want to split it with " " and rejoin with #. This is for learning linq in c#. I know i can easily manage this with replace and other built in features. But i am trying in this way.

 var sentenceresult = sentence.Select(c => c.Split(' '))

but how to apply "#" for each item?

4
  • 2
    So what is the expected output? You said split by "" but in your code you are spliting by ' '. Commented Dec 10, 2015 at 17:19
  • Is Linq really appropriate here? Commented Dec 10, 2015 at 17:21
  • @SteveWellens . No ..absolutely not. Its just for educational purpose. Commented Dec 10, 2015 at 17:22
  • @RahulSingh. Yes you are right. I updated the question. Commented Dec 10, 2015 at 17:23

3 Answers 3

6

You can do this with String.Join.

This should give you the expected output:-

string[] result = sentence.Select(x => String.Join("#", x.Split(' ')))
                          .ToArray();

Fiddle.

Update:

Enumerable.Select projects each item from your array sentence. So when you say Select(x => x will iterate through your array and will hold follwoing values in each iteration:-

"The quick brown"
"fox jumps over"
"the lazy dog."

Now, consider just the first sentence. String.Join method:

Concatenates all the elements of a string array, using the specified separator between each element.

So when we say x.Split(' ') it will actually split "The quick brown" (remember we are considering the first sentence here) and return a string array which is joined with #. Similarly for other sentence it will join the sentence.

Sign up to request clarification or add additional context in comments.

8 Comments

Why the x.Select(z => z)? Shouldn't be necessary.
@BradfordDillon - You are correct, since it is string array it is not required. Thanks.
@rahul Singh..Thats great. it works perfect. I have one more doubt. sentence.Select(x => x.Split(' ')) .Select(x => String.Join("#", x)) .ToArray(); works. But if i replace x in the second select statement with another variable it not works. sentence.Select(x => x.Split(' ')) .Select(x => String.Join("#", c)) .ToArray(); As per my understatnding first processed result will be available in the second select statement and the variable name is not important here. Am I wrong?
@Ammu - Check my update for explanation and let me know if you have any doubts.
If each x scope is only to that method, why sentence.Select(x => x.Split(' ')) .Select(x => String.Join("#", c)) .ToArray(); is not working?
|
2

Using what you have var sentenceresult = sentence.Select(c => c.Split(' ')) this will give you an enumerable of string arrays, ie. IEnumerable<string[]> where each entry in the enumerable is a string array of words from each substring. This is somewhat clunky to work with, unless you're really sure it's what you want.

You probably want to use SelectMany:

var sentenceresult = sentence.SelectMany(c => c.Split(' '));

This will give you a single collection of string objects, where each string is a single word from the individual which is equivalent to this:

var sentenceresult = new [] { "The", "quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog." };

Then you can use string.Join(sentenceresult) to get the following result:

The#quick#brown#fox#jumps#over#the#lazy#dog.

Comments

2

You can try a one-line solution:

string[] res = sentence.Select(str => String.Join("#", str.Split())).ToArray();

Or if you want one string:

string res2 = String.Join("#", sentence.SelectMany(str => str.Split()));

Comments

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.