objective c - Split string by particular character ,iOS -


this question has answer here:

i have string structured in way:

"description#data#img"

what's best method obtain 3 distinct strings through position of sharps?

nsstring *str=@"description#data#img";  //is str  nsarray *items = [str componentsseparatedbystring:@"#"];   //take 1 array split string  nsstring *str1=[items objectatindex:0];   //shows description nsstring *str2=[items objectatindex:1];   //shows data nsstring *str3=[items objectatindex:2];   // shows img  nslog(@"your  3 stirs ==%@   %@  %@", str1, str2, str3); 

swift

 //is str    var str: string = "description#data#img"  let items = string.components(separatedby: "#") //take 1 array split 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

// example string separated commas. let line = "apple,peach,kiwi"  // use components() split string. // ... split on comma chars. let parts = line.components(separatedby: ",")  // result has 3 strings. print(parts.count) print(parts) 

Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

ios - Change Storyboard View using Seague -