oneString
和 NSString
The fundamental difference between
String
Is a structure, and a structure is a value type, soString
It’s also a value type.- When a value type is assigned to a variable, constant, or passed to a function, its value is copied. This means that their instances, along with any value type attributes contained in those instances, are copied when passed through the code.
NSString
It’s a class, and a class is a reference type, soNSString
Also a reference type.- When a reference type is assigned to a variable, constant, or passed to a function, its value is not copied. Therefore, the existing instance itself is referred to rather than a copy of it.
twoString
与 NSString
The unique API
2.1 String
The unique API
2.1.1 initializationString
var str = String()
str = "123456"
Copy the code
2.1.2 String
的 isEmpty
attribute
- Determines whether the string is empty
var str = String();
str = "123456";
print(str.isEmpty);
// false
Copy the code
var str = String();
print(str.isEmpty);
// true
Copy the code
var str = "";
print(str.isEmpty);
// true
Copy the code
2.1.3 String
的 sorted()
Methods:
- String sort and output an array of characters
var str = "215423";
print(str.sorted()); / / /"1"."2"."2"."3"."4"."5"]
Copy the code
2.1.4 String
的 filter()
methods
- Filter unwanted characters
let str = 34 56 "12";
let filter = str.filter { (char) -> Bool inchar ! =""
};
print(filter); / / 123456Copy the code
2.1.5 String
的 enumerated()
Methods:
let str = "123456";
for data in str.enumerated() {
print(data);
}
/*
(offset: 0, element: "1")
(offset: 1, element: "2")
(offset: 2, element: "3")
(offset: 3, element: "4")
(offset: 4, element: "5")
(offset: 5, element: "6") * /Copy the code
2.2 NSString
The unique API
2.2.1 Initializing oneNSString
var nsStr = NSString();
nsStr = "123456";
print(nsStr);
Copy the code
var nsStr = NSString(stringLiteral: "123456");
Copy the code
var nsStr = NSString.init(string: "123456");
Copy the code
2.2.1 NSString
的 boolValue
attribute
- Returns true for non-zero digits that start with Y, Y, T, T, or false
var nsStr = NSString(stringLiteral: "123456");
print(nsStr.boolValue);
//true
Copy the code
2.2.2 NSString
的 isEqual()
methods
- Determines whether two strings are equal
var nsStr = NSString(stringLiteral: "123456");
print(nsStr.isEqual(to: "123456"));
//true
Copy the code
2.2.3 NSString
Numerical properties of
var nsStr = NSString(stringLiteral: "123456")
print(nsStr.intValue);
print(nsStr.floatValue);
print(nsStr.doubleValue);
Copy the code
2.3 String
与 NSString
Different written APIS that do the same thing
2.3.1 Obtaining the number of characters in a string
print(str.count); / / 6print( nsStr.length); / / 6Copy the code
2.3.2 Intercepting a string
- String Substring is returned after intercepting
var str = "123456";
print(str.prefix(str.count - 2)); //"1234"
print(str.suffix(3)); //"456"
print(str.dropLast(4)); //"12"
print(str.dropFirst(2)); //"3456"
Copy the code
- NSString Interception completed Mandatory String
var nsStr = NSString(stringLiteral: "123456");
print(nsStr.substring(to: nsStr.length - 3)) //"123"
print(nsStr.substring(from: 3)) //"456"
print(nsStr.substring(with: NSMakeRange(1, 4))) //"2345"
Copy the code
2.3.3 Adding Characters to the End of a String
- String is adding characters to itself
var str = "123456";
str.append("abc");
print(str);
//123456abc
Copy the code
- NSString is returning a new String that doesn’t change itself
var nsStr = NSString(stringLiteral: "123456");
let nsStr1 = nsStr.appending("abc");
print(nsStr); / / 123456print(nsStr1); //123456abc
Copy the code
2.4 String
与 NSString
The same API
2.4.1 Whether the prefix/suffix is specified
var str = "123456";
str.hasPrefix("12") / /true
str.hasSuffix("56") / /true
var nsStr = NSString(stringLiteral: "123456");
nsStr.hasPrefix("12") / /true
nsStr.hasSuffix("56") / /true
Copy the code
2.4.2 Splitting strings with specific characters
var str = "The 123456-11";
str.components(separatedBy: "-"); / / /"123456"."11"]
var nsStr = NSString(stringLiteral: "123456-22");
nsStr.components(separatedBy: "-"); / / /"123456"."22"]
Copy the code
2.4.3 Case conversion
var str = "abc";
print(str.uppercased()); //ABC
print(str.lowercased()); //abc
var nsStr = NSString(stringLiteral: "cba");
print(nsStr.uppercased); //CBA
print(nsStr.lowercased); //cba
Copy the code
2.4.4 Capitalize the first letter
var str = "abc";
print(str.capitalized); //Abc
var nsStr = NSString(stringLiteral: "cba");
print(nsStr.capitalized); //Cba
Copy the code
2.4.5 Removing a specific string
- The blank space
.whitespaces
- A newline
.newlines
- digital
.decimalDigits
- The letter
.letters
- The capital letters
.uppercaseLetters
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
Copy the code
2.4.6 Character replacement
- If the character you want to replace cannot be found, the original string is returned
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"
Copy the code
2.4.7 Ten \ 16 \ octal number turn string
- Result Mandatory String
let hexStr = String().appendingFormat("%x",16)// Convert decimal to hexadecimal, the result is"10"
let oStr = String().appendingFormat("%o",16)// Convert decimal to octal, resulting in"20"
let dStr = String().appendingFormat("%d",0x10)// Convert hexadecimal to decimal, and the result is"16"
let dStr1 = String(format: "%d", 0o10)// Convert octal to decimal, resulting in"8"
Copy the code
- The result is an NSString
let hexNSStr = NSString().appendingFormat("%x", 16)// Convert decimal to hexadecimal, the result is"10"
let oNSStr = NSString().appendingFormat("%o",16)// Convert decimal to octal, resulting in"20"
let dNSStr = NSString().appendingFormat("%d",0x10)// Convert hexadecimal to decimal, and the result is"16"
let dNSStr1 = NSString(format: "%d", 0o10)// Convert octal to decimal, resulting in"8"
Copy the code