Reputation: 305
I have an array of strings on lenght n.
I would like to only print out the values after a certain element, regardless of the amount of elements before or after.
Is there anyway to do this?
EDIT CODE
string separator = "\\";
string[] splitPath = path.Split('\\');
string joinedPath = String.Join(separator, splitPath[3], splitPath[4], splitPath[5]);
Console.WriteLine("Extracted: " + path);
I have it being rejoined at 3 because I know the array, but I want it so that it willdo it no matter the location
And nope its not homework ;) I've a console app thats printing out a big long path, i only want it to show part of that path, not just the file. I was thinking of deleting/removing the elements up to x and then joining them back up.
Upvotes: 0
Views: 3521
Reputation: 4468
Very simple little example:
int myIndex = 5;
string[] test = { "1", "2", "3", "4", "5", "6", "7", "8", "9", "0" };
for (int i = myIndex; i < test.Length; i++)
{
Console.WriteLine("Element: " + i.ToString());
}
Upvotes: 0
Reputation: 4739
You can do something like this:
// 'array' is the string array and 'startFrom' is where in the array you want to start from
string[] array = ...;
int startFrom = 5;
for(int count = startFrom; count < array.Length; count ++)
{
Console.WriteLine(array[count]);
// Or use array[count] for something else.
}
Hope this helps!
Upvotes: 0
Reputation: 39085
foreach(var element in myStringList.Skip(myStringList.IndexOf("certainElement"))
Console.WriteLine(element);
Upvotes: 3
Reputation: 120917
Do you mean something like this?
public void PrintArray(int fromIndex, string[] strings)
{
if(strings == null) throw new ArgumentNullException("strings");
if(fromIndex > strings.Length) throw new ArgumentException("Bad fromIndex", "fromIndex");
for(int i = fromIndex; i < strings.Length; i++)
{
Console.WriteLine(strings[i]);
}
}
Upvotes: 0
Reputation: 648
If I understand it right...you can use LINQ:
var result = yourArray.Skip(x);
or if you want to take only some strings:
var result = yourArray.Skip(x).Take(y);
don't forget:
using Sytem.Linq;
Upvotes: 3