03. NSString and String

It will take about 3 minutes to finish reading this article.

1. Mutual Transformation

NSString and String can transform to each other.
Example Code

1
2
3
4
5
6
7
8
9
10
11
12
func StringToNSString()  {
let a = String("1234567890")
let b = a as NSString
print(b.integerValue)
print(b.length)
}

func NSStringToString() {
let a : NSString = "1234567890"
let b = a as String
print(b)
}

2. Differences between NSString and String

NSString is a subclass of NSObject and that is to say, it is a class that is a reference type.

1
2
3
4
5
open class NSString : NSObject, NSCopying, NSMutableCopying, NSSecureCoding {

open var length: Int { get }
...
}

However, String is a value type which is a kind of struct.

1
2
3
4
@frozen public struct String {  
@inlinable public init()
...
}

They have some identical APIs, but they also have their own independent APIs.

1
2
3
4
var str = String()
str = "123456"
var nsStr = NSString();
nsStr = "123456";

1.1 Similar or Identical APIs

Sample Code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
//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

Sample Code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
//1. isEmpty
var str = String();
str = "123456";
print(str.isEmpty);
//2. sorted()
var str = "215423";
print(str.sorted()); //["1", "2", "2", "3", "4", "5"]

//3. filter()
let str = "12 34 56";
let filter = str.filter { (char) -> Bool in
char != " "
};
print(filter);//123456

//4. enumerated()
let str = "123456";
for data in str.enumerated() {
print(data);
}

1.2.2 Independent APIs of NSString

Sample Code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// 1. initialize methods
var nsStr = NSString(stringLiteral: "123456");
var nsStr = NSString.init(string: "123456");

// 2. boolValue
var nsStr = NSString(stringLiteral: "123456");
print(nsStr.boolValue);//true

// 3. isEqual()
var nsStr = NSString(stringLiteral: "123456");
print(nsStr.isEqual(to: "123456"));//true

// 4. Numeric Ops
var nsStr = NSString(stringLiteral: "123456")
print(nsStr.intValue);
print(nsStr.floatValue);
print(nsStr.doubleValue);