#import <Foundation/Foundation.h> //1 创建字符串的方式 int main(int argc, const char * argv[]) { @autoreleasepool { NSString * str = @"oc str"; NSLog(@"str %@",str); char * c = "c str"; char * c1 = "我是c语言字符串"; NSString * str1 = [[NSString alloc] initWithCString:c1 encoding:NSUTF8StringEncoding]; NSLog(@"str1 %@",str1); int i = 10; NSString * str2 = [[NSString alloc] initWithFormat:@"format %@ %d",str1,i]; NSLog(@"str2 %@",str2); int year = 2016; int month = 09; int day = 20; NSString * str3 = [[NSString alloc] initWithFormat:@"%d年%02d月%d日",year,month,day]; NSLog(@"%@",str3); } return 0; }
/* 1> 获取字符串的长度 2 >根据索引获得单个字符 3 >根据索引获得一个子串 4 >根据字符串获得该字符串在另一个字符串中的索引位置 */ #import <Foundation/Foundation.h> int main(int argc, const char * argv[]) { @autoreleasepool { NSString * str = @"baidu"; NSUInteger count = [str length]; NSLog(@"%lu",count); char c = [str characterAtIndex:0]; NSLog(@"%d",c); NSString * substr1 = [str substringFromIndex:3]; NSLog(@"substr1 %@",substr1); NSString * substr2 = [str substringToIndex:3]; NSLog(@"substr2 %@",substr2); NSRange range = {2,3}; /* NSUInteger location 起始位置 NSUInteger length 长度 */ // substringWithRange 从loccation开始的length个字符 NSString * substr3 = [str substringWithRange:range]; NSLog(@"%@",substr3); NSString * str1 = @"www.baidu.com"; NSRange range2 = [str1 rangeOfString:str]; NSLog(@"%@",NSStringFromRange(range2)); } return 0; }
版权声明:本文为博主原创文章,未经博主允许不得转载。
时间: 2024-10-10 17:07:37