1. 程式人生 > >判斷字串位元組數(中英文)

判斷字串位元組數(中英文)

// 判斷字串長度(包含中英文)
- (int)convertToInt:(NSString *)string
{
    int strlength = 0;
    char* p = (char*)[string cStringUsingEncoding:NSUnicodeStringEncoding];
    for (int i=0 ; i<[string lengthOfBytesUsingEncoding:NSUnicodeStringEncoding] ;i++) {
        if (*p) {
            p++;
            strlength++;
        }
        else
{ p++; } } return strlength; }
// 判斷字串包含漢字的個數
- (int)chineseNumber:(NSString *)str
{
    int t = 0;
    for ( int i = 0; i < str.length; i++) {
        unichar ch = [str characterAtIndex:i];
        if (0x4e00 < ch  && ch < 0x9fff)
        {
            t++;

        }
    }
    return
t; }
// 判斷字串是否為純數字
- (BOOL)isPureNumandCharacters:(NSString *)string
{
    string = [string stringByTrimmingCharactersInSet:[NSCharacterSet decimalDigitCharacterSet]];
    if(string.length > 0)
    {
        return NO;
    }
    return YES;
}