How do we find out the length (byte-wise) of a string [declared String type, not NSString]
in Swift 2.2?
I know one way out is to use _bridgeToObejectiveC().length
Is there any other way out?
How do we find out the length (byte-wise) of a string [declared String type, not NSString]
in Swift 2.2?
I know one way out is to use _bridgeToObejectiveC().length
Is there any other way out?
let str: String = "Hello, World"
print(str.characters.count) // 12
let str1: String = "Hello, World"
print(str1.endIndex) // 12
let str2 = "Hello, World"
NSString(string: str2).length //12
Refer String length in Swift 1.2 and Swift 2.0 and Get the length of a String
let str: String = "Hello, World"
print(str.characters.count) // 12
let byteLength = str.lengthOfBytesUsingEncoding(NSUTF8StringEncoding)
print("byte lenght: \(byteLength)")//12
let str2: String = "你好"
print(str2.characters.count) // 2
let byteLength2 = str.lengthOfBytesUsingEncoding(NSUTF8StringEncoding)
print("byte lenght: \(byteLength2)")//12
Try this code:
Swift
let test : String = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
let bytes : NSInteger = test.lengthOfBytesUsingEncoding(NSUTF8StringEncoding);
NSLog("%i bytes", bytes);
Objective C
refer this link:
I think type casting is another easier way to use the methods, properties of NSString class.
print((str as NSString).length)