美文网首页
Swift 字符

Swift 字符

作者: 西风那个吹呀吹 | 来源:发表于2021-03-30 12:46 被阅读0次
    • 字符是否是ASCII码值
    /// A Boolean value indicating whether this is an ASCII character.
    @inlinable public var isASCII: Bool { get }
    
    • ASCII码值
    @inlinable public var asciiValue: UInt8? { get }
    
    • 字符是否是空格
    /// A Boolean value indicating whether this character represents whitespace,
    /// including newlines.
    ///
    /// For example, the following characters all represent whitespace:
    ///
    /// - "\t" (U+0009 CHARACTER TABULATION) 制表符
    /// - " " (U+0020 SPACE) 空格
    /// - U+2029 PARAGRAPH SEPARATOR 段落分隔符
    /// - U+3000 IDEOGRAPHIC SPACE 表意空格
    public var isWhitespace: Bool { get }
    
    • 字符是否是换行符
        /// A Boolean value indicating whether this character represents a newline.
        ///
        /// For example, the following characters all represent newlines:
        ///
        /// - "\n" (U+000A): LINE FEED (LF)
        /// - U+000B: LINE TABULATION (VT)
        /// - U+000C: FORM FEED (FF)
        /// - "\r" (U+000D): CARRIAGE RETURN (CR)
        /// - "\r\n" (U+000D U+000A): CR-LF
        /// - U+0085: NEXT LINE (NEL)
        /// - U+2028: LINE SEPARATOR
        /// - U+2029: PARAGRAPH SEPARATOR
        @inlinable public var isNewline: Bool { get }
    
    • 字符是否是数字
        /// A Boolean value indicating whether this character represents a number.
        ///
        /// For example, the following characters all represent numbers:
        ///
        /// - "7" (U+0037 DIGIT SEVEN)
        /// - "⅚" (U+215A VULGAR FRACTION FIVE SIXTHS)
        /// - "㊈" (U+3288 CIRCLED IDEOGRAPH NINE)
        /// - "𝟠" (U+1D7E0 MATHEMATICAL DOUBLE-STRUCK DIGIT EIGHT)
        /// - "๒" (U+0E52 THAI DIGIT TWO)
        public var isNumber: Bool { get }
    
    • 字符是否是整数
        /// A Boolean value indicating whether this character represents a whole
        /// number.
        ///
        /// For example, the following characters all represent whole numbers:
        ///
        /// - "1" (U+0031 DIGIT ONE) => 1
        /// - "५" (U+096B DEVANAGARI DIGIT FIVE) => 5
        /// - "๙" (U+0E59 THAI DIGIT NINE) => 9
        /// - "万" (U+4E07 CJK UNIFIED IDEOGRAPH-4E07) => 10_000
        @inlinable public var isWholeNumber: Bool { get }
    
    • isNumber与isWholeNumber的区别
    let number: Character = "⅚";
    print(number.isNumber) // 打印结果: true
    print(number.isWholeNumber) // 打印结果: false
    
    • 字符转整数
        /// The numeric value this character represents, if it represents a whole
        /// number.
        ///
        /// If this character does not represent a whole number, or the value is too
        /// large to represent as an `Int`, the value of this property is `nil`.
        ///
        ///     let chars: [Character] = ["4", "④", "万", "a"]
        ///     for ch in chars {
        ///         print(ch, "-->", ch.wholeNumberValue)
        ///     }
        ///     // 4 --> 4
        ///     // ④ --> 4
        ///     // 万 --> 10000
        ///     // a --> nil
        public var wholeNumberValue: Int? { get }
    
    • 字符是否是十六进制数字
        /// A Boolean value indicating whether this character represents a
        /// hexadecimal digit.
        ///
        /// Hexadecimal digits include 0-9, Latin letters a-f and A-F, and their
        /// fullwidth compatibility forms. To get the character's value, use the
        /// `hexDigitValue` property.
        @inlinable public var isHexDigit: Bool { get }
    
    • 字符转十六进制数字
        /// The numeric value this character represents, if it is a hexadecimal digit.
        ///
        /// Hexadecimal digits include 0-9, Latin letters a-f and A-F, and their
        /// fullwidth compatibility forms. If the character does not represent a
        /// hexadecimal digit, the value of this property is `nil`.
        ///
        ///     let chars: [Character] = ["1", "a", "F", "g"]
        ///     for ch in chars {
        ///         print(ch, "-->", ch.hexDigitValue)
        ///     }
        ///     // 1 --> 1
        ///     // a --> 10
        ///     // F --> 15
        ///     // g --> nil
        public var hexDigitValue: Int? { get }
    
    • 字符是否为字母
        /// A Boolean value indicating whether this character is a letter.
        ///
        /// For example, the following characters are all letters:
        ///
        /// - "A" (U+0041 LATIN CAPITAL LETTER A)
        /// - "é" (U+0065 LATIN SMALL LETTER E, U+0301 COMBINING ACUTE ACCENT)
        /// - "ϴ" (U+03F4 GREEK CAPITAL THETA SYMBOL)
        /// - "ڈ" (U+0688 ARABIC LETTER DDAL)
        /// - "日" (U+65E5 CJK UNIFIED IDEOGRAPH-65E5)
        /// - "ᚨ" (U+16A8 RUNIC LETTER ANSUZ A)
        public var isLetter: Bool { get }
    
    • 返回字符的大写版本,返回值为字符串
        /// Returns an uppercased version of this character.
        ///
        /// Because case conversion can result in multiple characters, the result
        /// of `uppercased()` is a string.
        ///
        ///     let chars: [Character] = ["e", "é", "и", "π", "ß", "1"]
        ///     for ch in chars {
        ///         print(ch, "-->", ch.uppercased())
        ///     }
        ///     // e --> E
        ///     // é --> É
        ///     // и --> И
        ///     // π --> Π
        ///     // ß --> SS
        ///     // 1 --> 1
        public func uppercased() -> String
    
    • 返回字符的小写版,返回值为字符串
        /// Returns a lowercased version of this character.
        ///
        /// Because case conversion can result in multiple characters, the result
        /// of `lowercased()` is a string.
        ///
        ///     let chars: [Character] = ["E", "É", "И", "Π", "1"]
        ///     for ch in chars {
        ///         print(ch, "-->", ch.lowercased())
        ///     }
        ///     // E --> e
        ///     // É --> é
        ///     // И --> и
        ///     // Π --> π
        ///     // 1 --> 1
        public func lowercased() -> String
    
    • 字符是否为大写
        /// A Boolean value indicating whether this character is considered uppercase.
        ///
        /// Uppercase characters vary under case-conversion to lowercase, but not when
        /// converted to uppercase. The following characters are all uppercase:
        ///
        /// - "É" (U+0045 LATIN CAPITAL LETTER E, U+0301 COMBINING ACUTE ACCENT)
        /// - "И" (U+0418 CYRILLIC CAPITAL LETTER I)
        /// - "Π" (U+03A0 GREEK CAPITAL LETTER PI)
        @inlinable public var isUppercase: Bool { get }
    
    • 字符是否为小写
        /// A Boolean value indicating whether this character is considered lowercase.
        ///
        /// Lowercase characters change when converted to uppercase, but not when
        /// converted to lowercase. The following characters are all lowercase:
        ///
        /// - "é" (U+0065 LATIN SMALL LETTER E, U+0301 COMBINING ACUTE ACCENT)
        /// - "и" (U+0438 CYRILLIC SMALL LETTER I)
        /// - "π" (U+03C0 GREEK SMALL LETTER PI)
        @inlinable public var isLowercase: Bool { get }
    
    • 字符是否是以任何形式改变
        /// A Boolean value indicating whether this character changes under any form
        /// of case conversion.
        @inlinable public var isCased: Bool { get }
    
    • 字符是否符号
        /// A Boolean value indicating whether this character represents a symbol.
        ///
        /// This property is `true` only for characters composed of scalars in the
        /// "Math_Symbol", "Currency_Symbol", "Modifier_Symbol", or "Other_Symbol"
        /// categories in the
        /// [Unicode Standard](https://unicode.org/reports/tr44/#General_Category_Values).
        ///
        /// For example, the following characters all represent symbols:
        ///
        /// - "®" (U+00AE REGISTERED SIGN)
        /// - "⌹" (U+2339 APL FUNCTIONAL SYMBOL QUAD DIVIDE)
        /// - "⡆" (U+2846 BRAILLE PATTERN DOTS-237)
        public var isSymbol: Bool { get }
    
    • 字符是否是数学符号
        /// A Boolean value indicating whether this character represents a symbol
        /// that naturally appears in mathematical contexts.
        ///
        /// For example, the following characters all represent math symbols:
        ///
        /// - "+" (U+002B PLUS SIGN)
        /// - "∫" (U+222B INTEGRAL)
        /// - "ϰ" (U+03F0 GREEK KAPPA SYMBOL)
        ///
        /// The set of characters that have an `isMathSymbol` value of `true` is not
        /// a strict subset of those for which `isSymbol` is `true`. This includes
        /// characters used both as letters and commonly in mathematical formulas.
        /// For example, "ϰ" (U+03F0 GREEK KAPPA SYMBOL) is considered both a
        /// mathematical symbol and a letter.
        ///
        /// This property corresponds to the "Math" property in the
        /// [Unicode Standard](http://www.unicode.org/versions/latest/).
        public var isMathSymbol: Bool { get }
    
    • 字符是否表示货币符号
        /// A Boolean value indicating whether this character represents a currency
        /// symbol.
        ///
        /// For example, the following characters all represent currency symbols:
        ///
        /// - "$" (U+0024 DOLLAR SIGN)
        /// - "¥" (U+00A5 YEN SIGN)
        /// - "€" (U+20AC EURO SIGN)
        public var isCurrencySymbol: Bool { get }
    
    • 字符是否表示标点符号
        /// A Boolean value indicating whether this character represents punctuation.
        ///
        /// For example, the following characters all represent punctuation:
        ///
        /// - "!" (U+0021 EXCLAMATION MARK)
        /// - "؟" (U+061F ARABIC QUESTION MARK)
        /// - "…" (U+2026 HORIZONTAL ELLIPSIS)
        /// - "—" (U+2014 EM DASH)
        /// - "“" (U+201C LEFT DOUBLE QUOTATION MARK)
        public var isPunctuation: Bool { get }
    
    • ... 方法,返回包含两个边界的闭合范围
        /// Returns a closed range that contains both of its bounds.
        ///
        /// Use the closed range operator (`...`) to create a closed range of any type
        /// that conforms to the `Comparable` protocol. This example creates a
        /// `ClosedRange<Character>` from "a" up to, and including, "z".
        ///
        ///     let lowercase = "a"..."z"
        ///     print(lowercase.contains("z"))
        ///     // Prints "true"
        ///
        /// - Parameters:
        ///   - minimum: The lower bound for the range.
        ///   - maximum: The upper bound for the range.
        public static func ... (minimum: Character, maximum: Character) -> ClosedRange<Character>
    
    • ..<方法,左边界到右边界的区间,值不包括右边界值
        /// Returns a half-open range that contains its lower bound but not its upper
        /// bound.
        ///
        /// Use the half-open range operator (`..<`) to create a range of any type
        /// that conforms to the `Comparable` protocol. This example creates a
        /// `Range<Double>` from zero up to, but not including, 5.0.
        ///
        ///     let lessThanFive = 0.0..<5.0
        ///     print(lessThanFive.contains(3.14))  // Prints "true"
        ///     print(lessThanFive.contains(5.0))   // Prints "false"
        ///
        /// - Parameters:
        ///   - minimum: The lower bound for the range.
        ///   - maximum: The upper bound for the range.
        public static func ..< (minimum: Character, maximum: Character) -> Range<Character>
    
    
    • prefix ..<方法,返回最大但不包括其上限的部分范围
        /// Returns a partial range up to, but not including, its upper bound.
        ///
        /// Use the prefix half-open range operator (prefix `..<`) to create a
        /// partial range of any type that conforms to the `Comparable` protocol.
        /// This example creates a `PartialRangeUpTo<Double>` instance that includes
        /// any value less than `5.0`.
        ///
        ///     let upToFive = ..<5.0
        ///
        ///     upToFive.contains(3.14)       // true
        ///     upToFive.contains(6.28)       // false
        ///     upToFive.contains(5.0)        // false
        ///
        /// You can use this type of partial range of a collection's indices to
        /// represent the range from the start of the collection up to, but not
        /// including, the partial range's upper bound.
        ///
        ///     let numbers = [10, 20, 30, 40, 50, 60, 70]
        ///     print(numbers[..<3])
        ///     // Prints "[10, 20, 30]"
        ///
        /// - Parameter maximum: The upper bound for the range.
        prefix public static func ..< (maximum: Character) -> PartialRangeUpTo<Character>
    
    • prefix ...方法,返回部分范围(包括上限)
        /// Returns a partial range up to, and including, its upper bound.
        ///
        /// Use the prefix closed range operator (prefix `...`) to create a partial
        /// range of any type that conforms to the `Comparable` protocol. This
        /// example creates a `PartialRangeThrough<Double>` instance that includes
        /// any value less than or equal to `5.0`.
        ///
        ///     let throughFive = ...5.0
        ///
        ///     throughFive.contains(4.0)     // true
        ///     throughFive.contains(5.0)     // true
        ///     throughFive.contains(6.0)     // false
        ///
        /// You can use this type of partial range of a collection's indices to
        /// represent the range from the start of the collection up to, and
        /// including, the partial range's upper bound.
        ///
        ///     let numbers = [10, 20, 30, 40, 50, 60, 70]
        ///     print(numbers[...3])
        ///     // Prints "[10, 20, 30, 40]"
        ///
        /// - Parameter maximum: The upper bound for the range.
        prefix public static func ... (maximum: Character) -> PartialRangeThrough<Character>
    
    • postfix ...方法,返回部分范围,包括下限
        /// Returns a partial range extending upward from a lower bound.
        ///
        /// Use the postfix range operator (postfix `...`) to create a partial range
        /// of any type that conforms to the `Comparable` protocol. This example
        /// creates a `PartialRangeFrom<Double>` instance that includes any value
        /// greater than or equal to `5.0`.
        ///
        ///     let atLeastFive = 5.0...
        ///
        ///     atLeastFive.contains(4.0)     // false
        ///     atLeastFive.contains(5.0)     // true
        ///     atLeastFive.contains(6.0)     // true
        ///
        /// You can use this type of partial range of a collection's indices to
        /// represent the range from the partial range's lower bound up to the end
        /// of the collection.
        ///
        ///     let numbers = [10, 20, 30, 40, 50, 60, 70]
        ///     print(numbers[3...])
        ///     // Prints "[40, 50, 60, 70]"
        ///
        /// - Parameter minimum: The lower bound for the range.
        postfix public static func ... (minimum: Character) -> PartialRangeFrom<Character>
    
    • > <= >= != 方法
        @inlinable public static func > (lhs: Character, rhs: Character) -> Bool
        @inlinable public static func <= (lhs: Character, rhs: Character) -> Bool
        @inlinable public static func >= (lhs: Character, rhs: Character) -> Bool
        public static func != (lhs: Character, rhs: Character) -> Bool
    

    相关文章

      网友评论

          本文标题:Swift 字符

          本文链接:https://www.haomeiwen.com/subject/pdiwhltx.html