user7597016
user7597016

Reputation:

C# WPF Separate characters from a string (starting from the back)

I have such a comic string.

www.asdsad.de/dsfdsf/sdfdsf=dsfdsfs?dsfsndfsajdn=sfdjasdhads=test.xlsx

I would like to get only the test.xlsx out. So I wanted to say that I wanted to separate the string from behind. That he he once the first = sign found me the string supplies the from the end to the = sign goes.

Whats the best way to do this?

Unfortunately, I would not know how I should do with SubString, since the length can always be different. But I know that in the end is what I need and the unnecessary with the first = Begin from behind

Upvotes: 0

Views: 71

Answers (2)

Ciprian Lipan
Ciprian Lipan

Reputation: 340

Another option:

string source = "www.asdsad.de/dsfdsf/sdfdsf=dsfdsfs?dsfsndfsajdn=sfdjasdhads=test.xlsx";

Stack<char> sb = new Stack<char>();

for (var i = source.Length - 1; i > 0; i--)
{
    if (source[i] == '=')
    {
        break;
    }

    sb.Push(source[i]);
}

var result = string.Concat(sb.ToArray());

Upvotes: 0

Dmitrii Bychenko
Dmitrii Bychenko

Reputation: 186813

Yes, Substring will do, and there's no need to know the length:

string source = "www.asdsad.de/dsfdsf/sdfdsf=dsfdsfs?dsfsndfsajdn=sfdjasdhads=test.xlsx";

// starting from the last '=' up to the end of the string
string result = source.SubString(source.LastIndexOf("=") + 1);

Upvotes: 1

Related Questions