NSString *string = @"Progamming is funny";
NSLog(@"%@",string);
NSString *str1 = @"this is string A";
NSString *str2 = @"this is string B";
NSString *res;
NSComparisonResult comparionResult;
//count the number of characters
NSLog(@"Length of str1: %lu", [str1 length]);
//copy one string to another
res = [NSString stringWithString:str1];
NSLog(@"copy: %@",res);
//copy one string to the end of another
str2 = [str1 stringByAppendingString:str2];
NSLog(@"Conactenation: %@",str2);
//Test if 2 strings are equal
if([str1 isEqualToString:str2] == YES)
NSLog(@"str1 == str2");
else
NSLog(@"str1 != str2");
//Test if one string is <, ==, or > than another
comparionResult = [str1 compare: str2];
if(comparionResult == NSOrderedAscending)
NSLog(@"str1 < str2");
else if(comparionResult == NSOrderedSame)
NSLog(@"str1 == str2");
else
NSLog(@"str1 > str2");
//conver a string to upperCase
res = [str1 uppercaseString];
NSLog(@"%@",res);
//conver a string to lowerCase
res = [str1 lowercaseString];
NSLog(@"%@", res);
NSRange subRange;
//Extact first 3 chars from string
res = [str1 substringToIndex:3];
NSLog(@"First 3 chars of str1: %@", res);
//Extact chars to end of string starting at index 5
res = [str1 substringFromIndex:5];
NSLog(@"chars from index 5 of str1: %@",res);
//Extact char from index 8 through 13 (6 chars)
res = [[str1 substringFromIndex:8] substringToIndex:6];
NSLog(@"chars from index 8 through 13: %@",res);
//An easier way to do the same thing
res = [str1 substringWithRange:NSMakeRange(8, 6)];
NSLog(@"chars from index 8 through 13: %@",res);
//locate one string inside another
subRange = [str1 rangeOfString:@"string A"];
NSLog(@"string is at index %lu, length is %lu",subRange.location,subRange.length);
subRange = [str1 rangeOfString:@"string B"];
if(subRange.location == NSNotFound)
NSLog(@"string not found");
else
NSLog(@"string is at index %lu, length is %lu",subRange.location,subRange.length);
///mutable
NSString *search,*replace;
NSMutableString *mstr;
NSRange subString;
//create mutable string from nonmutable
mstr = [NSMutableString stringWithString:str1];
NSLog(@"%@",mstr);
//Insert characters
[mstr insertString:@" mutable" atIndex:7];
NSLog(@"%@",mstr);
//Effective contacentation if insert at end
[mstr insertString:@" and string B" atIndex:[mstr length]];
NSLog(@"%@",mstr);
//or can user appendString directly
[mstr appendString:@" and string C"];
NSLog(@"%@",mstr);
//delete substring based on range
[mstr deleteCharactersInRange:NSMakeRange(16, 13)];
NSLog(@"%@",mstr);
//Find range first and the use it for delection
subString = [mstr rangeOfString:@" string B and"];
if(subString.location != NSNotFound){
[mstr deleteCharactersInRange:subString];
NSLog(@"%@",mstr);
}
//set the mutable string
[mstr setString:@"This is string A"];
NSLog(@"%@",mstr);
//replace a range of chars with another
[mstr replaceCharactersInRange:NSMakeRange(8, 8) withString:@"a mutable string"];
NSLog(@"%@",mstr);
//search and replace
search = @"This is ";
replace = @"An example of ";
subString = [mstr rangeOfString:search];
if(subString.location != NSNotFound){
[mstr replaceCharactersInRange:subString withString:replace];
NSLog(@"%@",mstr);
}
//search and replace all occurrences
search = @"a";
replace = @"X";
subString = [mstr rangeOfString:search];
while (subString.location != NSNotFound) {
[mstr replaceCharactersInRange:subString withString:replace];
subString = [mstr rangeOfString:search];
}
NSLog(@"%@",mstr);
iOS基础一 NSString
最新推荐文章于 2021-08-16 09:14:46 发布