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.
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.
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.
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.
You can use components(separatedBy:) method to divide a string into substrings by specifying string separator. let str = "Hello! Swift String."
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:@", "];
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