All these code examples produce the following output.
5
😀
import Foundation
let test = "Hi😀!" as NSString
let location = 2
let length = 2
print(test.length)
print(test.substring(with: NSRange(location: location, length: length)))
#import <Foundation/Foundation.h>
int main(int argc, char *argv[]) {
@autoreleasepool {
NSString *test = @"Hi😀!";
NSInteger location = 2;
NSInteger length = 2;
printf("%lu\n", [test length]);
printf("%s\n", [[test substringWithRange:NSMakeRange(location, length)] cStringUsingEncoding:NSUTF8StringEncoding]);
}
}
class Parse {
public static void main(String[] args) {
String test = "Hi😀!";
int location = 2;
int length = 2;
System.out.println(test.length());
System.out.println(test.substring(location, location+length));
}
}
fun main(args: Array<String>) {
val test = "Hi😀!"
val location = 2;
val length = 2;
println(test.length)
println(test.substring(location, location+length))
}
let test = "Hi😀!"
let location = 2
let length = 2
console.log(test.length)
console.log(test.substr(location, length))
<?php
$test = "Hi😀!";
$location = 2;
$length = 2;
echo (strlen(mb_convert_encoding($test, 'UTF-16', 'UTF-8'))/2)."\n";
echo mb_convert_encoding(substr(mb_convert_encoding($test, 'UTF-16', 'UTF-8'), $location*2, $length*2), 'UTF-8', 'UTF-16')."\n";
fun main(args: Array<String>) {
val test = "Hi😀!"
val location = 2;
val length = 2;
println(test.length)
println(test.substring(location, location+length))
}