I use asp.net 4 and c#.
I have a string:
string original = "hi-this-is-a-string-15"
Please note the last 3 digit from right -15
Now the same string but with more digit after the last dash
hi-this-is-a-string-9999
Please note the last 5 letters from right -9999
I need a RegEx or another solution which allow me to make a sub-selection in string original on any digit following the last dash ex: -15 or -999.
The Role should be flexible to detect any digit variable length after the last dash ex: -1 or -5656565656565...
At the end of the process I should have:
string original = "hi-this-is-a-string-15"
string cropped = "hi-this-is-a-string"
string suffix = "-15"
Any idea how to do it? Thanks for your help on this.
My original code (Generates error):
string original = "hi-this-is-a-string-15";
int l = original.Length;
int i = original.LastIndexOf("-");
string sub = original.Substring(i, l);
error: Index and length must refer to a location within the string. Parameter name: length
You can get the index of last occurrence and return the sub string form that index like.