1. Intercepting a string
NSString *str = @"huaWEInihaoma小明1994abcde";
str = [str substringToIndex:4];
NSLog(@"%@",str); //huaW
Copy the code
Check whether the string contains a field
// NSString * STR = @"huaWEInihaoma 1994abcde"; If ([STR rangeOfString: @rangeofString]. = NSNotFound ) { NSLog(@"yes"); }else{ NSLog(@"no"); } //yes // if ([STR containsString:@"小 小 "]) {// Do what you want}Copy the code
3. Change the string to upper/lower case
NSString * STR = @"huaWEInihaoma 1994abcde"; str = [str lowercaseString]; // lowercase //huaweinihaoma 1994abcde STR = [STR uppercaseString]; // uppercase //HUAWEINIHAOMA 1994ABCDE NSLog(@"%@", STR);Copy the code
Delete (replace) the specified string
NSString *str = @" a:b:c:d:e:f";
str = [str stringByReplacingOccurrencesOfString:@":" withString:@""];
NSLog(@"%@",str); //abcdef
Copy the code
Five, reserve two decimal points
NSString *string = [NSString stringWithFormat:@"%.2f",M_PI]; // The output is: 3.14 NSLog(@"%@", string);Copy the code
6. Use the method of 0 completion
NSInteger count = 9; NSString *string = [NSString stringWithFormat:@"%02zd",count]; NSString *string = [NSString stringWithFormat:@"%02zd",count]; // The output is: 09 NSLog(@"%@", string);Copy the code
7. What if there is a special symbol in the string
NSInteger count = 50; NSString *string = [NSString stringWithFormat:@"%zd "",count]; NSString *string = [NSString stringWithFormat:@"%zd "",count]; // The output is: 50" NSLog(@"%@", string);Copy the code
How to print % in a string
NSInteger count = 50; //% is a special symbol. If you use % in NSString, you should write NSString *string = [NSString String WithFormat:@"%zd%%",count]; // the output is: 50% NSLog(@"%@", string);Copy the code
Split string
/ / division "?" Before and after strings NSString *string =@"https://www.baidu.com?ssid=123123&pwd=111111"; NSArray *array = [string componentsSeparatedByString:@"?"] ; NSLog(@"array:%@",array); / / output is: / / "https://www.baidu.com" / / "ssid = 123123 & PWD = 111111"Copy the code
Remove the space at the beginning and end of the string
NSString *strBefore = @" abcd123 "; NSString *strAfter = [strBefore stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]]; NSLog(@"%@",strAfter); //// Output :"abcd123"Copy the code
UIAlertController text content line feed display
// add: \n to NSString *name = @"小 小 "; NSString *address = @" global village "; NSString * labelText = [NSString stringWithFormat:@" Name :%@\n Address :%@",name,address]; NSLog(@"%@",labeltext); /* Name: Xiao Ming Address: Global village */Copy the code
12. Determine whether it starts or ends with “AAA”
NSString *string = @"AAAbcdfgHHH"; [string hasPrefix:@"AAA"]; [string hasSuffix:@"HHH"]; // Check if the end is "HHH"Copy the code