-1

I need to check if the last character of a string ends with an int. I would like to avoid using a regular expression, because I think it is very hard, but if there is no smoother way of doing it, I will try it.

I basically tried something that looks like this:

text.EndsWith(INTEGER)]
Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
Alex_Joo
  • 111
  • 2
  • 7
  • This doesn't compile... `I don't would like to avoid regex` are you saying you want to stay away from `regex` or... Also what's the issue you are seeing, what's the expected output? – Trevor Nov 05 '19 at 21:28
  • Do you just want to know if the last character is a decimal digit (`0` through `9`) using the "normal" digits? Or do you want to also accept decimal digits in [other writing systems](https://www.fileformat.info/info/unicode/category/Nd/list.htm)? What about non-decimal digits, like fractions and roman numeral characters (e.g., ½)? – Joe Sewell Nov 05 '19 at 21:31
  • 2
    @JoeSewell he specified `int` in the question body – Rufus L Nov 05 '19 at 21:32
  • `bool result = !string.IsNullOrEmpty(text) && char.IsDigit(text[text.Length - 1]);` – Rufus L Nov 05 '19 at 21:45
  • 1
    @OlivierRogier why did you change the question completely by converting from "check last character is digit" to "check string ends with an integer"? – Alexei Levenkov Nov 05 '19 at 21:47
  • `char.IsDigit((text is string str) && str.Length > 0 ? str[str.Length - 1] : ' ')` another alternative and dirty :) – Trevor Nov 05 '19 at 22:00
  • @Alex_Joo A *number* and an *integer* are not a *digit*... can you update the title and the body to help others to not be confused and to allow some to remove the wrong duplicates, please? –  Nov 07 '19 at 17:44

5 Answers5

9
Boolean result = char.IsDigit(text[text.Length - 1]);
John smith
  • 160
  • 6
4

using linq Last method and Isdigit function

bool isDigit = Char.IsDigit(("one1").Last());
Tiisetso Tjabane
  • 2,088
  • 2
  • 19
  • 24
0

Isolate the last character and try parse it to integer:

private static bool IsStringEndsWithDigit(string str)
{
    if (String.IsNullOrEmpty(str))
    {
        return false;
        // or throw an exception
        // throw new Exception("string can not be empty");
    }
    string last = str.Substring(str.Length - 1, 1);
    int num;
    var isNum = int.TryParse(last, out num);
    return isNum;
}
Jonathan Applebaum
  • 5,738
  • 4
  • 33
  • 52
  • @Çöđěxěŕ I don't understand why it didn't work (in the example it returns false). also, the question is not relevant to a different culture the question is about identifying digit at the end of a string. I don't understand why downvote. – Jonathan Applebaum Nov 05 '19 at 21:49
0
var lastChar = text.Last();
var endsWithNum = lastChar >= '0' && lastChar <= '9';
Joe Sewell
  • 6,067
  • 1
  • 21
  • 34
-1
string s = "hello world0";
int len = s.Length;
List<char> digits = new List<char>(){'0','1','2','3','4','5','6','7','8','9'};
if (digits.Contains(s[len - 1]))
{
    Console.WriteLine("last character is digits");
}
SAR
  • 180
  • 1
  • 9