Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get value after some specific character in C#

Tags:

string

c#

regex

So I want to extract the value from a string the value will place at the right after my specific character in this case my specific character is - and will place at the right.

The string will look like this:

TEST-QWE-1
TEST/QWE-22
TEST@QWE-3
TEST-QWE-ASD-4

And from that string I want to extract

1
22
3
4

How I do that in C#? Thanks in advance

like image 726
James Avatar asked Nov 15 '25 01:11

James


1 Answers

mystring.Substring(mystring.IndexOf("-") + 1)

Or use LastIndexOf in case there are other dashes before the last part:

mystring.Substring(mystring.LastIndexOf("-") + 1)

Substring: https://learn.microsoft.com/en-us/dotnet/api/system.string.substring?view=netframework-4.7.2

LastIndexOf: https://learn.microsoft.com/en-us/dotnet/api/system.string.lastindexof?view=netframework-4.7.2

like image 182
Arash Motamedi Avatar answered Nov 17 '25 16:11

Arash Motamedi