How to check if length of a string is equal to the number appended at its end in C#

1 Answer

0 votes
using System;
using System.Text.RegularExpressions;

class Program
{
    public static int extractLastNumber(string str) {
        string[] arr = Regex.Split(str, @"\D+");
         
        return int.Parse(arr[arr.Length - 1]);
    }
    
    public static bool stringLenghtEqualToNumberAtEnd(String str) {
        return str.IndexOfAny(new char[] {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'}) == extractLastNumber(str);
    }
    
    static void Main() {
        string str = "c# programming14";

        Console.Write(stringLenghtEqualToNumberAtEnd(str));
    }
}




/*
run:

True

*/

 



answered Aug 31, 2023 by avibootz
...