     ///
     /// - Parameter range: 指定的范围
     /// - Returns: 截取到的字符串
-    public func substring(with range: Range<Int>) -> String? {
-        guard (range.upperBound <= string.count) && (range.lowerBound >= 0) else {
-            print("\(#function): index out of range")
-            return nil
-        }
-        let start = string.index(string.startIndex, offsetBy: range.lowerBound)
-        let length = range.count
+    public func substring(with range: Range<Int>) -> String {
+        guard let startIndex = string.validStartIndex(original: range.lowerBound),
+            let endIndex = string.validEndIndex(original: range.upperBound),
+            startIndex < endIndex else {
+                print("\(#function): index out of range")
+                return ""
+        }
+        let start = string.index(string.startIndex, offsetBy: startIndex.encodedOffset)
+        let validLength = endIndex.encodedOffset - startIndex.encodedOffset
         let str = String(string.suffix(from: start))
-        return String(str.prefix(length))
+        return String(str.prefix(validLength))
     }
 }

    ///
    /// - Parameter original: 待检验的索引值
    /// - Returns: 经过检验的有效索引值或 nil
    private func validStartIndex(original: Int) -> String.Index? {
        guard original <= endIndex.encodedOffset else { return nil }
        return validIndex(original: original)
    }

    ///
    /// - Parameter original: 待检验的索引值
    /// - Returns: 经过检验的有效索引值或 nil
    fileprivate func validEndIndex(original: Int) -> String.Index? {
        guard original >= startIndex.encodedOffset else { return nil }
        return validIndex(original: original)
    }
