C语言字符串
字符串必须用双引号包含字符串中的每个字符占一个字节,ASCIIC语言字符串的末尾有一个隐藏的’\0’打印C语言的字符串用 %s 占位符,传递字符串的首地址即可
OC字符串对象(NSString)
字符串前面加上 @打印OC的字符串对象用 %@OC字符串中对象中的每一个字符都是uichar字符,uichar字符符合unicode编码Utf8编码存储字符串,不需要考虑字符存储
#import <Foundation/Foundation.h>
int main(
int argc,
const char * argv[]) {
@autoreleasepool {
char *cstring =
"Hello World 中国";
printf(
"%c\n",cstring[
7]);
printf(
"%ld\n",
sizeof(
"Hello World"));
NSString *ocstring = @
"Hello World 中国";
NSLog(@
"ocstring = %@",ocstring);
NSString *OCStr = [[
NSString alloc] initWithUTF8String:
"Welcome to China"];
NSLog(@
"OCStr = %@",OCStr);
NSString *OCStr1 = [[
NSString alloc] initWithFormat:@
"%s##%d##%@",
"Hello",
888,@
"China"];
NSLog(@
"OCStr1 = %@",OCStr1);
NSString *OCStr2 = @
"上海火车站";
NSString *OCStr3 = [[
NSString alloc] initWithString:OCStr2];
NSLog(@
"OCStr3 = %@",OCStr3);
NSString *OCStr4 = [[
NSString alloc] initWithCString:
"中国教育" encoding:NSUTF8StringEncoding];
NSLog(@
"OCStr4 = %@",OCStr4);
NSString *OCStr5 = [
NSString stringWithUTF8String:
"Hello World!"];
NSLog(@
"OCStr5 = %@",OCStr5);
NSString *OCStr6 = [
NSString stringWithString:OCStr2];
NSLog(@
"OCStr6 = %@",OCStr6);
NSString *OCStr7 = [
NSString stringWithFormat:@
"%d,%.2f,%s",
125,
45.69,
"Hello World"];
NSLog(@
"OCStr7 = %@",OCStr7);
NSString *OCStr8 = [
NSString stringWithCString:
"I am a good boy 来自中国" encoding:NSUTF8StringEncoding];
NSLog(@
"OCStr8 = %@",OCStr8);
NSString *str = @
"welcome 中国";
NSInteger len = [str length];
NSLog(@
"len = %li",len);
unichar ch = [str characterAtIndex:
3];
NSLog(@
"ch = %C",ch);
NSString *OCStr9 = [str stringByAppendingString:@
"你好"];
NSString *OCStr10 = [str stringByAppendingFormat:@
"%d%s",
123,
" World"];
NSLog(@
"OCStr10 = %@",OCStr10);
NSLog(@
"subString = %@" , [OCStr10 substringFromIndex:
4]);
NSLog(@
"subString = %@" , [OCStr10 substringToIndex:
4]);
NSRange range1 = {
4,
6};
NSLog(@
"subString = %@" , [OCStr10 substringWithRange:range1]);
NSLog(@
"subString = %@" , [OCStr10 substringWithRange:NSMakeRange(
4,
6)]);
NSString *OCStr11 = @
"bai du hello world bai du";
NSRange range2 = [OCStr11 rangeOfString:@
"we"];
if (range2
.location ==
NSNotFound) {
NSLog(@
"NSNotFound = %li" ,
NSNotFound);
}
else{
NSLog(@
"查找该子串,location = %lu , length = %lu" , range2
.location , range2
.length);
}
NSRange range3 = [OCStr11 rangeOfString:@
"du" options:NSBackwardsSearch];
NSLog(@
"查找到该子串,location = %lu , length = %lu" , range3
.location , range3
.length);
NSRange range4 = [OCStr11 rangeOfString:@
"du" options:NSLiteralSearch range:NSMakeRange(
7,
14)];
NSLog(@
"查找到该子串,location = %lu , length = %lu" , range4
.location , range4
.length);
NSString *OCStr12 = @
"hello World!";
NSLog(@
"str = %@" , [OCStr12 uppercaseString]);
NSLog(@
"str = %@" , [OCStr12 lowercaseString]);
NSLog(@
"str = %@" , [OCStr12 capitalizedString]);
NSLog(@
"num = %i" , [@
"1234asd" intValue]);
NSLog(@
"num = %li" , [@
"123456789" integerValue]);
NSLog(@
"num = %.2f" , [@
"3.1456" floatValue]);
NSString *Str = @
"hello world china hello world";
NSLog(@
"str = %@" , [Str stringByReplacingCharactersInRange:NSMakeRange(
12,
5) withString:@
"welcome"]);
NSLog(@
"str = %@" , [Str stringByReplacingOccurrencesOfString:@
"world" withString:@
"baidu"]);
NSLog(@
"str = %@" , [Str stringByReplacingOccurrencesOfString:@
"hello" withString:@
"HELLO" options:NSLiteralSearch range:NSMakeRange(
12,
17)]);
}
return 0;
}