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?
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.
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.
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:
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.
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.
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.
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)
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