Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the length in bytes of a NSString?

Tags:

How do I get the bytes length of NSString? if myString contains "hallo", myString.length will return 5, but how many actual bytes are taken?

like image 818
ticofab Avatar asked Jun 28 '12 12:06

ticofab


People also ask

What is an NSString?

A static, plain-text Unicode string object that bridges to String ; use NSString when you need reference semantics or other Foundation-specific behavior.

How do you find the length of a string in Objective C?

int len = [myString length];


2 Answers

NSString *test=@"ABCDEFGHIJKLMNOPQRSTUVWXYZ"; NSUInteger bytes = [test lengthOfBytesUsingEncoding:NSUTF8StringEncoding]; NSLog(@"%i bytes", bytes); 
like image 77
Sanchit Paurush Avatar answered Sep 27 '22 17:09

Sanchit Paurush


To get the bytes use

NSData *bytes = [string dataUsingEncoding:NSUTF8StringEncoding]; 

Then you can check bytes.length

Number of bytes depend on the string encoding

like image 22
Omar Abdelhafith Avatar answered Sep 27 '22 15:09

Omar Abdelhafith