返回顶部

收藏

Objective-C字符串操作

更多
void testString(){
    // String str = "banxi1988";
    NSString* str = @"banxi1988";
    NSString* bstr = @"banxi1988";
    // str.equals(bstr);
    BOOL same = [str isEqualToString:bstr];
    NSLog(@"isSame:%d",same);
    // str.startsWith("banxi");
    BOOL hasPrefix = [str hasPrefix:@"banxi"];
    NSLog(@"hasPrefix:%d",hasPrefix);
    // str.length()
    NSLog(@"str length:%lu",[str length]);
    // str.charAt(int)
    NSLog(@"char at 1:%hu",[str characterAtIndex:1]);
    NSLog(@"compare:%ld",[str compare:@"banxi1989"]);
    // str.compareToIgnoreCase(String)
    NSLog(@"compareInsensitive:%ld",[str caseInsensitiveCompare:@"BANXI1988"]);
    // str.toUpperCase()
    NSLog(@"uppercaseString:%@",[str uppercaseString]);
    // str.toLowerCase()
    NSLog(@"lowercaseString:%@",[@"BANXI" lowercaseString]);
    // str.substring(int)
    NSLog(@"substringFromIndex:%@",[str substringFromIndex:1]);
    // str.substring(int, int)
    NSLog(@"substringWithRange:%@", [str substringWithRange:NSMakeRange(0, 2)]);
    NSLog(@"stringByTrimmingCharactersInSet:%@",[@" banxi " stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]]);
    // String.format(String,Object...)
    NSLog(@"stringWithFormat:%@",[NSString stringWithFormat: @"%d is less than %@",3,@"yes"]);
    // str.concat(String)
    NSLog(@"stringByAppendingString:%@",[str stringByAppendingString:@"@gmail.com"]);
}
//该片段来自于http://outofmemory.cn

标签:c++,手机开发

收藏

0人收藏

支持

0

反对

0