//1. Get the number of characters in the string print(str.count); //6 print(nsStr.length); //6 //2. Prefix/Suffix Operations var str ="123456"; str.hasPrefix("12") //true str.hasSuffix("56") //true
var nsStr =NSString(stringLiteral: "123456"); nsStr.hasPrefix("12") //true nsStr.hasSuffix("56") //true //3. Prefix/Suffix Operations var str ="123456-11"; str.components(separatedBy: "-"); //["123456", "11"] var nsStr =NSString(stringLiteral: "123456-22"); nsStr.components(separatedBy: "-"); //["123456", "22"] //4. Letter case conversion ops for uppercased/lowercased/capitalized var str ="abc"; print(str.uppercased()); //ABC print(str.lowercased()); //abc
var nsStr =NSString(stringLiteral: "cba"); print(nsStr.uppercased); //CBA print(nsStr.lowercased); //cba
var str ="abc"; print(str.capitalized); //Abc
var nsStr =NSString(stringLiteral: "cba"); print(nsStr.capitalized); //Cba //5. Strip specific strings ops for trimmingCharacters let str ="\r abc "; let strTrimed = str.trimmingCharacters(in: .whitespacesAndNewlines) print(strTrimed); //abc
let nsStr =NSString(stringLiteral: "\r cba "); let nsStrTrimed = nsStr.trimmingCharacters(in: .whitespacesAndNewlines); print(nsStrTrimed); //cba //6. Character substitution ops for replacingOccurrences:of:with let str ="hello String"; let str1 = str.replacingOccurrences(of: "String", with: "world") print(str1) //"hello world"
let nsStr =NSString(stringLiteral:"hello NSString"); let nsStr1 = nsStr.replacingOccurrences(of: "NSString", with: "world") print(nsStr1) //"hello world"
//7. Convert decimal hexadecimal octal data to string let hexStr =String().appendingFormat("%x",16)//10->16,result:"10" let oStr =String().appendingFormat("%o",16)//10->8,result:"20" let dStr =String().appendingFormat("%d",0x10)//16->10,result:"16" let dStr1 =String(format: "%d", 0o10)//8->10,result: "8"
let hexNSStr =NSString().appendingFormat("%x", 16)//10->16,result:"10" let oNSStr =NSString().appendingFormat("%o",16)//10->8,result:"20" let dNSStr =NSString().appendingFormat("%d",0x10)//16->10,result:"16" let dNSStr1 =NSString(format: "%d", 0o10)//8->10,result: "8"
1.2 Independent APIs 1.2.1 Independent APIs of String