简体   繁体   中英

Get value after some specific character in C#

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

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://docs.microsoft.com/en-us/dotnet/api/system.string.substring?view=netframework-4.7.2

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

I would suggest you to learn about Regex for string processing. In your case a simple Regex pattern like [0-9]+$ would match your numbers.

Since you stated that the number is always to the right of your string you could also use string.Split('-').Last()

Use LastIndexOf to get the last occurrence of '-'

var p = str.LastIndexOf('-');
return p >= 0 && (p + 1 < str.Length) ? str.Substring(p + 1) : "";

You can use string.LastIndexOf() and string.Substring() to do that. And take care when the special character not occurred in your input.

string[] inputs = new string[]{ 
    "TEST-QWE-1", 
    "TEST/QWE-22",
    "TEST@QWE-3", 
    "TEST-QWE-ASD-4", 
    "TEST-QWE-ASD-4", 
    "TEST",
    "TEST-"
};
foreach(string input in inputs){
    int lastIdx = input.LastIndexOf("-");
    string output = lastIdx > -1 ? input.Substring(lastIdx + 1) : "";
    Console.WriteLine(input + " => " + output);
}
/* console outputs:
TEST-QWE-1 => 1
TEST/QWE-22 => 22
TEST@QWE-3 => 3
TEST-QWE-ASD-4 => 4
TEST-QWE-ASD-4 => 4
TEST =>
TEST- =>
*/

I will post another regex to capture what you want: -([^-]+)$

It's different from already posted, since it will capture everything except hyphen (with [^-]+ ) between hyphen ( - ) and end of a string ( $ means end of a string).

The desired result will be stored in first cupturing group.

Code snippet:

var s = "TEST-QWE-1";
var match = Regex.Match(s, "-([^-]+)$");
if (match.Success)
  Console.WriteLine(match.Groups[1]);

Demo

You can use Regex , and this is the string you will need. [^-]+$

Simply loop over each string you have.

var regex = new Regex(@"([^-]+$)");

regex.Matches(str);

You can use a simple Regex like (-\\d+$)

You can as well use Split() and get the last element

"TEST-QWE-ASD-4".Split('-').Last();

The way to go is use the LastIndexOf method like this :

string input = "TEST-QWE-1";
var lastIndex = input.LastIndexOf("-");
var id = input.Substring(lastIndex + 1); // this is so you don't get the minus as well if you don't want it.

so, first we get the last index of the character we care about. second, we perform a substring using this index to get the result we want

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM