This article is a summary of swift articles on www.cnswift.org/ website combined with OC related knowledge. The article will be updated with the deepening of learning. Welcome to pay attention to avoid getting lost and let us make progress together.

Create a string

swift

1. Create using string literals

A string literal is a fixed sequence of text characters surrounded by two “.

Let STR = "hello"// create a string constant using a string literal var str2 = "Hi"// create a string variable using a string literalCopy the code

Create multi-line strings by enclosing them with the “” symbol

2. Use the initialization method

var str = String()
Copy the code

The difference between creating mutable or immutable strings in Swift is the use of let or var

3. String interpolation

String interpolation involves wrapping \() around elements inserted into string literals

OC

Oc also supports literal creation as well as initialization method creation

NSString *str = @"Hello"; NSString *str2 = [NSString stringWithFormat:@"Hi"]; // The initialization method is createdCopy the code

Oc can also build strings by string interpolation, but it needs to be specified at build time

Oc creates a multi-line string in a way that differs from Swift in that it directly adds a newline character (\n) to create a multi-line string

The difference with Swift is that OC mutable strings are created via NSMutableString, while immutable strings are created via NSString

NSMutableString *mutStr = [NSMutableString stringWithString:@"1"]; [mutStr appendString:@"2"]; // Add the character NSLog(@"mutStr:%@",mutStr) to the mutable string; // Print: 12Copy the code

character

swift

Swift has a special type for a single Character, Character. This type can store only one Character, and more precisely, Character refers to a single cluster of extended glyph (an extended glyph cluster is a human-readable Character produced by one or more ordered Unicode scalars).

Like the code above, in stringscafeA character is appended\u{301}It then prints four more characters, indicating that the added character and the last character in the string are in an extended glyph cluster. The count of the printed string is still 4

OC

Oc does not have the concept of extended glyph clusters, so the above operation will print 5 characters when added in OC. The reason is that the length in NSString in OC is represented based on the number of 16-bit codes in the UTF-16 representation of the string, whereas in SWIFT it is represented by the number of Unicode extended character clusters.

String index

swift

Each string value in SWIFT has an associated index type that corresponds to the position of each Character in the string. Because Character is an extended glyph cluster, string cannot be indexed by integer values.

**let** str = "123456"
**let** start = str[str.startIndex]
print(start)//1
**let** end = str[str.index(before: str.endIndex)]
print(end)"//6

Copy the code

StartIndex: the position of the first Character Index (before:): takes the character before the index. Index (after:): takes the character after the index. Index (_:offsetBy:) Takes the character after the offset

Insert, delete, compare

swift

insert

  1. Insert (_: at:) inserts a single character into a specific index position
    • Parameter 1: character to be inserted Parameter 2: index position to be inserted
  2. Insert (contentsOf: at:) Inserts a string into a specific index position
    • Parameter 1: string to insert Parameter 2: index position to insert
Var STR = "hello" // insert a single character str.insert("! , at: str.endIndex) print(STR)// print: hello! // Insert str.insert(contentsOf: "??" , at: str.endIndex) print(STR)// print: hello! ??Copy the code

delete

  1. Remove (at:) deletes a single character
    • Parameter: Index position of the character to be deleted
  2. RemoveSubrange (_:) deletes the string
    • Parameter: Range of strings to delete

**var** STR = "Hello! ?" Str.remove (at: str.index(before: str.endIndex)) print(STR)// print: hello! // Remove the string str.removesubrange (str.startIndex.. <str.index(str.startIndex, offsetBy: 3) print(STR)// print: lo!Copy the code

To compare

equal

  1. use=To determine if the strings are equal
  2. use! =To determine that strings are not equal

Note that two strings are considered equal if their extended character clusters have the same linguistic meaning and shape.

Prefixes and suffixes are equal

  1. hasPrefix(_:..) : Whether the prefixes are equal
  2. hasSuffix(…) : Suffixes are equal

oc

increase

The string increment in oc needs to use the mutable string NSMutableString

  • AppendString: Adds a string to the target string

delete

Delete also uses’ NSMutableString ‘

  • deleteCharactersInRange:Deletes the specified interval string

To compare

  • IsEqualToString: Checks that two strings are the same

Equality of prefix and suffix

There are also ways to determine whether the prefix and suffix are equal in oc

  • HasPrefix Whether the prefixes are equal
  • HasSuffix whether suffixes are equal

This article is a summary of swift articles on www.cnswift.org/ website combined with OC related knowledge. The article will be updated with the deepening of learning. Welcome to pay attention to avoid getting lost and let us make progress together.