I have a string that can have values like
"1 Of 1"
"2 Of 4"
"8 Of 10"
etc.
I want the value of the last number in the string, ie, 1
, 4
, 10
.
Would Regex \\d+$
work?
您可以使用var lastno = input.Split().Last();
You can also do this without regex
as follows:
string s = "1 Of 1"
string[] words = s.Split(' ');
words[words.Length - 1]
And to answer your question, YES, it does work. (Although, I must say, Blorgbeard raises an excellent point)
"1 Of 1" ==> '\d+$' ==> "1"
"1 Of 4" ==> '\d+$' ==> "4"
"1 Of 10" ==> '\d+$' ==> "10"
For example,
string value = "8 Of 10";
value = value.Substring(value.IndexOf('f') + 2, value.Length)
Yes it would but a little cut off at last!
string[] inputs = new[] {"1 Of 1","2 Of 4", "8 Of 10"};
foreach (var input in inputs)
{
string[] numbers = Regex.Split(input, @"\D+");
Console.WriteLine("Last Number from input \"{0}\" is: {1}",
input,numbers[numbers.Length-1]);
}
You can use this code.It will give perfect output.
string str = "10 OF 10";
int pos = str.IndexOf("F");
string str1 = str.Substring(pos + 1);
Console.Write(str1);
Use this RegEx:
var SearchString = @"8of10";
var x = Regex.Match(SearchString, @"([0-9]+)[^0-9]*$");
if (x.Success && x.Groups.Count > 0)
{
var foundNumber = x.Groups[1].Captures[0].Value;
}
**regexp { ([0-9])( |\t)*([a-zA-Z]+)( |t)*([0-9])$} $x A B C D E F** ( tcl)
The value of the last digit will be stored in the variable F.
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.