Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to split string into substrings on iOS?

I received an NSString from the server. Now I want to split it into the substring which I need. How to split the string?

For example:

substring1:read from the second character to 5th character

substring2:read 10 characters from the 6th character.

like image 508
Chilly Zhong Avatar asked Feb 27 '09 09:02

Chilly Zhong


People also ask

How do I split a string into substrings?

The split() method splits a string into an array of substrings. The split() method returns the new array. The split() method does not change the original string. If (" ") is used as separator, the string is split between words.

What divides a string into an array of substrings?

The split() method splits (divides) a string into two or more substrings depending on a splitter (or divider). The splitter can be a single character, another string, or a regular expression. After splitting the string into multiple substrings, the split() method puts them in an array and returns it.

How do you split a string in Objective C?

Objective-C Language NSString Splitting If you need to split on a set of several different delimiters, use -[NSString componentsSeparatedByCharactersInSet:] . If you need to break a string into its individual characters, loop over the length of the string and convert each character into a new string.

How do I separate words in a string Swift?

You can use components(separatedBy:) method to divide a string into substrings by specifying string separator. let str = "Hello! Swift String."


1 Answers

You can also split a string by a substring, using NString's componentsSeparatedByString method.

Example from documentation:

NSString *list = @"Norman, Stanley, Fletcher"; NSArray *listItems = [list componentsSeparatedByString:@", "]; 
like image 159
codelogic Avatar answered Sep 19 '22 18:09

codelogic