14

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?

Istorn
  • 485
  • 1
  • 5
  • 23

3 Answers3

43
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)
Anbu.Karthik
  • 82,064
  • 23
  • 174
  • 143
4
NSArray *components=[@"Description#Data#IMG" componentsSeparatedByString:@"#"];
iphonic
  • 12,615
  • 7
  • 60
  • 107
3

I would do something like this:

NSString *string = @"Description#Data#IMG";

NSArray *items = [string componentsSeparatedByString:@"#"];
Painted Black
  • 426
  • 2
  • 8