James
James

Reputation: 400

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

Upvotes: 4

Views: 13765

Answers (7)

schgab
schgab

Reputation: 666

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 this if you value code readability and maintainability.

If you're concerned about performance and memory usage, especially for very long strings, then Substring() and LastIndexOf() may be a more efficient option.

In most cases, the performance difference is negligible, and you can choose the one that makes your code more clear and understandable.

Upvotes: 3

Michał Turczyn
Michał Turczyn

Reputation: 37367

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

Upvotes: 0

Speedoops
Speedoops

Reputation: 126

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- =>
*/

Upvotes: 1

Andrei Dragotoniu
Andrei Dragotoniu

Reputation: 6335

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

Upvotes: 1

shingo
shingo

Reputation: 27021

Use LastIndexOf to get the last occurrence of '-'

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

Upvotes: 1

Arash Motamedi
Arash Motamedi

Reputation: 10682

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

Upvotes: 6

user8478480
user8478480

Reputation: 435

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);

Upvotes: -1

Related Questions