1、字符串切割
1、帶節點的字符串,如@"<p>憎惡的節點<br/></p>"我們只想要中央的中文
處置辦法:
NSString *string1 = @"<p>憎惡的節點<br/></p>";
/*此處將不想要的字符全體放進characterSet1中,不需別的加逗號或空格之類的,除非字符串中有你想要去除的空格,此處< p /等都是零丁存在,不作為全部字符*/
NSCharacterSet *characterSet1 = [NSCharacterSet characterSetWithCharactersInString:@"<p/brh>"];
// 將string1按characterSet1中的元素朋分成數組
NSArray *array1 = [string1 componentsSeparatedByCharactersInSet:characterSet1];
NSLog(@"array = %@",array1);
for(NSString *string1 in array1)
{
if ([string1 length]>0) {
// 此處string即為中文字符串
NSLog(@"string = %@",string1);
}
}
打印成果:
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 = 憎惡的節點
2、帶空格的字符串,如
@"hello world"去失落空格
NSString *string2 = @"hello world";
/*處置空格*/
NSCharacterSet *characterSet2 = [NSCharacterSet whitespaceCharacterSet];
// 將string1按characterSet1中的元素朋分成數組
NSArray *array2 = [string2 componentsSeparatedByCharactersInSet:characterSet2];
NSLog(@"\narray = %@",array2);
// 用來寄存處置後的字符串
NSMutableString *newString1 = [NSMutableString string];
for(NSString *string in array1)
{
[newString1 appendString:string];
}
NSLog(@"newString = %@", newString1);
打印成果:
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;
2、用字符將NSArray中的元素拼接起來
NSArray *array = [NSArray arrayWithObjects:@"hello",@"world",nil];
//如要用,:等字符串拼接,只需將上面的@" "空格換成@","或@":"便可
NSString *string = [array componentsJoinedByString:@" "];
NSLog(@"string = %@",string);
打印成果:
hello world
3、截取子串:
這裡以獲得時光為例,應用NSDate獲得到以後時光時,有時刻只須要日期或許只須要時光
1、從字符串開首截取到指定的地位,如
//獲得到以後日期時光
NSDate *date = [NSDate date];
//界說日期格局,此處不重點評論辯論NSDate,故不具體解釋,在前面會具體評論辯論
NSDateFormatter *dateformatter = [[NSDateFormatter alloc] init];
//設置日期格局
[dateformatter setDateFormat:@"YYYY-MM-dd HH:mm"];
//將日期轉換成NSString類型
NSString *string = [dateformatter stringFromDate:date];
NSLog(@"\ncurrent = %@",string);
//截取日期substringToIndex
NSString *currentDate = [string substringToIndex:10];
NSLog(@"\ncurrentDate = %@",currentDate);
打印成果:
current = 2016-01-1711:12 currentDate = 2016-01-17
2、抽取中央子串-substringWithRange
//截取月日
NSString *currentMonthAndDate = [string substringWithRange:[NSMakeRange(5, 5)]];
NSLog(@"currentMonthAndDate = %@",currentMonthAndDate);
打印成果:
currentMonthAndDate = 06-27
3、從某一名置開端截取- substringFromIndex
//截取時光substringFromIndex
NSString *currentTime = [string substringFromIndex:11];
NSLog(@"\ncurrentTime = %@",currentTime);\
打印成果:
currentTime = 11:25
4、比擬字符串
NSString *first = @"string";
NSString *second = @"String";
1、斷定兩個字符串能否雷同-isEqualToString辦法
BOOL isEqual = [first isEqualToString:second];
NSLog(@"first is Equal to second:%@",isEqual);
打印成果:
first is Equal to second:0
2、compare辦法比擬字符串三個值
NSOrderedSame//能否雷同
NSOrderedAscending//升序,按字母次序比擬,年夜於為真
NSOrderedDescending//降序,按字母次序比擬,小於為真
BOOL result = [first compare:sencond] == NSOrderedSame;
NSLog(@"result:%d",result);
打印成果:
result:0
BOOL result = [first compare:second] == NSOrderedAscending;
NSLog(@"result:%d",result);
打印成果:
result:0
BOOL result = [first compare:second] == NSOrderedDecending; NSLog(@"result:%d",result);
打印成果:
result:1
3、不斟酌年夜小寫比擬字符串
BOOL result = [first compare:second
options:NSCaseInsensitiveSearch | NSNumericSearch] == NSOrderedSame;
NSLog(@"result:%d",result);
打印成果:
result:1
5、轉變字符串年夜小寫
NSString *aString = @"A String";
NSString *string = @"String";
//年夜寫
NSLog(@"aString:%@",[aString uppercaseString]);
//小寫
NSLog(@"string:%@",[string lowercaseString]);
//首字母年夜小寫
NSLog(@"string:%@",[string capitalizedString]);
打印成果:
aString:A STRING string:string string:String
6、在字符串中搜刮子串
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];
打印成果:
astring:Location:10,Leight:6
【Objective-C中應用NSString類操作字符串的辦法小結】的相關資料介紹到這裡,希望對您有所幫助! 提示:不會對讀者因本文所帶來的任何損失負責。如果您支持就請把本站添加至收藏夾哦!