0

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.

2
  • 2
    Yeah, loop over the array starting at the index of your "certain element". Seems like homework? Commented Jun 22, 2012 at 10:46
  • @Morawski Have put in the code Commented Jun 22, 2012 at 11:13

6 Answers 6

3

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;
Sign up to request clarification or add additional context in comments.

Comments

3
foreach(var element in myStringList.Skip(myStringList.IndexOf("certainElement"))
    Console.WriteLine(element);

Comments

0

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]);
     }
}

Comments

0

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!

Comments

0

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());
}

3 Comments

Well it's F, incorrect. For printing out i.ToString() (just consecutive numbers), why would you need any string[] test at all? ;)
Force of habit, typed it out real quick. Bit confused by this line Well it's F, incorrect.? The string array may be consecutive numbers but that's irrelevant, it still demonstrates a possible solution to this question.
Just a joke referring to the fact that the question is probably OP's homework ;) He needs to print values from the array anyhow - not indexes - so what he actually needs is rather Console.WriteLine(test[i]);
0
Console.WriteLine(string.Join(", ", myArr.Skip(someAmount)));

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.