参考:
加,剪,截,替,查 ,删
1.“加”
--
resultCode = [array componentsJoinedByString:@","];
这个用得挺多的,以某个特殊的符号(如@","),把数组元素拼接一条字符串
而且数组元素里面不一定是NSString 类型喔
以下是测试:
//数组装了NSNumber类型的,貌似数组只能装类,不能直接装基本数据类型
NSArray *arr = @[@(1),@(3)];
NSString * test = @"";
test = [arr componentsJoinedByString:@","];
NSLog(@"___%@",test);
打印:
2016-03-18 10:13:34.259 test[4946:59102] ___1,3
1.1 在已有字符串后面添加字符
//appendString: and appendFormat:
NSMutableString *String1 = [[NSMutableString alloc] initWithString:@"This is a NSMutableString"];
//[String1 appendString:@", I will be adding some character"];
[String1 appendFormat:@"%@", [NSString stringWithFormat:@"%@\n",changeStr]];
*/
1.2在已有字符串后面在所指定的位置中插入给出的字符串
//-insertString: atIndex:
NSMutableString *String1 = [[NSMutableString alloc] initWithString:@"This is a NSMutableString"];
[String1 insertString:@"Hi! " atIndex:0];
NSLog(@"String1:%@",String1);
2.“剪”
self.test = [[code componentsSeparatedByString:@"#"] firstObject];
self.test = [[code componentsSeparatedByString:@"#"] lastObject];
componentsSeparatedByString
返回的是一个数组
另外
没有得“剪”,会保留原来的字符串的,所以大方的“剪”即可
3.查
NSString *string1 = @"This is a string";
NSString *string2 = @"string";
NSRange range = [string1 rangeOfString:string2];
int location = range.location;
int leight = range.length;
NSString *astring = [[NSString alloc] initWithString:[NSString stringWithFormat:@"Location:%i,Leight:%i",location,leight]];
NSLog(@"astring:%@",astring);
NSString *str1 = @"can you \n speak English";
NSString *str = @"\n";
//在str1这个字符串中搜索\n,判断有没有
if ([str1 rangeOfString:str].location != NSNotFound) {
NSLog(@"这个字符串中有\n");
}
//rangeOfString 前面的参数是要被搜索的字符串,后面的是要搜索的字符
//NSNotFound 表示请求操作的某个内容或者item没有发现,或者不存在
//01:检查字符串是否以另一个字符串开头- (BOOL) hasPrefix: (NSString *) aString;
NSString *String1 = @"NSStringInformation.txt";
[String1 hasPrefix:@"NSString"] = = 1 ? NSLog(@"YES") : NSLog(@"NO");
[String1 hasSuffix:@".txt"] = = 1 ? NSLog(@"YES") : NSLog(@"NO");
当然写法是很灵活的比如:
//是否包含其它字符
NSString *astring = [[NSString alloc] initWithString:@"This is a String!"];
Boolean contains = [astring rangeOfString:@”This”].length>0;
ps:rangeOfString这个方法有遍历的意思,但是不得不吐槽的是拿到的range.location还有用,但是range.length这个只能拿到该搜索字符串的长度,貌似真没啥用出
4.截
包括:4.1 截到那里(to);
4.2从哪里开始截取(from);
4.3 截取的范围(range);
//-substringToIndex: 从字符串的开头一直截取到指定的位置,但不包括该位置的字符
NSString *string1 = @"This is a string";
NSString *string2 = [string1 substringToIndex:3];
NSLog(@"string2:%@",string2);
//-substringFromIndex: 以指定位置开始(包括指定位置的字符),并包括之后的全部字符
NSString *string1 = @"This is a string";
NSString *string2 = [string1 substringFromIndex:3];
NSLog(@"string2:%@",string2);
//-substringWithRange: //按照所给出的位置,长度,任意地从字符串中截取子串
NSString *string1 = @"This is a string";
NSString *string2 = [string1 substringWithRange:NSMakeRange(0, 4)];
NSLog(@"string2:%@",string2);
特定字符串之间截取
NSString *string1 = @"This is a string";
NSRange end = [string1 rangeOfString:@"string"];
NSRange start = [string1 rangeOfString:@"This"];
NSString *string2 = [string1 substringWithRange:NSMakeRange(start.location, end.location)];
NSLog(@"string2:%@",string2);
5.替
NSString *strUrl = [urlString stringByReplacingOccurrencesOfString:@" " withString:@""]; 去掉空格
NSString *strUrl = [urlString stringByReplacingOccurrencesOfString:@" " withString:@"/"];替换字符
- (void)viewDidLoad
{
[super viewDidLoad];
NSString *str0 = @"中文my name is xuanyusong";
NSString *temp = @"is";
NSRange rang = [str0 rangeOfString:temp];
NSLog(@"搜索的字符串在str0中起始点的index 为 %d", rang.location);
NSLog(@"搜索的字符串在str0中结束点的index 为 %d", rang.location + rang.length);
//将搜索中的字符串替换成为一个新的字符串
NSString *str = [str0 stringByReplacingCharactersInRange:rang withString:@"哇咔咔卡卡咔"];
NSLog(@"替换后字符串为%@", str);
//将字符串中" " 全部替换成 *
str = [str0 stringByReplacingOccurrencesOfString :@" " withString:@"@"];
NSLog(@"替换后字符串为%@", str);
}
"CharactersInRange"和"OccurrencesOfString"都是替换,区别,"CharactersInRange"是目标范围替换,"OccurrencesOfString"是全替
举个例子:电话号码的星号替换
//substringWithRange:NSMakeRange(3,4) 从第三位开始截取四个字符
NSString *a = @"13781213137";
NSString *string=[a stringByReplacingOccurrencesOfString:[a substringWithRange:NSMakeRange(3,4)]withString:@"****"];
NSLog(@"b:%@",string);
Log:
b:137****3137
还有就是借助NSScanner来替
- (NSString *)filterHTML:(NSString *)html
{
NSScanner * scanner = [NSScanner scannerWithString:html];
NSString * text = nil;
while([scanner isAtEnd]==NO)
{
[scanner scanUpToString:@"<" intoString:nil];
[scanner scanUpToString:@">" intoString:&text];
html = [html stringByReplacingOccurrencesOfString:[NSString stringWithFormat:@"%@>",text] withString:@""];
}
return html;
}
6.删
在已有字符串中按照所给出范围和长度删除字符
//deleteCharactersInRange:
NSMutableString *String1 = [[NSMutableString alloc] initWithString:@"This is a NSMutableString"];
[String1 deleteCharactersInRange:NSMakeRange(0, 5)];
NSLog(@"String1:%@",String1);