Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Any sample code for componentsSeparatedByCharactersInSet? (on iPhone)

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"?

like image 438
Chilly Zhong Avatar asked Mar 01 '09 13:03

Chilly Zhong


People also ask

What is NSString?

A static, plain-text Unicode string object which you use when you need reference semantics or other Foundation-specific behavior.

How do you split a string in Objective C?

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.

How do you split in Swift?

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.

What is component in Swift?

The components(separatedBy:) method is used to divide a string into substrings using the specified string separator.


1 Answers

Try this

NSString *str = @"A~B^C";

NSArray *arr = [str componentsSeparatedByCharactersInSet:
          [NSCharacterSet characterSetWithCharactersInString:@"^~"]];

NSLog(@"%@", arr);
like image 180
epatel Avatar answered Sep 21 '22 11:09

epatel