Summary of the methods in Objective C that manipulate strings using the NSString class
- 2020-05-14 04:55:42
- OfStack
1. String cutting
1. String with nodes, such as "@"
<
p
>
Nasty node
<
br/
>
<
/p
>
"We only want the Chinese in the middle
Treatment:
NSString *string1 = @"<p> Nasty node <br/></p>";
/* Put all the unwanted characters in here characterSet1 There is no need to add additional commas or Spaces unless there is a space you want to remove from the string, as shown here < p / And so on exist separately, not as a whole character */
NSCharacterSet *characterSet1 = [NSCharacterSet characterSetWithCharactersInString:@"<p/brh>"];
// will string1 According to the characterSet1 Is divided into an array
NSArray *array1 = [string1 componentsSeparatedByCharactersInSet:characterSet1];
NSLog(@"array = %@",array1);
for(NSString *string1 in array1)
{
if ([string1 length]>0) {
// here string Is a Chinese string
NSLog(@"string = %@",string1);
}
}
Print result:
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 = Nasty node
2. A string with Spaces, such as
@hello world remove Spaces
NSString *string2 = @"hello world";
/* Processing the blank space */
NSCharacterSet *characterSet2 = [NSCharacterSet whitespaceCharacterSet];
// will string1 According to the characterSet1 Is divided into an array
NSArray *array2 = [string2 componentsSeparatedByCharactersInSet:characterSet2];
NSLog(@"\narray = %@",array2);
// To hold the processed string
NSMutableString *newString1 = [NSMutableString string];
for(NSString *string in array1)
{
[newString1 appendString:string];
}
NSLog(@"newString = %@", newString1);
Print result:
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: to handle other elements such as letters, simply change the value of 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. Concatenate the elements in NSArray with the characters
NSArray *array = [NSArray arrayWithObjects:@"hello",@"world",nil];
// If you want to use ,: String concatenation, just put the following @" " A space for @"," or @":" Can be
NSString *string = [array componentsJoinedByString:@" "];
NSLog(@"string = %@",string);
Print result:
hello world
3. Intercept substrings:
Here is an example of getting the time. When you get the current time with NSDate, sometimes you only need a date or only a time
1. Intercepts from the beginning of a string to the specified location, such as
// Gets the current date and time
NSDate *date = [NSDate date];
// Define date format , I won't focus here NSDate , which will be discussed in more detail later
NSDateFormatter *dateformatter = [[NSDateFormatter alloc] init];
// Set the date format
[dateformatter setDateFormat:@"YYYY-MM-dd HH:mm"];
// Convert the date to NSString type
NSString *string = [dateformatter stringFromDate:date];
NSLog(@"\ncurrent = %@",string);
// Capture the date substringToIndex
NSString *currentDate = [string substringToIndex:10];
NSLog(@"\ncurrentDate = %@",currentDate);
Print result:
current = 2016-01-1711:12
currentDate = 2016-01-17
2. Extract the middle substring -substringWithRange
// Intercept,
NSString *currentMonthAndDate = [string substringWithRange:[NSMakeRange(5, 5)]];
NSLog(@"currentMonthAndDate = %@",currentMonthAndDate);
Print result:
currentMonthAndDate = 06-27
3. Intercept -substringFromIndex from a certain position
// The interception time substringFromIndex
NSString *currentTime = [string substringFromIndex:11];
NSLog(@"\ncurrentTime = %@",currentTime);\
Print result:
currentTime = 11:25
4. Compare strings
NSString *first = @"string";
NSString *second = @"String";
1. Determine whether the two strings are the same -isEqualToString method
BOOL isEqual = [first isEqualToString:second];
NSLog(@"first is Equal to second:%@",isEqual);
Print result:
first is Equal to second:0
2. The compare method compares three values of the string
NSOrderedSame// Whether or not the same
NSOrderedAscending// Ascending, in alphabetical order, greater than is true
NSOrderedDescending// In descending order, in alphabetical order, less than is true
BOOL result = [first compare:sencond] == NSOrderedSame;
NSLog(@"result:%d",result);
Print result:
result:0
BOOL result = [first compare:second] == NSOrderedAscending;
NSLog(@"result:%d",result);
Print result:
result:0
BOOL result = [first compare:second] == NSOrderedDecending; NSLog(@"result:%d",result);
Print result:
result:1
Compare strings regardless of case
BOOL result = [first compare:second
options:NSCaseInsensitiveSearch | NSNumericSearch] == NSOrderedSame;
NSLog(@"result:%d",result);
Print result:
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
0
5. Change string case
NSString *aString = @"A String";
NSString *string = @"String";
// A capital
NSLog(@"aString:%@",[aString uppercaseString]);
// lowercase
NSLog(@"string:%@",[string lowercaseString]);
// Initial case
NSLog(@"string:%@",[string capitalizedString]);
Print result:
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
1
6. Search for substrings in a 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];
Print result:
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
2