1. 程式人生 > >關於IOS判斷字串長度的方法

關於IOS判斷字串長度的方法

 
    NSString *[email protected]"helloworld你好嗎";//16
    NSUInteger length= text.length;
    NSUInteger length2=[text lengthOfBytesUsingEncoding:NSUTF8StringEncoding];
    NSLog(@"length=%lu,length2=%lu,length3=%lu",(unsigned long)length ,(unsigned long)length2,[self unicodeLengthOfString:text]);

-(NSUInteger) unicodeLengthOfString: (NSString *) text {
    NSUInteger asciiLength = 0;
    for (NSUInteger i = 0; i < text.length; i++) {
        unichar uc = [text characterAtIndex: i];
        asciiLength += isascii(uc) ? 1 : 2;
    }
    NSUInteger unicodeLength = asciiLength / 2;
    if(asciiLength % 2) {
        unicodeLength++;
    }
    return asciiLength;
}

相關推薦

關於IOS判斷字串長度方法

NSString *[email protected]"helloworld你好嗎";//16 NSUInteger length= text.length; NSUInteger length2=[text lengthOfBytes

iOS 判斷字串是否含有Emoji表情-iOS12

iOS 判斷字串是否含有Emoji表情 測試 iOS12 表情. 在蘋果新的表情中, 表情字串長度在1~14位. 以下只命中第一位, 如果發現錯誤命中, 請加入多位判斷. - (BOOL)stringContainsEmoji:(NSString *)string {

[iOS]判斷字串是否為合法郵箱地址

- (IBAction)saveEmailAction:(id)sender { if ([self isValidateEmail:_mailField.text]) { [self.delegate saveEmail:_mailField.text];

shell 判斷字串長度是否為0

  test.sh #!/bin/bash echo "enter the string:" read filename if test -z $filename ; then echo "the length is 0" else echo "the length is not 0" f

JS判斷字串長度(英文佔1個字元,中文漢字佔2個字元)

//計算字串長度(英文佔1個字元,中文漢字佔2個字元) 方法一: [javascript] view plain copy String.prototype.gblen = function() {         var len = 0;         

js判斷字串長度

function countLength(str) { var inputLength = 0; //給一個變數來記錄長度 for (var i = 0; i < str.length; i++) {

php判斷字串長度 strlen()與mb_strlen()函式

我們知道在php中要判斷字串長度我們只要使用strlen()與mb_strlen()函式既可以了,但是並不是我們想得這麼簡單,特別是在對中英文混合時上面函式有點力不從心哦,下面我來給各位朋友介紹。 strlen() PHP strlen() 函式 定義和用法 s

iOS判斷字串是否包含emoji表情

- (BOOL)isContainsEmoji:(NSString *)string { __block BOOL isEomji = NO; [string enumerateSubs

ios 判斷字串是否全部空格

//A character set containing only the whitespace characters space (U+0020) and tab (U+0009) and the newline and nextline characters (U+000A–U+000D, U+008

iOS 判斷字串中含有某個字串

//判斷字串是否包含“iOS”方式一:containString查詢字串是否包含“ iOS ”,適用於IOS8及以上,以下的系統會因為沒有此API而報錯。 NSString*str = @“我是 iO

iOS 判斷字串是不是URL

- (BOOL)isValidUrl { NSString *regex [email protected]"[a-zA-z]+://[^\\s]*"; NSPredicate *urlTest = [NSPredicate predicateWi

iOS判斷字串中是否有中文及處理方式

//判斷是否有中文 -(BOOL)hasChinese:(NSString *)str { for(int i=0; i< [str length];i++){ int

iOS判斷字串中包含數字和字母的幾種情況

//直接呼叫這個方法就行 -(int)checkIsHaveNumAndLetter:(NSString*)password{ //數字條件 NSRegularExpression *tNum

在JSP頁面中使用標籤判斷字串長度的問題

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%><%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%>

iOS--判斷字串NSString中數字、中文、大小寫英文

NSString *testString = @"中文123ljfLJF"; NSInteger alength = [testString length]; for (int i

struts s:if 擷取字串判斷字串長度

 <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <%@ taglib prefix="s" uri="/struts-tags" %> <% Strin

iOS判斷字串是否包含空格

NSString *_string = [NSString stringWithFormat:@"123 456"]; NSRange _range = [_string rangeOfString:

iOS 判斷字串是否為空字串 && 判斷字串包含那個字元

- (BOOL) isBlankString:(NSString *)string { if (string == nil || string == NULL) { return YES; } if ([string isKindOf

js,判斷字串長度

//功能:統計包含漢字的字元個數 //說明:漢字佔2個字元,非漢字佔1個字元 function checksum(chars) {     var sum = 0;     for (var i=0

iOS -轉載-字串是否為空判斷方法

- (BOOL)blankString{ if (![self isKindOfClass:[NSString class]] ){ return YES; } if ([self isEqual:[NSNull null]]){ return