Stephen Orr
Stephen Orr

Reputation: 11

How to get the last index of a string that starts with X from a list in C#?

So I made a list containing lines from an html document and I'm trying to get the index of last CSS reference made (to append a new reference after it). The following code does not work for me.

string prefix = "<link rel=\"stylesheet\""; //goal is to find last line that starts with this.

LineList = File.ReadAllLines(file).ToList();

int index = LineList.LastIndexOf(prefix);

I'm guessing .LastIndexOf() only works on exactly defined strings. How would I find the index of the last line that starts with (or contains) prefix?

Thank you

Upvotes: 0

Views: 344

Answers (5)

mrogal.ski
mrogal.ski

Reputation: 5940

As you said you want to find last line that starts with and you're searching for line that equals your prefix.

To fix this I recommend this using this code :

string prefix = "<link rel=\"stylesheet\""; //goal is to find last line that starts with this.
LineList = File.ReadAllLines(file).ToList();
int index = LineList.Select( (line, index) => new { text = line, idx = index }).LastOrDefault( a => a.line.StartsWith(prefix).idx;

Another way you can achieve this is to use for loop and just assign index of current iteration:

string prefix = "<link rel=\"stylesheet\""; //goal is to find last line that starts with this.
LineList = File.ReadAllLines(file).ToList();
int index = -1;
for( int i = 0; i < LineList.Length; i++)
    if(LineList[i].StartsWith(prefix)) index = i;
}

Or you can use simplies Linq possible :

string prefix = "<link rel=\"stylesheet\""; //goal is to find last line that starts with this.
LineList = File.ReadAllLines(file).ToList();
int index = LineList.IndexOf(LineList.LastOrDefault(line => line.StartsWith(prefix)));

Upvotes: 1

cSteusloff
cSteusloff

Reputation: 2628

You can use LINQ:

string prefix = "<link rel=\"stylesheet\""; //goal is to find last line that starts with this.

LineList = File.ReadAllLines(file).ToList();

int index = LineList.IndexOf(LineList.LastOrDefault(l => l.Contains(prefix)));

Upvotes: 0

Chris Pickford
Chris Pickford

Reputation: 9001

Query the list to get the item:

var item = LineList.Where(x => x.StartsWith(prefix)).LastOrDefault();

Then use the item to get the index:

int index = -1;
if (item != null)
    index = LineList.IndexOf(item);

Upvotes: 0

InBetween
InBetween

Reputation: 32780

Well, simply iterate backwards through the lines enumeration until you reach the first one that meets your criteria:

for (var i = lineList.Count -1 ; i != 0; i--)
{
    if (lineList[i].StartsWith("<link rel=\"stylesheet\"")
    {
        return i;
    }
}

return -1;

You could do this with with linq but I think you need to grasp some basics first.

Upvotes: 1

Ian
Ian

Reputation: 30823

You could use LINQ and string method StartsWith like this:

string prefix = "<link rel=\"stylesheet\""; //goal is to find last line that starts with this.

List<string> LineList = File.ReadAllLines(file).ToList();
int index = LineList.Select((n, i) => new { Value = n, Index = i })
    .LastOrDefault(x => x.Value.StartsWith(prefix))
    .Index;

The idea above is to create index for this list and then choose the last one which StartsWith the prefix.

If looping can be used, you could also create simple looping starting from the last element and return the first index of the element which starts with the prefix

Upvotes: 1

Related Questions