2

I am new to C# and only know the basics. I'm looking for a code example on how I would get every other word from a string variable. For example if the variable was

String x = "Help me, coding is difficult"; 

I would want to return single string "Help coding difficult". It needs to be a function that takes one string and return filtered version of it.

Someone suggested duplicates that I mostly already seen during my research:

  • How to split text into words? shows very complicated logic that takes into account punctuation for example. I think I'm fine just to rely on spaces, but if you have better suggestion with an explanation - would be nice.
  • Select every second element from array using lambda - seems promising, but sample shows how to work with integer array. I have string (or maybe string array if one can adapt string splitting code to provide one).
  • C# Print list of string array that also sounds promising but it shows how to print the result, not how to return it as value from a function.
Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179
  • How do you define the boundary between a word? If `me,` was the first word, would you include or exclude the comma? – gunr2171 Feb 03 '22 at 21:25
  • Preferably Exclude the comma which i think could be done using .split if im not mistaken? – Sean Normandale Feb 03 '22 at 21:27
  • 1
    Unless you can constrain the text in some known way, or are comfortable with lots of bugs around edge cases, you're on the wrong side of this comic: https://xkcd.com/1425/ and what you're asking for is the equivalent of a fully functional natural language processor. – Joel Coehoorn Feb 03 '22 at 21:30
  • What about punctuation? Or apostrophes? Or hyphens? ["What counts as a Word?"](https://www.youtube.com/watch?v=m8niIHChc1Y) is hard to describe, unless you're simply doing "a space is the only thing that counts". – gunr2171 Feb 03 '22 at 21:32
  • So if I was to leave the punctuation in with the word output and only exclude the spaces. is there a way to skip the 2nd, 4th, 6th word and so on and only output the 1st , 3rd, 5th, and so on? – Sean Normandale Feb 03 '22 at 21:36
  • 1
    If you're just looking to strip out the punctuation, and split on spaces, then yes `string.Split` is the right direction, followed by something like a `for` loop. We *can* answer these kinds of questions, but I think you would be better suited following some tutorials first, and ask here once you have an attempted implementation – Andrew Williamson Feb 03 '22 at 21:39
  • What should happen to this sentence? `"Help me!Coding is difficult"`? – Lasse V. Karlsen Feb 03 '22 at 21:40
  • A strategy might be to replace punctuation marks with spaces, remove multiple spaces and then use an alternate selector as in [this example](https://stackoverflow.com/questions/4123685/how-to-get-alternate-elements-using-enumerable-in-c-sharp) – Peter Smith Feb 03 '22 at 21:43
  • If upvoter can [edit] the question with clarifications requested in comment it would be nice (and possibly even it will no longer need list of duplicates I suggested). – Alexei Levenkov Feb 03 '22 at 21:49
  • @AlexeiLevenkov I think every 2nd word is not the same as every 2nd element as suggested by your duplicate. – Magnus Feb 03 '22 at 21:55
  • @Magnus sounds fair - I've edited the question to add clarifications how none of the duplicates helped (or even if they are related) – Alexei Levenkov Feb 03 '22 at 22:14
  • Does this answer your question? [Split every two words from string using asp.net c#](https://stackoverflow.com/questions/56807078/split-every-two-words-from-string-using-asp-net-c-sharp) – outis Feb 04 '22 at 10:37

3 Answers3

4

You need to define first what are word delimiters, just a space, a comma, period or semicolon or what else? Then you can use String.Split.

char[] wordDelimiter = new[] { ' ','\t', ',', '.', '!', '?', ';', ':', '/', '\\', '[', ']', '(', ')', '<', '>', '@', '"', '\'' };
String x = "Help me, coding is difficult"; 
string[] words = x.Split(wordDelimiter, StringSplitOptions.RemoveEmptyEntries);

Now you have an array with all words. But you want every other, you could fill them into a List<string> and use a for-loop that skips every other:

var everyOtherWord = new List<string>();
for(int i = 0; i < words.Length; i += 2)
{
    everyOtherWord.Add(words[i]);
}

I would want to return "Help coding difficult"

So you really want to have a string as result that contains these words separated by space? Then use String.Join (somehow the counterpart method of String.Split):

string result = string.Join(" ", everyOtherWord);
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
1

Assuming that you count a word as "every character between spaces, or the start/end of the string", and you're not trying to remove any letters.

var input = "Help me, coding is difficult";
var everyOther = input
    .Split(' ')
    .Where((x, i) => i % 2 == 0);
Console.WriteLine(string.Join(" ", everyOther));

The code for the Where was taken from this post.

The idea is to use the overload of Where which gives you the index value, and see if that index value is even, if so, include it in the results.

gunr2171
  • 16,104
  • 25
  • 61
  • 88
  • As im new to C# & coding in general... are you able to breakdown the ".Where((x, i) => i % 2 == 0);" so I have a better understanding of how it works and what its doing? – Sean Normandale Feb 03 '22 at 22:16
  • 1
    Uh, well. There's a number of topics here. Linq. Lambdas. Modulus division. Those three terms should give you a starting point. – gunr2171 Feb 03 '22 at 22:20
  • @SeanNormandale that part applies a filter to the collection, based on the index. It's syntactic sugar for `bool IsOdd(item, index) { return index % 2 == 0; }`, which is passed to the `Where` method: `.Where(IsOdd)` – Andrew Williamson Feb 04 '22 at 00:24
0
string s = "You win some. You lose some.";

string[] subs = s.Split(' ');

for(int i=0 ; i < subs.length ; i+=2)
{
    Console.WriteLine($"Substring: {sub[i]}");
}
Amjad S.
  • 1,196
  • 1
  • 4
  • 16