标签:nsstring
//初始化 //iniWithString------Returns an NSString object initialized by copying the characters from another given string. // 返回一个NSString对象初始化复制来自另一个给定字符串的字符。 NSString *str = @"liuyafang"; NSString *str1 = [[NSString alloc] initWithString:str]; NSLog(@"str1 = %@", str1); //计算字符串长度 //length------Returns the number of Unicode characters in the receiver. //返回接收器中Unicode字符的数量 NSInteger count = [str length]; //可见长度 NSLog( @"%ld", count); //initWithFormat: //Returns an NSString object initialized by using a given format string as a template into which the remaining argument values are substituted. //返回一个NSString对象初始化使用给定的格式字符串作为模板,其余参数值代替。 NSString *str2 = @"dapingmu"; NSString *str3 = [[NSString alloc] initWithFormat:@"liuyafang"]; NSLog(@"str2 = %@", str2); NSLog(@"str3 = %@", str3); //判断是否以指定字符开头或者结尾 BOOL pre = [str3 hasPrefix:@"liu"]; NSLog(@"%d", pre); BOOL suf = [str3 hasSuffix:@"fang"]; NSLog(@"%d", suf); //截取字符串的长度和起始位置----Finds and returns the range of the first occurrence of a given string within the receiver. // NSRange rag =[str3 rangeOfString:@"liu"]; NSLog(@"length = %ld, lacation = %ld", rag.length, rag.location); //截取从输入的起始位置开始输出 NSString *sub = [str3 substringFromIndex:3]; NSLog(@"%@", sub); //截取到输入的位置并输出 NSString *subb = [str3 substringToIndex:3]; NSLog(@"%@", subb); //截取一个范围字符串 NSRange aa = {0 , 4}; NSString *ran1 = [str3 substringWithRange:aa]; NSString *ran = [str3 substringWithRange:NSMakeRange(0, 5)]; //NSMakeRange(0, 5) 范围的起始位置和末尾位置 NSLog(@"%@", ran1); NSLog(@"%@", ran); //拼接字符串 NSString *str4 = @" very good !"; NSString *app = [str stringByAppendingString:str4]; NSLog(@"%@", app); NSLog(@"%@",str); NSString *b = [str stringByAppendingFormat:@"%@==%d", str,5]; //格式化拼接,,,有问题 ,, NSLog(@"%@",b); //替换字符串 NSString *rep = [str3 stringByReplacingOccurrencesOfString:@"ya" withString:@"xiao"]; NSLog(@"%@", rep); //转换成小写的 NSString *lowe= [@"ljlkmJNnhjnHhhbhHnbjjbnghUKJkj" lowercaseString]; NSLog(@"%@", lowe); //首字母转换成大写; NSString *cap = [@"ad Hj da ajda ajdl la " capitalizedString]; NSLog(@"%@",cap); NSMutableString *mutabal = [NSMutableString stringWithCapacity:5]; NSLog(@"%@", mutabal); NSMutableString *mutabal1 =[NSMutableString stringWithFormat:@"liuyafang"]; NSLog(@"%@", mutabal1); //可变字符串拼接 //Adds a constructed string to the receiver.---添加一个构造字符串到接收方。 [mutabal1 appendFormat:@"good"]; NSLog(@"%@", mutabal1); [mutabal1 appendFormat:@"%@", @"good"]; NSLog(@"%@", mutabal1); //Adds to the end of the receiver the characters of a given string.--增加了接收机的最后一个给定字符串的字符。 [mutabal1 appendString:@"what!"]; NSLog(@"%@", mutabal1); [mutabal1 stringByAppendingString:@"nimei"]; NSLog(@"----------%@", mutabal1); //删除范围字符串 // Removes from the receiver the characters in a given range.----删除来自接收者的角色在一个给定的范围内。 NSRange aaa = {0, 3}; [mutabal1 deleteCharactersInRange:aaa]; NSLog(@"%@", mutabal1); //NSMakeRange-------Creates a new NSRange from the specified values.--创建一个新的NSRange指定值。 [mutabal1 deleteCharactersInRange:NSMakeRange(0, 3)]; NSLog(@"%@",mutabal1); //Replaces the characters of the receiver with those in a given string.----替换字符的接收器与给定的字符串。 [mutabal1 setString:@"liu"]; NSLog(@"%@", mutabal1); //作业1。。判断是否以EfGk结尾,如果是替换成WXYZ,然后转变成小写 NSString *exercise = @"aBcD_EfGk"; BOOL a1 = [exercise hasSuffix:@"EfGk"]; NSLog(@"%d", a1); NSString *exer = [exercise stringByReplacingOccurrencesOfString:@"EfGk" withString:@"WXYZ"]; NSLog(@"---====>%@", exer); NSString *exer1 = [exer lowercaseString]; NSLog(@"------>%@",exer1); //作业1。2判断是都以png结尾,如果是替换成jpg,如果不是则添加.jpg NSMutableString *page = [NSMutableString stringWithFormat:@"xiaoliu.png"]; BOOL aa11 = [page hasSuffix:@"png"]; NSLog(@"%d",aa11); if ( aa11 == 1) { NSString *page1 = [page stringByReplacingOccurrencesOfString:@"png" withString:@"jpg"]; NSLog(@"%@", page1); }else{ [page appendString:@".jpg"]; NSLog(@"%@", page); } //数组初始化,,获取元素个数 NSArray *arr = [NSArray arrayWithObjects:@"ss",@"dd",@"aa", nil]; NSLog(@"%@", arr); NSInteger con = [arr count]; NSLog(@"%ld", con); // 根据对象获得所引致 //Returns the lowest index whose corresponding array value is equal to a given object.--- NSInteger dd = [arr indexOfObject:@"dd"]; NSLog(@"%ld", dd); //根据所引致获得对象 //Returns the object located at the specified index. NSArray *ppp = [arr objectAtIndex:0]; NSLog(@"0000000%@", ppp); ///-------------可变数组--------------/// NSMutableArray *mutarr = [NSMutableArray arrayWithCapacity:5]; NSLog(@"%@", mutarr); NSMutableArray *mt = [NSMutableArray arrayWithObjects:@"aaa", @"bbb", @"ccc", @"ddd", nil]; NSMutableArray *mm = [NSMutableArray arrayWithObjects:@"ee", @"qq", nil]; NSLog(@"%@", mt); //添加元素 //Inserts a given object at the end of the array. [mt addObject:@"fff"]; NSLog(@"%@", mt); //插入元素 // Inserts a given object into the array‘s contents at a given index. [mt insertObject:@"ooo" atIndex:2]; NSLog(@"%@", mt); //数组连接,,, //Adds the objects contained in another given array to the end of the receiving array’s content. [mt addObjectsFromArray:mm]; NSLog(@"%@", mt); //删除元素 [mt removeObjectAtIndex:2]; NSLog(@"%@", mt); //替换元素 [mt replaceObjectAtIndex:0 withObject:@"ttttt"]; NSLog(@"%@", mt); //交换两个指定位置对元素 [mt exchangeObjectAtIndex:0 withObjectAtIndex:1]; NSLog(@"%@", mt); //图书管理 // NSMutableArray *library = [NSMutableArray arrayWithObjects:@"tushu1",@"tushu2", @"tushu3", @"tushu4", nil]; // NSInteger count1 = [library count]; // NSInteger ddd; // NSLog(@"1-添加图书2-删除图书3-修改图书4-查找图书5-查看图书"); // scanf("%ld", &ddd); // if (ddd == 1) { // [library addObject:@"tushu5"]; // NSLog(@"%@", library); // } // if (ddd == 2) { // [library removeObject:@"tushu3"]; // NSLog(@"%@", library); // } // if (ddd == 3) { // [library setObject:@"tushu0" atIndexedSubscript:0]; // NSLog(@"%@", library); // } // if (ddd == 4) { // NSInteger place = [library indexOfObject:@"tushu3"]; // NSLog(@"%ld", place); // } // if (ddd == 5) { // for (int i = 0; i < count1; i++) { // NSLog(@"%@",library[i]); // } // } //正式作业1; NSString *jiequ = @"20|http://www.baidu.com"; NSString *neww = [jiequ substringFromIndex:3]; //从这个位置开始截取, 截取前面输出后面。 NSLog(@"%@", neww); NSString *new = [jiequ substringToIndex:2]; //截取到这个位置 NSLog(@"%@", new); //将文件改写成213 NSString *qing = @"文艺青年"; NSString *a213 = [qing stringByReplacingOccurrencesOfString:@"文艺" withString:@"213"]; NSLog(@"%@", a213);
NSString,NSSarray的常用用法,布布扣,bubuko.com
标签:nsstring
原文地址:http://liuyafang.blog.51cto.com/8837978/1541827