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

标签 ios objective-c iphone swift

<分区>

我有一个以这种方式构造的字符串:

“描述#Data#IMG”

通过升号的位置得到三个不同字符串的最佳方法是什么?

最佳答案

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

选项-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)

关于ios - 按特定字符拆分字符串,iOS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23991392/

相关文章:

ios - swift :为什么是 int!成员实例更改为 int?在 block ?

ios - 在 iOS 上可以实现纯 p2p 吗?

ios - 使用非常长的 NSAttributedString 是否安全(好与坏)?

objective-c - -forwardInvocation 适用于 Clang - LLVM 但不适用于 GCC

ios - 如何在 cocoa touch 框架中使用 MagicalRecord 库?

iphone - 多个 UIWebViews 问题

iphone - 将核心数据与现有的单一 View 应用程序一起使用

c++ - 链接静态 C++ 库时 Objective-C 中的损坏符号表

ios - 我可以使用 UIRequiredDeviceCapabilities 将应用程序限制为 iPad 和 iPhone 6+ 吗?

ios - 如何在Xcode中使用动态单元格实现多个segue?