快速連結

2022年12月25日

幫文字加入星號來遮罩文字

參考網址:How to mask a String to show only the last 3 characters?

extension String{
      /// 幫文字加入星號來遮罩文字
    /// - Parameters:
    ///   - startIndex: 開始遮罩的位置(含)
    ///   - endIndex: 結束遮罩的位置(不含),請勿超過文字長度,沒有做防呆
    /// - Returns: 遮罩後文字
    func maskToStarredWords(_ startIndex: Int, _ endIndex: Int) -> String{
        return prefix(startIndex) + String(repeating: "*", count: Swift.min(endIndex - startIndex + 1, count)) + suffix(count - endIndex - 1)
    }
}