检查目标C中字符数组的长度

Mal*_*ion 1 objective-c ios

我正在翻译一个小的java库,用于我正在编写的目标c应用程序.

char[] chars = sentence.toCharArray();
int i = 0;
while (i < chars.length) { ... }
Run Code Online (Sandbox Code Playgroud)

句子是NSString.我想将上面的java代码翻译成目标c.这是我到目前为止所拥有的:

sentence = [sentence stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]; // trims sentence off white space    
const char *chars = [sentence UTF8String];
Run Code Online (Sandbox Code Playgroud)

我如何在上述条件下?我不确定在将字符串转换为字符数组后我应该如何检查字符串的长度.

Ram*_*uri 7

Your Objective-C string already holds a measure of it's length, it just a matter of retrieving it:

sentence = [sentence stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]; // trims sentence off white space    
NSUInteger length= sentence.length;
const char *chars = [sentence UTF8String];
Run Code Online (Sandbox Code Playgroud)

But I would like to remember that even if you didn't know the length, you could use the C strlen function:

sentence = [sentence stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]; // trims sentence off white space    
const char *chars = [sentence UTF8String];
size_t length= strlen(chars);
Run Code Online (Sandbox Code Playgroud)