Summary of common operation methods of the string NSString in ES0en ES1en
- 2020-06-07 05:19:20
- OfStack
1. Create a string
(1) Constant string
NSString *string = @"i am an iOSDevTip!";
(2) Common creation methods
NSString *string = [[NSString alloc] init];
string = @"i am an iOSDevTip too!";
(3) Use initWithString to create strings
NSString *string = [[NSString alloc] initWithString:@"iOSDevTip is here!"];
2. Format the create string
(1) int format string
int age = 20;
NSString *personAge = [NSString stringWithFormat:@"this person age is %d",age];
Since int formats strings, float, double, and so on can also format strings.
(2) NSString format string
NSString *name = @"iOSDevTip";
NSString *personName = [NSString stringWithFormat:@"this person name is %@",name];
3. String comparison
(1) Comparison of isEqualToString methods
NSString *stingOne = @"This is an iOSDevTip!";
NSString *stringTwo = @"This is an iOSDevTip!";
BOOL result = [stingOne isEqualToString:stringTwo];
(2) Comparison of compare methods
BOOL result = [stingOne compare:stringTwo] == NSOrderedSame;
compare: The method returns a value of type NSComparisonResult. NSComparisonResult has the following enumerated values.
typedef NS_ENUM(NSInteger, NSComparisonResult) {NSOrderedAscending = -1L, NSOrderedSame, NSOrderedDescending};
4. String capitalization conversion
(1) Lowercase to uppercase
NSString *string = @"This is an iOSDevTip!";
[string lowercaseString];
(2) Uppercase to lowercase
NSString *string = @"This is an iOSDevTip!";
[string uppercaseString];
(3) Capital letters
NSString *string = @"ligang";
NSLog(@"string: %@",[string capitalizedString]);
Print:
2015-07-16 23:06:11.652 iOSStrongDemo[10279:3062010] string: Ligang
5. Intercept the string
(1) substringToIndex intercepts the string
NSString *string = @"This is a operation string!";
NSString *subToString = [string substringToIndex:6];
(2) The intercepted subToString is This i
substringFromIndex intercepts the string
NSString *subFromString = [string substringFromIndex:6];
(3) The intercepted subFromString is s a operation string!
substringWithRange intercepts the string
NSString *rangeString = [string substringWithRange:NSMakeRange(6, 3)];
s a!
6. Determine if the string contains another string
(1) rangeOfString judgment
NSString *string1 = @"This is a iOSDevTip";
NSString *string2 = @"iOSDevTip";
NSRange range = [string1 rangeOfString:string2];
NSInteger location = range.location;
NSInteger leight = range.length;
NSString *logString = [[NSString alloc] initWithString:[NSString stringWithFormat:@"Location:%ld,Leight:%ld",location,leight]];
NSLog(@"logString:%@",logString);
Print it out:
iOSStrongDemo[8837:2221170] logString:Location:10,Leight:9
(2) Judge whether the presuffix is included
NSString *string = @"I love China";
BOOL isHasI = [string hasPrefix:@"I"];
BOOL isHasChina = [string hasSuffix:@"China"];
7. Split strings
NSString *string = @"This is a iOSDevTip";
NSArray *array = [string componentsSeparatedByString:@"a"];
NSString *string1 = [array objectAtIndex:0];
NSString *string2 = [array objectAtIndex:1];
NSLog(@"string1:%@ string2:%@",string1,string2);
Print:
2015-07-16 22:40:39.559 iOSStrongDemo[10165:3055448] string1:This is string2: iOSDevTip
8. Insert string
NSMutableString *string = [[NSMutableString alloc] initWithString:@"I China"];
[string insertString:@"Love " atIndex:2];
NSLog(@"string: %@",string);
Print:
2015-07-16 22:44:10.706 iOSStrongDemo[10206:3057014] string: I Love China
(1) Append a string
NSString *string = [[NSString alloc] init];
string = @"i am an iOSDevTip too!";
8
Print:
2015-07-16 22:42:32.305 iOSStrongDemo[10189:3056410] string:I Love China
9. Delete strings
NSString *string = [[NSString alloc] init];
string = @"i am an iOSDevTip too!";
9
Print:
2015-07-16 22:46:58.437 iOSStrongDemo[10219:3057749] String1: I China
Replace the string
NSString *string = [[NSString alloc] initWithString:@"iOSDevTip is here!"];
0
Print:
2015-07-16 22:56:07.405 iOSStrongDemo[10236:3059503] replaceString: I like China
11. Removes Spaces and newlines at the beginning and end of the string
NSString *string = @" I love China ";
NSString *text = [string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
NSLog(@"text:%@",text);
Print:
2015-07-16 23:00:47.845 iOSStrongDemo[10265:3061013] text:I love China