Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Split string by particular character ,iOS [duplicate]

I have a string structured in this way:

"Description#Data#IMG"

What's the best method to obtain three distinct strings through the position of sharps?

like image 459
Istorn Avatar asked Jun 02 '14 09:06

Istorn


People also ask

How do I separate a string from a character in Swift?

To split a String by Character separator in Swift, use String. split() function. Call split() function on the String and pass the separator (character) as argument. split() function returns a String Array with the splits as elements.

How do I split a string in Swift 4?

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.

How do I split a text string at a certain character?

To split a text string at a certain character, you can use a combination of the LEFT, RIGHT, LEN, and FIND functions. In the example shown, the formula in C5 is: = LEFT(B5,FIND("_", B5) - 1) And the formula in D5 is:

What is split () function in Python?

For splitting string python provides a function called split (). What Is split () function ? split () method breaks a given string by a specified separator and return a list of strings.

How to split a string into smaller strings in Python?

When splitting string, it takes one string and break it into two or more lists of substrings. Some time we may need to break a large string into smaller strings. For splitting string python provides a function called split(). split() method breaks a given string by a specified separator and return a list of strings.

How to split string at 4th position in Python?

For example string is “PythonIsProgrammingLanguage” , and you want to split this string at 4th position. So the code for this will be as follows – This code will split string at 4th position. You can see the string is broken up from 4th position and splitted into list of substrings. Now we will split string by a particular character.


1 Answers

NSString *str=@"Description#Data#IMG";  //is your str

NSArray *items = [str componentsSeparatedByString:@"#"];   //take the one array for split the string

NSString *str1=[items objectAtIndex:0];   //shows Description
NSString *str2=[items objectAtIndex:1];   //Shows Data
NSString *str3=[items objectAtIndex:2];   // shows IMG

Finally NSLog(@"your  3 stirs ==%@   %@  %@", str1, str2, str3);

Swift

 //is your str  
 var str: String = "Description#Data#IMG"

let items = String.components(separatedBy: "#") //take the one array for split the string
var str1: String = items.objectAtIndex(0)    //shows Description
var str2: String = items.objectAtIndex(1)    //Shows Data
var str3: String = items.objectAtIndex(2) // shows IMG

option-2

let items = str.characters.split("#")
var str1: String  =  String(items.first!)
var str1: String  =  String(items.last!)

option 3

// An example string separated by commas.
let line = "apple,peach,kiwi"

// Use components() to split the string.
// ... Split on comma chars.
let parts = line.components(separatedBy: ",")

// Result has 3 strings.
print(parts.count)
print(parts)
like image 179
Anbu.Karthik Avatar answered Oct 12 '22 03:10

Anbu.Karthik