按特定字符拆分字符串,iOS

按特定字符拆分字符串,iOS,ios,objective-c,iphone,swift,Ios,Objective C,Iphone,Swift,我有这样一个字符串: “描述#数据#IMG” 通过锐器的位置获得三个不同字符串的最佳方法是什么 NSArray *components=[@"Description#Data#IMG" componentsSeparatedByString:@"#"]; Swift //is your str var str: String = "Description#Data#IMG" let items = String.components(separatedBy: "#") //take

我有这样一个字符串:

“描述#数据#IMG”

通过锐器的位置获得三个不同字符串的最佳方法是什么

NSArray *components=[@"Description#Data#IMG" componentsSeparatedByString:@"#"];
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
选项2

let items = str.characters.split("#")
var str1: String  =  String(items.first!)
var str1: String  =  String(items.last!)
选项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)

我会这样做:

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

NSArray *items = [string componentsSeparatedByString:@"#"];
NSString *string = @"Description#Data#IMG";

NSArray *items = [string componentsSeparatedByString:@"#"];