Skip to content

Instantly share code, notes, and snippets.

@0x30
Last active March 31, 2023 07:48
Show Gist options
  • Save 0x30/15abad6afaa0ce6e7d49dd28e705772c to your computer and use it in GitHub Desktop.
Save 0x30/15abad6afaa0ce6e7d49dd28e705772c to your computer and use it in GitHub Desktop.
城市代码 获取 Emoij , 字符串 拼音 处理
//
// String+Country.swift
// Rate
//
// Created by 0x30 on 2017/11/20.
// Copyright © 2017年 s. All rights reserved.
//
import Foundation
extension String{
/// 根据 城市Code 获取 Emoij 表情
///
/// - Parameter country: 国家代码
/// - Returns: 城市Emoij 表情字符串
static func flag(country:String) -> String {
let base : UInt32 = 127397
var s = ""
for v in country.unicodeScalars {
s.unicodeScalars.append(UnicodeScalar(base + v.value)!)
}
return String(s)
}
/// 去掉字符串的前后字符串
///
/// - Returns: 处理后的字符串
func trim() -> String {
return self.trimmingCharacters(in: .whitespacesAndNewlines)
}
/// 将中文 转换为 拼音字符串
///
/// - Returns: 拼音字符串
func toPinYin() -> String{
let string = CFStringCreateMutableCopy(nil, 0, self as CFString);
CFStringTransform(string, nil, kCFStringTransformMandarinLatin, false);
CFStringTransform(string, nil, kCFStringTransformStripDiacritics, false);
if let str = string as String?{
return str.replacingOccurrences(of: " ", with: "")
}
return self
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment