On the 03 string operation of swift

Keywords: Swift

String operation

01 get length

var a = "he l lo"
print(a.count) // Calculated space, output 7

02 String.Index type

The String.Index type represents the position of a character in a string.

You can use a[String.Index] to get the character at a certain position.

var a = "hello"
print(a.startIndex) // Output Index(_rawBits: 1)
print(a[a.startIndex]) // Get the first bit of string, output h
  • How to output the last bit?

    print(a[a.endIndex]) / / an error is reported, because the position of endIndex is the end of the string, not the last character you see

    You can use a.index(before: String.Index) and a.index(after: String.Index) to get the first and last bits of a location respectively.

    a[a.index(before: a.endIndex)] can get the last character of the string.

  • How to output a specified bit?

    a[a.index(a.startIndex, offsetBy: 2)], which means starting from a.startIndex and counting two digits later.

    If a negative number is used after offsetBy, it is from the back to the front.

  • Output characters of a certain segment

    var begin = a.index(a.startIndex, offsetBy: 1)
    var end = a.index(a.startIndex, offsetBy:4)
    print(str[begin...end]) // Output ello
    

    Or use the prefix(Int) method to get the first n characters:

    var str = a.prefix(2)
    print(str) // Output he
    
  • How to find the location of the first occurrence of a character

    a.firstIndex(of: "e")

03 add, delete, modify and check

1. check

  • Determine whether the character is in the string

    Use the contains(Char) method: (case sensitive)

    var str = "hello"
    print(str.contains("h")) // true
    print(str.contains("hel")) // true
    

    You can also use the contains(where: String.contains(""): (this method returns the true value if there is only one contain)

    var str = "hello"
    print(str.contains(where: String.contains("ae"))) // true
    
  • Determine whether the beginning or end of a string is a character

    You can use hasPrefix("") to determine the beginning

    var str = "hello"
    print(str.hasPrefix("h")) // true
    print(str.hasPrefix("he")) // true
    print(str.hasPrefix("e")) // false
    

    Use hasSuffix() to judge the end

    var str = "hello"
    print(str.hasPrefix("o")) // true
    print(str.hasPrefix("lo")) // true
    print(str.hasPrefix("ol")) // false
    

2. increase

  • Add a new string at the end of the string

    append() can:

    var str = "hello"
    str.append(" world") // hello world
    
  • Add a string to a location

    insert(contentsOf: str, at: String.Index) can insert str before the at position:

    var str = "hello"
    str.insert(contentsOf: "AAA", at: str.startIndex) // 	AAAhello
    

3. changes

  • Replace a field

    Replacesubrange (section, with: "Lalalala") section is the range of String.Index, and is replaced with "lalala":

    var str = "hello"
    let section = str.startIndex...str.index(str.endIndex, offsetBy: -2)
    str.replaceSubrange(section, with: "lalala") // "lalalao"
    

    You can also use replaceingoccurrences (of: STR, with: "jj") to replace the str field with "jj":

    var str = "hello"
    str.replacingOccurrences(of: "ll", with: "jj") // "hejjo"
    

    If there is no such field, it will not be replaced

4. delete

  • Delete characters in a location

    remove(at: String.Index)

    var str = "hello"
    str.remove(at: str.index(str.startIndex, offsetBy: 2)) // l
    print(str) // helo
    
  • Delete a field

    removeSubrange(String.Index...String.Index)

    var str = "hello"
    str.removeSubrange(str.startIndex...str.index(str.endIndex, offsetBy: -2))
    print(str) // o
    

04 traversing strings with for loop

  • Direct traversal

    var str = "hello"
    for item in str {
      print(item)
    }
    
  • Use the index() method to traverse

    var str = "hello"
    for item in 0..<str.count {
        print(str[str.index(str.startIndex, offsetBy: item)])
    }
    

Posted by Jenski on Fri, 17 Apr 2020 21:45:53 -0700