1

I want to create a function but I don't know how it would work or how to create it. I want to create a function something similar to below.

I have a string, lets say its this for example:

string myString = "This is my string and it will forever be my string.";

What if I wanted to split this by a space and get each part? which I do...

string[] parts = myString.Split(' ');

Now, I want to get everything but the first 3 words in my string parts, how would I merge each string in parts except the first 3? which will return

string and it will forever be my string.

Something similar to this:

public string getMergedString(string[] parts, int start) // This method would return everything from where you told it to start...
{

}
Ashkru
  • 1,495
  • 1
  • 17
  • 26
  • Possible duplicate of [Array slices in C#](http://stackoverflow.com/questions/406485/array-slices-in-c-sharp) – mason Feb 17 '17 at 17:23
  • 1
    Nothing similar to your `getMergedString` is going to work, because it has no code in it. How would you try to solve your problem, using the tools you're familiar with? You know what a loop is, don't you? – 15ee8f99-57ff-4f92-890c-b56153 Feb 17 '17 at 17:23

2 Answers2

5
public string getMergedString(string[] parts, int start) // This method would return everything from where you told it to start...
{
    return String.Join(" ", parts.Skip(start));
}

Quick explanation of the code:

string.Join(separator, IEnumerable values)

This joins an IEnumerable object containing strings to one, unified string.

Docs: https://msdn.microsoft.com/en-us/library/system.string.join(v=vs.110).aspx

parts.Skip(int count)

This part of the code skips a given amount of elements, before returning them. Skip(int count) is an extension method found in the System.Linq namespace. You need .Net 3.5 or higher in order for you to be able to use this method.

Docs: https://msdn.microsoft.com/en-us/library/bb358985(v=vs.110).aspx

SimonC
  • 1,547
  • 1
  • 19
  • 43
Jon
  • 3,230
  • 1
  • 16
  • 28
0
            string myString = "This is my string and it will forever be my string.";
        string[] words = myString.Split(' ');
        var myNewString = string.Join(" ", words.Skip(3));
Tarek Abo ELkheir
  • 1,311
  • 10
  • 13