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.

3
  • Does "ImportantValue" have some kind of specific formatting or pattern that you are looking for? Maybe it will always be the same value? Commented Feb 19, 2020 at 13:13
  • Is it always the third one? Commented Feb 19, 2020 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 Commented Feb 19, 2020 at 13:20

5 Answers 5

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

Comments

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

Comments

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

1 Comment

This is the same answer as @Christos
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

Comments

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

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.