2

I have a string that looks like this: Value->Value2->Value3->ImportantValue->val.

I would like to extract ImportantValue. I have read other posts concerning this question but none of them works for me. I tried this:

int pFrom = path.IndexOf("->") + "->".Length;
int pTo = path.LastIndexOf("->val");
String result = path.Substring(pFrom, pTo - pFrom);

It returns Value2->Value3->ImportantValue->val because it gives out the string between the first -> and ->val. Any ideas on how I would change the above code so that it returns ImportantValue? Thanks in advance.

EDIT

Sorry, I forgot to specify that ImportantValue is always different. The entire string also changes, meaning it will have more three Values. But the ImportantValue is always the second to last element so I will mark the answer from Paul Kertscher as the right one and upvote the other similar answers.

Mysterio
  • 139
  • 12
  • Does "ImportantValue" have some kind of specific formatting or pattern that you are looking for? Maybe it will always be the same value? – Cheesebaron Feb 19 '20 at 13:13
  • Is it always the third one? – Stucky Feb 19 '20 at 13:13
  • Could you give us a realistic string to work with? It might be easier to identify the important value rather than using the '->' marker – sr28 Feb 19 '20 at 13:20

5 Answers5

3

If the ImportantValue is always the second to last element, you could do the following

var stringParts = path.Split(new[]{ "->" }, StringSplitOptions.None);
if(stringParts.Length >= 2)
{
    var importantValue = stringParts[stringParts.Length - 2];
}
else
{
    throw new ArgumentException("Input did not match the expected value");
}

You could also use IEnumerable extensions

var importantValue = path.Split(new[]{ "->" }, StringSplitOptions.None)
                         .Reverse() // reverse the enumerable
                         .Skip(1)   // discard "val"
                         .First();  // return the "ImportantValue"
Paul Kertscher
  • 9,416
  • 5
  • 32
  • 57
2

Provided that the string has always the same format, you could use just the Split method, and fetch the previous from the last element of the array returned from Split.

var values = path.Split(new []{"->"}, StringSplitOptions.None);
var importantValue = values[values.Length-2];

Pls check .NET Fiddle

Christos
  • 53,228
  • 8
  • 76
  • 108
2

You can use a Split method for that and access a second item from the end

var str = "Value->Value2->Value3->ImportantValue->val";
var items = str.Split(new []{"->"}, StringSplitOptions.None);
var importantValue = items[items.Length - 2];

C# 8 introduced an new approach for indices, so you can use var importantValue = items[^2]; syntax as well

You also may check a length of resulting array, that it has at least two elements

Pavel Anikhouski
  • 21,776
  • 12
  • 51
  • 66
0

I believe you can use string.split with ">" as your char array and get the array. Once you get the array you should be able to iterate the array and get the required value

mip
  • 51
  • 3
0

When important value is the 2nd from the end, LastIndexOf instead of IndexOf is a quite natural choice; please, note that since we seach from the end, it's pTo which we compute first.

  string path = "Value->Value2->Value3->ImportantValue->val";

  string delimiter = "->";

  int pTo   = path.LastIndexOf(delimiter);
  int pFrom = pTo >= 0 ? path.LastIndexOf(delimiter, pTo) + delimiter.Length : -1;

  // Either important value or null
  string result = pFrom >= 0 
    ? path.Substring(pFrom, pTo - pFrom) 
    : null; // or throw exception here
Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215