Objective-C中使用NSString類操作字符串的方法小結(jié)
一、字符串切割
1、帶節(jié)點(diǎn)的字符串,如@"<p>討厭的節(jié)點(diǎn)<br/></p>"我們只想要中間的中文
處理方法:
NSString *string1 = @"<p>討厭的節(jié)點(diǎn)<br/></p>";
/*此處將不想要的字符全部放進(jìn)characterSet1中,不需另外加逗號或空格之類的,除非字符串中有你想要去除的空格,此處< p /等都是單獨(dú)存在,不作為整個字符*/
NSCharacterSet *characterSet1 = [NSCharacterSet characterSetWithCharactersInString:@"<p/brh>"];
// 將string1按characterSet1中的元素分割成數(shù)組
NSArray *array1 = [string1 componentsSeparatedByCharactersInSet:characterSet1];
NSLog(@"array = %@",array1);
for(NSString *string1 in array1)
{
if ([string1 length]>0) {
// 此處string即為中文字符串
NSLog(@"string = %@",string1);
}
}
打印結(jié)果:
2016-01-17 10:55:34.017 string[17634:303] array = ( "", "", "", "\U8ba8\U538c\U7684\U8282\U70b9", "", "", "", "", "", "", "", "", "" ) 2016-01-17 10:55:34.049 string[17634:303] string = 討厭的節(jié)點(diǎn)
2、帶空格的字符串,如
@"hello world"去掉空格
NSString *string2 = @"hello world";
/*處理空格*/
NSCharacterSet *characterSet2 = [NSCharacterSet whitespaceCharacterSet];
// 將string1按characterSet1中的元素分割成數(shù)組
NSArray *array2 = [string2 componentsSeparatedByCharactersInSet:characterSet2];
NSLog(@"\narray = %@",array2);
// 用來存放處理后的字符串
NSMutableString *newString1 = [NSMutableString string];
for(NSString *string in array1)
{
[newString1 appendString:string];
}
NSLog(@"newString = %@", newString1);
打印結(jié)果:
2016-01-17 11:02:49.656 string[17889:303] array = ( hello, world ) 2016-01-17 11:02:49.657 string[17889:303] newString = helloworld
PS:處理字母等其他元素只需將NSCharacterSet的值改變即可。
+ (id)controlCharacterSet;
+ (id)whitespaceCharacterSet;
+ (id)whitespaceAndNewlineCharacterSet;
+ (id)decimalDigitCharacterSet;
+ (id)letterCharacterSet;
+ (id)lowercaseLetterCharacterSet;
+ (id)uppercaseLetterCharacterSet;
+ (id)nonBaseCharacterSet;
+ (id)alphanumericCharacterSet;
+ (id)decomposableCharacterSet;
+ (id)illegalCharacterSet;
+ (id)punctuationCharacterSet;
+ (id)capitalizedLetterCharacterSet;
+ (id)symbolCharacterSet;
+ (id)newlineCharacterSet NS_AVAILABLE(10_5, 2_0);
+ (id)characterSetWithRange:(NSRange)aRange;
+ (id)characterSetWithCharactersInString:(NSString *)aString;
+ (id)characterSetWithBitmapRepresentation:(NSData *)data;
+ (id)characterSetWithContentsOfFile:(NSString *)fName;
二、用字符將NSArray中的元素拼接起來
NSArray *array = [NSArray arrayWithObjects:@"hello",@"world",nil];
//如要用,:等字符串拼接,只需將下面的@" "空格換成@","或@":"即可
NSString *string = [array componentsJoinedByString:@" "];
NSLog(@"string = %@",string);
打印結(jié)果:
hello world
三、截取子串:
這里以獲取時間為例,利用NSDate獲取到當(dāng)前時間時,有時候只需要日期或者只需要時間
1、從字符串開頭截取到指定的位置,如
//獲取到當(dāng)前日期時間
NSDate *date = [NSDate date];
//定義日期格式,此處不重點(diǎn)討論NSDate,故不詳細(xì)說明,在后面會詳細(xì)討論
NSDateFormatter *dateformatter = [[NSDateFormatter alloc] init];
//設(shè)置日期格式
[dateformatter setDateFormat:@"YYYY-MM-dd HH:mm"];
//將日期轉(zhuǎn)換成NSString類型
NSString *string = [dateformatter stringFromDate:date];
NSLog(@"\ncurrent = %@",string);
//截取日期substringToIndex
NSString *currentDate = [string substringToIndex:10];
NSLog(@"\ncurrentDate = %@",currentDate);
打印結(jié)果:
current = 2016-01-1711:12 currentDate = 2016-01-17
2、抽取中間子串-substringWithRange
//截取月日
NSString *currentMonthAndDate = [string substringWithRange:[NSMakeRange(5, 5)]];
NSLog(@"currentMonthAndDate = %@",currentMonthAndDate);
打印結(jié)果:
currentMonthAndDate = 06-27
3、從某一位置開始截取- substringFromIndex
//截取時間substringFromIndex
NSString *currentTime = [string substringFromIndex:11];
NSLog(@"\ncurrentTime = %@",currentTime);\
打印結(jié)果:
currentTime = 11:25
四、比較字符串
NSString *first = @"string";
NSString *second = @"String";
1、判斷兩個字符串是否相同-isEqualToString方法
BOOL isEqual = [first isEqualToString:second];
NSLog(@"first is Equal to second:%@",isEqual);
打印結(jié)果:
first is Equal to second:0
2、compare方法比較字符串三個值
NSOrderedSame//是否相同
NSOrderedAscending//升序,按字母順序比較,大于為真
NSOrderedDescending//降序,按字母順序比較,小于為真
BOOL result = [first compare:sencond] == NSOrderedSame;
NSLog(@"result:%d",result);
打印結(jié)果:
result:0
BOOL result = [first compare:second] == NSOrderedAscending;
NSLog(@"result:%d",result);
打印結(jié)果:
result:0
BOOL result = [first compare:second] == NSOrderedDecending; NSLog(@"result:%d",result);
打印結(jié)果:
result:1
3、不考慮大小寫比較字符串
BOOL result = [first compare:second
options:NSCaseInsensitiveSearch | NSNumericSearch] == NSOrderedSame;
NSLog(@"result:%d",result);
打印結(jié)果:
result:1
五、改變字符串大小寫
NSString *aString = @"A String";
NSString *string = @"String";
//大寫
NSLog(@"aString:%@",[aString uppercaseString]);
//小寫
NSLog(@"string:%@",[string lowercaseString]);
//首字母大小寫
NSLog(@"string:%@",[string capitalizedString]);
打印結(jié)果:
aString:A STRING string:string string:String
六、在字符串中搜索子串
NSString *string1 = @"This is a string";
NSString *string2 = @"string";
NSRange range = [string1 rangeOfString:string2];
NSUInteger location = range.location;
NSUInteger leight = range.length;
NSString *astring = [[NSString alloc] initWithString:[NSString stringWithFormat:@"Location:%li,Leight:%li",location,leight]];
NSLog(@"astring:%@",astring);
[astring release];
打印結(jié)果:
astring:Location:10,Leight:6
相關(guān)文章
淺談iOS中幾個常用協(xié)議 NSCopying/NSMutableCopying
下面小編就為大家分享一篇淺談iOS中幾個常用協(xié)議 NSCopying/NSMutableCopying,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2017-12-12iOS實(shí)現(xiàn)導(dǎo)航欄透明示例代碼
本篇文章主要介紹了iOS實(shí)現(xiàn)導(dǎo)航欄透明示例代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-03-03IOS-MVC層讀取服務(wù)器接口JSON數(shù)據(jù)
這篇文章主要為大家詳細(xì)介紹了IOS-MVC層讀取服務(wù)器接口JSON數(shù)據(jù),文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-12-12