Mysterio
Mysterio

Reputation: 139

C# - Finding a string between two strings

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.

Upvotes: 2

Views: 743

Answers (5)

Paul Kertscher
Paul Kertscher

Reputation: 9713

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"

Upvotes: 3

Dmitrii Bychenko
Dmitrii Bychenko

Reputation: 186698

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

Upvotes: 0

Pavel Anikhouski
Pavel Anikhouski

Reputation: 23228

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

Upvotes: 2

mip
mip

Reputation: 51

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

Upvotes: 0

Christos
Christos

Reputation: 53958

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

Upvotes: 2

Related Questions