3

I've got a problem. I need to split my every string like this: For example: "Economic drive without restrictions"

I need array with sub string like that: "Economic drive without" "drive without restrictions"

For now i have this:

            List<string> myStrings = new List<string>();
        foreach(var text in INPUT_TEXT) //here is Economic drive without restrictions
        {
            myStrings.DefaultIfEmpty();
            var textSplitted = text.Split(new char[] { ' ' });
            int j = 0;
            foreach(var textSplit in textSplitted)
            {

                int i = 0 + j;
                string threeWords = "";
                while(i != 3 + j)
                {
                    if (i >= textSplitted.Count()) break;
                    threeWords = threeWords + " " + textSplitted[i];
                    i++;
                }
                myStrings.Add(threeWords);
                j++;
            }
        }
sidron
  • 79
  • 7

4 Answers4

6

You could use this LINQ query:

string text = "Economic drive without restrictions";
string[] words = text.Split();
List<string> myStrings = words
    .Where((word, index) => index + 3 <= words.Length)
    .Select((word, index) => String.Join(" ", words.Skip(index).Take(3)))
    .ToList();

Because others commented that it would be better to show a loop version since OP is learning this language, here is a version that uses no LINQ at all:

List<string> myStrings = new List<string>();
for (int index = 0; index + 3 <= words.Length; index++)
{ 
    string[] slice = new string[3];
    Array.Copy(words, index, slice, 0, 3);
    myStrings.Add(String.Join(" ", slice));
}
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
  • 1
    I wonder why people always come up with linq queries to questions like this. I think these people are just learning programming, so what is the benefit of copy and pasting a linq query that they dont understand, and then still struggling with the basic operations. – Bgl86 Sep 18 '15 at 11:57
  • @Bgl86: it's subjective, but imo LINQ is easier and more understandable than "basic operations" like explicit loops. So why not also learn how to write readable and maintainable code with LINQ? Compare OP's approach with this concise query. – Tim Schmelter Sep 18 '15 at 12:01
  • I know LINQ. But I think understanding basics like loops and conditions is essential when you learn programming. So how would OP solve the same Task in Javascript with no LINQ available? – Bgl86 Sep 18 '15 at 12:07
  • @MatthewWatson: point taken, edited my answer to provide also a non linq version. – Tim Schmelter Sep 18 '15 at 12:15
  • 1
    @Bgl86: point taken, edited my answer to provide also a non linq version. However, it's not javascript and the task was not to avoid all .NET methods. – Tim Schmelter Sep 18 '15 at 12:16
1

I try to give a simple solution. So i hope you can better understand it.

    List<string> myStrings = new List<string>();

    string input = "Economic drive without restrictions";

    var allWords = input.Split(new char[] {' '});

    for (int i = 0; i < allWords.Length - 2; i++)
    {
        var textSplitted = allWords.Skip(i).Take(3);
        string threeString = string.Join(" ", textSplitted);
        myStrings.Add(threeString);
    }

    foreach (var myString in myStrings)
    {
        Console.WriteLine(myString);
    }

The method Take(n) is from Linq. It takes the first n elements of the given array. for example if you have an array a,b,c,d,e then Take(3) will give you a new array a,b,c.

The method Skip(n) is from Linq. It gives you the new array by skipping first n elements. given array a,b,c,d,e then Skip(1) will return b,c,d,e. as you can see it skipped the first elements.

Now with this two methods you can move on array 3 by 3 and get the words you want.

M.kazem Akhgary
  • 18,645
  • 8
  • 57
  • 118
  • 1
    Little nitpick: `Skip` or `Take` don't give you an array, they are using deferred execution, so basically they give you nothing but the query. You need to execute it, f.e. with `ToArray`, `ToList`, in a `foreach` or hidden in `string.Join`. – Tim Schmelter Sep 18 '15 at 11:53
  • @TimSchmelter Thanks for pointing me on that. yes its not an array. but its not needed to convert them in to Array here since this methods and also Join method works with IEnumerable – M.kazem Akhgary Sep 18 '15 at 11:55
1

Just for comparative purposes, here's another solution that doesn't use Linq:

string[] words = INPUT_TEXT.Split();
List<string> myStrings = new List<string>();

for (int i = 0; i < words.Length - 2; ++i)
    myStrings.Add(string.Join(" ", words[i], words[i+1], words[i+2]));

Or using ArraySegment<string>:

string[] words = INPUT_TEXT.Split();
List<string> myStrings = new List<string>();

for (int i = 0; i < words.Length - 2; ++i)
    myStrings.Add(string.Join(" ", new ArraySegment<string>(words, i, 3)));
Matthew Watson
  • 104,400
  • 10
  • 158
  • 276
0

I would use one of the methods described here ; for instance the following that takes the elements 3 by 3.

var groups = myStrings.Select((p, index) => new {p,index})
                      .GroupBy(a =>a.index/3);

Warning, it is not the most memory efficient, if you start parsing big strings, it might blow up on you. Try and observe.

Then you only need to handle the last element. If it has less than 3 strings, fill it up from the left.

Community
  • 1
  • 1
Timothée Bourguignon
  • 2,190
  • 3
  • 23
  • 39