如何检查是否一个字符串是多少? [英] How to check if a string is a number?

查看:252
本文介绍了如何检查是否一个字符串是多少?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要检查,如果一个字符串与此code数字。我必须检查字符串中的所有字符都是整数,但同时回报总是ISDIGIT = 1。我不知道是什么原因,如果不工作。

 字符TMP [16];
scanf函数(%S,TMP);INT ISDIGIT = 0;
INT J = 0;
而(J<的strlen(TMP)及和放大器; ISDIGIT == 0){
  如果(TMP [J]> 57安培;&安培; TMP [J]< 48)
    ISDIGIT = 0;
  其他
    ISDIGIT = 1;
  J ++;
}


解决方案

忘记ASCII code盘,使用 ISDIGIT ISNUMBER (见 男人ISNUMBER )。第一个功能检查字符是否为0-9,第二个也接受其他各种字符数取决于当前的语言环境。

有可能甚至更好的功能做检查 - 重要的教训是,这是一个有点复杂得多,它看起来,因为数字串的precise定义依赖于特定的语言环境和字符串编码。

I want to check if a string is a number with this code. I must check that all the chars in the string are integer, but the while returns always isDigit = 1. I don't know why that if doesn't work.

char tmp[16];
scanf("%s", tmp);

int isDigit = 0;
int j=0;
while(j<strlen(tmp) && isDigit == 0){
  if(tmp[j] > 57 && tmp[j] < 48)
    isDigit = 0;
  else
    isDigit = 1;
  j++;
}

解决方案

Forget about ASCII code checks, use isdigit or isnumber (see man isnumber). The first function checks whether the character is 0–9, the second one also accepts various other number characters depending on the current locale.

There may even be better functions to do the check – the important lesson is that this is a bit more complex than it looks, because the precise definition of a "number string" depends on the particular locale and the string encoding.

这篇关于如何检查是否一个字符串是多少?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆