ios字符串的使用

componentsJoinedByString 和 componentsSeparatedByString 的方法的区别

将string字符串转换为array数组

 NSArray  *array = [Str componentsSeparatedByString:@","];

==反向方法

将array数组转换为string字符串

 NSString *tempString = [mutableArray componentsJoinedByString:@","];--分隔符

1.字符串的比较

//*****************C字符串的比较
char *p = "jos";
char *q = "ios";

int ret = strcmp(p, q);//ret = p - q
if (ret > 0) {
    NSLog(@"p > q");
} else if (ret < 0) {
    NSLog(@"p < q");
} else {
    NSLog(@"p == q");
}

//*****************比较相等与否
NSString * str1 = @"I love ios";
NSString * str2 = @"I love ios";
if ([str1 isEqualToString:str2]) {
    NSLog(@"相等");
}else{
    NSLog(@"不相等");
}

//*****************OC字符串比较大小
NSString * str3 = @"how do you do";
NSString * str4 = @"how do you do";
NSComparisonResult result = [str3 compare:str4];
if (result == NSOrderedSame) {
    NSLog(@"str3 == str4");
}else if (result == NSOrderedAscending){
    NSLog(@"str3 < str4");
}else if (result == NSOrderedDescending){
    NSLog(@"str3 > str4");
}

//*****************不区分大小写的比较
NSString * str5 = @"how are you";
NSString * str6 = @"HOW ARE YOU";
NSComparisonResult result1 = [str5 caseInsensitiveCompare:str6];
if (result1 == NSOrderedSame) {
    NSLog(@"str5 == str6");
}else if (result1 == NSOrderedAscending){
    NSLog(@"str5 < str6");
}else if (result1 == NSOrderedDescending){
    NSLog(@"str5 > str6");
}

NSString * str = @"I love iOS";

//小写转大写
NSString * str1 = [str uppercaseString];

//大写转小写
NSString * str2 = [str lowercaseString];

//首字母大写
NSString * str3 = [str capitalizedString];

//字符串范围
NSRange range = [str rangeOfString:@"love"];//空格也要算在内
//位置range.location == 2,长度range.length == 4

//提取字符串(第一个字符的下标是0)
NSString * str4 = [str substringFromIndex:3];//截取从第3个字符开始到结尾(即:ove iOS)
NSString * str5 = [str substringToIndex:5];//截取前5个字符(即:I lov)
NSString * str6 = [str substringWithRange:NSMakeRange(2, 4)];//截取从第2个字符起往后数4个字符(即:love)

//判断字符串首尾
if ([str hasPrefix:@"I love"]) {
    NSLog(@"是以I love开头");
}
if ([str hasSuffix:@"iOS"]) {
    NSLog(@"是以iOS结尾");
}

//字符串的追加1(追加到尾部)
NSMutableString * mStr1 = [[NSMutableString alloc] initWithString:@"I love iOS"];
[mStr1 appendString:@" very much"];  
//结果:mStr1 = @"I love iOS very much"

//字符串的追加2(追加到尾部)
NSMutableString * mStr2 = [[NSMutableString alloc] initWithString:@"I love iOS"];
int i = 2;
[mStr2 appendFormat:@" %d", i];  
//结果:mStr2 = @"I love iOS 2�"

//字符串的插入
NSMutableString * mStr3 = [[NSMutableString alloc] initWithString:@"abcdefg"];
[mStr3 insertString:@"yyy" atIndex:3];
//结果:mStr3 = @"abcyyydefg"

//字符串的删除
NSMutableString * mStr4 = [[NSMutableString alloc] initWithString:@"abcdefg"];
[mStr4 deleteCharactersInRange:NSMakeRange(3, 2)];
//结果:mStr4 = @"abcfg"

//字符串的替换
NSMutableString * mStr5 = [[NSMutableString alloc] initWithString:@"abcdefg"];
[mStr5 replaceCharactersInRange:NSMakeRange(0, 2) withString:@"1234"]
//结果:mStr5 = @"1234cdefg"
 

#创建字典#
NSDictionary *dict = [[NSDictionary alloc] initWithObjectsAndKeys:@"one", @"1", @"two", @"2", @"three", @"3",  nil];
#键值对的个数#
NSInteger count = [dict count];
#通过键查找值#
NSString *str = [dict objectForKey:@"two”];
NSString *str = dict[@"two”];
#遍历#
for (NSString *str in dict) {
        NSLog(@"%@", str);      //遍历的是字典dict中的键
        NSLog(@"%@", dict[str]);//遍历的是字典dict中的值
}

#创建可变字典#
NSMutableDictionary * mdict = [[NSMutableDictionary alloc] initWithDictionary:dict];
#增加键值对#
[mdict setObject:@"four" forKey:@"4"];
#删除键值对#
[mdict5 removeObjectForKey:@“4”];//删除单个键
[mdict5 removeAllObjects];//全部删除
#字典替换#
NSDictionary * dict = @{@“3”:@“xixi", @"2":@"gogo"};
[mdict setDictionary:dict];//把字典mdict全部改为字典dict

字符串的拆分(componentsSeparatedByCharactersInSet)

有字符串“A~B^C" ,我拆分为 "A", "B" 和 "C"?

NSString *str = @"A~B^C";

NSArray *arr = [str componentsSeparatedByCharactersInSet: [NSCharacterSet characterSetWithCharactersInString:@"^~"]];
NSLog(@"%@", arr);

猜你喜欢

转载自blog.csdn.net/a18339063397/article/details/82686816