I know that this works to get the first item of an array
string aString = @"hello/all\this\is/a\test";
string firstItemOfSplit = aString.Split(new char[] {'\\', '/'})[0];
//firstItemOfSplit = hello
is there a way to get the last item? Something like
string aString = @"hello/all\this\is/a\test";
string lastItemOfSplit = aString.Split(new char[] {'\\', '/'})[index.last];
//lastItemOfSplit = test
You could always use LINQ:
string lastItem = aString.Split(...).Last();
Note that Enumerable.Last()
is optimized when working on an IList<T>
and you're not applying a predicate - so it's not even going to walk over the sequence to find the last one. (Not that it's likely to be an issue anyway.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With