I want to split a long string received from a server to a few substrings. The separate characters are different.
Are there any sample code for method: componentsSeparatedByCharactersInSet? Or may I ask a simple code to split "A~B^C" to "A", "B" and "C"?
A static, plain-text Unicode string object which you use when you need reference semantics or other Foundation-specific behavior.
You can also split a string by a substring, using NString's componentsSeparatedByString method. You should be able to use NSString's "componentsSeparatedByCharactersInSet:" to split on multiple characters.
To split a string to an array in Swift by a character, use the String. split(separator:) function. However, this requires that the separator is a singular character, not a string.
The components(separatedBy:) method is used to divide a string into substrings using the specified string separator.
Try this
NSString *str = @"A~B^C";
NSArray *arr = [str componentsSeparatedByCharactersInSet:
[NSCharacterSet characterSetWithCharactersInString:@"^~"]];
NSLog(@"%@", arr);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With