c# - How to check if a string ends with a number, without regex?

C# - How to check if a string ends with a number, without regex?

To check if a string ends with a number in C# without using regular expressions, you can use basic string operations and built-in methods. Here's a step-by-step guide to accomplish this:

Steps

  1. Check if the string is empty: If it's empty, it can't end with a number.
  2. Extract the last character: Get the last character of the string.
  3. Check if the last character is a digit: Use char.IsDigit to determine if the last character is a digit.

Implementation

Here is a C# method to determine if a string ends with a number:

using System; public class Program { public static void Main() { // Test cases Console.WriteLine(EndsWithNumber("Hello123")); // True Console.WriteLine(EndsWithNumber("HelloWorld")); // False Console.WriteLine(EndsWithNumber("12345")); // True Console.WriteLine(EndsWithNumber("123Hello")); // False Console.WriteLine(EndsWithNumber("")); // False } public static bool EndsWithNumber(string input) { // Check if the string is null or empty if (string.IsNullOrEmpty(input)) { return false; } // Get the last character char lastChar = input[input.Length - 1]; // Check if the last character is a digit return char.IsDigit(lastChar); } } 

Explanation

  1. Check for Empty String:

    • Use string.IsNullOrEmpty(input) to handle cases where the string is empty or null.
  2. Extract Last Character:

    • Use input[input.Length - 1] to get the last character of the string.
  3. Check if Last Character is a Digit:

    • Use char.IsDigit(lastChar) to check if the character is a numeric digit.

Summary

  • Time Complexity: O(1), as checking the last character is a constant-time operation.
  • Space Complexity: O(1), as no additional space is required beyond the input string.

This method efficiently determines if a string ends with a number using basic string operations and without relying on regular expressions.

Examples

  1. C# check if string ends with a number using char methods

    string input = "example123"; bool endsWithNumber = char.IsDigit(input[^1]); Console.WriteLine(endsWithNumber); // Output: True 

    Description: This code checks if the last character of the string is a digit using char.IsDigit.

  2. C# determine if string ends with a digit using LINQ

    string input = "example123"; bool endsWithNumber = input.Any() && char.IsDigit(input.Last()); Console.WriteLine(endsWithNumber); // Output: True 

    Description: This code uses LINQ to check if the string has any characters and if the last character is a digit.

  3. C# check if last character of string is a digit using string methods

    string input = "example123"; bool endsWithNumber = input.Length > 0 && char.IsDigit(input[input.Length - 1]); Console.WriteLine(endsWithNumber); // Output: True 

    Description: This code checks the length of the string and if the last character is a digit.

  4. C# check if string ends with a number using TryParse

    string input = "example123"; bool endsWithNumber = int.TryParse(input[^1].ToString(), out _); Console.WriteLine(endsWithNumber); // Output: True 

    Description: This code attempts to parse the last character as an integer to determine if it is a digit.

  5. C# identify if string ends with numeric character using char

    string input = "example123"; bool endsWithNumber = input.Length > 0 && input[^1] >= '0' && input[^1] <= '9'; Console.WriteLine(endsWithNumber); // Output: True 

    Description: This code checks if the last character is between '0' and '9'.

  6. C# check if last character in string is digit using ASCII values

    string input = "example123"; bool endsWithNumber = input.Length > 0 && input[^1] >= 48 && input[^1] <= 57; Console.WriteLine(endsWithNumber); // Output: True 

    Description: This code uses ASCII values to check if the last character is a digit.

  7. C# confirm if string ends with a number using string operations

    string input = "example123"; bool endsWithNumber = input.Length > 0 && "0123456789".Contains(input[^1]); Console.WriteLine(endsWithNumber); // Output: True 

    Description: This code uses the Contains method on a string of digits to check if the last character is a digit.

  8. C# verify if string ends with a digit using a loop

    string input = "example123"; bool endsWithNumber = false; if (input.Length > 0) { char lastChar = input[^1]; foreach (char digit in "0123456789") { if (lastChar == digit) { endsWithNumber = true; break; } } } Console.WriteLine(endsWithNumber); // Output: True 

    Description: This code loops through each digit character to check if the last character of the string is a digit.

  9. C# check last character using substring method

    string input = "example123"; bool endsWithNumber = false; if (input.Length > 0) { string lastChar = input.Substring(input.Length - 1); endsWithNumber = "0123456789".Contains(lastChar); } Console.WriteLine(endsWithNumber); // Output: True 

    Description: This code uses Substring to get the last character of the string and checks if it is a digit.

  10. C# confirm string ends with digit using EndsWith method

    string input = "example123"; bool endsWithNumber = false; for (char digit = '0'; digit <= '9'; digit++) { if (input.EndsWith(digit.ToString())) { endsWithNumber = true; break; } } Console.WriteLine(endsWithNumber); // Output: True 

    Description: This code uses the EndsWith method within a loop to check if the string ends with any digit character.


More Tags

selection non-english ngroute mutablelivedata pushsharp vuforia laravel-middleware strcpy calico django-admin

More Programming Questions

More Gardening and crops Calculators

More Chemical reactions Calculators

More Genetics Calculators

More Stoichiometry Calculators