ภาพรวม
iswdigit() เป็นฟังก์ชันใน C++ STL (header <cwctype>) ใช้ตรวจสอบว่า Wide Character เป็นตัวเลขทศนิยม (Decimal Digit: 0-9) หรือไม่
Syntax
int iswdigit(wint_t c);
พารามิเตอร์
c- Wide character ที่ต้องการตรวจสอบ (ชนิดwint_tหรือWEOF)
ค่าที่คืน
- Non-zero (true) - ถ้า c เป็นตัวเลข 0-9
- 0 (false) - ถ้าไม่ใช่ตัวเลข
ตัวอย่างที่ 1: ตรวจสอบตัวอักษรเดี่ยว
#include <cwctype>
#include <iostream>
using namespace std;
int main() {
wchar_t c1 = L'2';
wchar_t c2 = L'*';
if (iswdigit(c1))
wcout << c1 << L" is a digit";
else
wcout << c1 << L" is not a digit";
wcout << endl;
if (iswdigit(c2))
wcout << c2 << L" is a digit";
else
wcout << c2 << L" is not a digit";
return 0;
}
ผลลัพธ์
2 is a digit
* is not a digit
ตัวอย่างที่ 2: แปลง String เป็น Long Integer
#include <stdio.h>
#include <wchar.h>
#include <wctype.h>
int main() {
wchar_t str[] = L"1776ad";
long int year;
if (iswdigit(str[0])) {
year = wcstol(str, NULL, 10);
wprintf(L"The year that followed %ld was %ld.\n", year, year + 1);
}
return 0;
}
ผลลัพธ์
The year that followed 1776 was 1777
คำอธิบายเพิ่มเติม
- ตรวจสอบเฉพาะ 0-9 เท่านั้น (ไม่รวมตัวเลขในภาษาอื่น เช่น ๐-๙)
- ใช้ร่วมกับ
wcstol(),wcstoll()สำหรับแปลง String เป็นตัวเลข - สำหรับ ASCII ธรรมดา ใช้
isdigit()(header<cctype>) แทนได้ - Wide version เหมาะสำหรับการประมวลผล Unicode/International text
ฟังก์ชันเกี่ยวข้อง
| ฟังก์ชัน | หน้าที่ |
|---|---|
iswdigit() | ตรวจสอบตัวเลข 0-9 |
iswxdigit() | ตรวจสอบ Hex digit (0-9, A-F, a-f) |
iswalnum() | ตรวจสอบตัวอักษรหรือตัวเลข |
iswalpha() | ตรวจสอบตัวอักษร |