C++判断String是否以数字开头
时间: 2023-05-21 16:03:17 浏览: 248
可以使用isdigit()函数来判断一个字符串是否以数字开头,示例代码如下:
```c++
#include <iostream>
#include <string>
using namespace std;
bool isStartWithDigit(string str) {
if (isdigit(str[0])) {
return true;
}
return false;
}
int main() {
string str1 = "123abc";
string str2 = "abc123";
if (isStartWithDigit(str1)) {
cout << str1 << " starts with digit." << endl;
} else {
cout << str1 << " does not start with digit." << endl;
}
if (isStartWithDigit(str2)) {
cout << str2 << " starts with digit." << endl;
} else {
cout << str2 << " does not start with digit." << endl;
}
return 0;
}
```
输出结果为:
```
123abc starts with digit.
abc123 does not start with digit.
```
阅读全文