标签:
NSMutableString 继承于NSString 所以NSSting的所有方法NSMutableString都继承 并且也可以用
下面有两个方法NSString没有的
<span style="font-size:14px;">//长度为0 同样表示空的字符串 NSMutableString * str3 = [[NSMutableString alloc] initWithCapacity:0];</span>
<span style="font-size:14px;">//类方法的创建形式 空的 NSMutableString * str4 = [NSMutableString string];</span>//可变字符串的相关方法
<span style="font-size:14px;">//重置字符串 str1 = @"dsa"; NSMutableString * str1 = [[NSMutableString alloc] initWithString:@"dsa"]; // str1 = @"s1s2s3ss"; [str1 setString:@"s1s2s3ss"]; //格式化的形式追加字符串 str1 = @"s1s2s3ss1"; [str1 appendFormat:@"%d",1]; //追加一个字符串 str1 = @"s1s2s3ss1asd"; [str1 appendString:@"asd"]; //删除字符串 range = {3,3}; NSRange range = NSMakeRange(3, 3); // str1 = @"s1sss1asd"; 删除的字符串是s2s [str1 deleteCharactersInRange:range]; NSLog(@"%@",str1); //插入字符串 在下标为3的地方插入,这里对要插入的字符串长度没限制,不会覆盖掉原来后面的字符串 [str1 insertString:@"UUUUUU" atIndex:3]; NSLog(@"%@",str1); //str1 = @"s1sUUUUUUss1asd"; //替换字符串 [str1 replaceCharactersInRange:NSMakeRange(3, 7) withString:@"AAAAAAAAAAA"]; NSLog(@"%@",str1); //str1 = @"s1sAAAAAAAAAAAs1asd"; </span>
<span style="font-size:14px;">/* 10.将原字符串进行小写转大写,大写转小写 */ NSMutableString * str101 = [[NSMutableString alloc]initWithFormat:@"%@",@"HeHeDa"]; NSMutableString * str102 = [[NSMutableString alloc] init]; for (int i = 0; i < str101.length; i++) { unichar tempChar = [str101 characterAtIndex:i]; if (tempChar >= 65 && tempChar <= 90) { NSMutableString * tempString1 = [[NSMutableString alloc] initWithFormat:@"%c",tempChar]; NSString * str103 = [tempString1 lowercaseString]; [str102 appendString:str103]; } else{ NSMutableString * tempString2 = [[NSMutableString alloc] initWithFormat:@"%c",tempChar]; NSString * str104 = [tempString2 uppercaseString]; [str102 appendString:str104]; } } NSLog(@"%@",str102); /* 11.对字符串按字母顺序进行排序 比如输入@“aczabczab”输出@“aaabbcczz” */ NSMutableString * str111 = [[NSMutableString alloc]initWithFormat:@"%@",@"aczabczab"]; for (int i = 0; i < str111.length - 1; i++) { for (int j = i + 1; j < str111.length; j++) { unichar ci = [str111 characterAtIndex:i]; unichar cj = [str111 characterAtIndex:j]; if (ci > cj) { NSRange iRange = {i,1}; NSRange jRange = {j,1}; [str111 replaceCharactersInRange:iRange withString:[[NSString alloc] initWithFormat:@"%c",cj]]; [str111 replaceCharactersInRange:jRange withString:[[NSString alloc] initWithFormat:@"%c",ci]]; } } } NSLog(@"%@",str111); /* 12.使用NSLog打印99乘法表 */ for (int i = 1; i < 10; i++) { NSMutableString * str121 = [[NSMutableString alloc] init]; for (int j = 1; j <= i; j++) { [str121 appendFormat:@"%d*%d=%d\t",j,i,i * j]; } NSLog(@"%@",str121); } </span>
版权声明:本文为博主原创文章,未经博主允许不得转载。
Snail—OC学习之可变字符串NSMutableString
标签:
原文地址:http://blog.csdn.net/qq1791422018/article/details/46973059