Ben Five learning iOS: Using Swift to get pinyin initials, support to take one sentence in each word pinyin first letter

Source: Internet
Author: User

In a recent project, encountered the problem of getting pinyin initials, looked up the method on the Internet, did not find the right to use, so he studied a bit, wrote the following methods, welcome to exchange, I hope to help you.

  pyfirst.swift//  get pinyin initials, support to take a sentence in each word pinyin first//  Created by Zhou Honey ([email protected]) on 2015/6/1 (Children's Day).//  Copyright (c) 2015 www.miw.cn. All Rights Reserved.//import Foundationfunc Pyfirst (string:string, _ Allfirst:bool=false)->String{    var py= "#"    if let S = string {        if s = = "" {            return py        }        var str = cfstringcreatemutablecopy (nil, 0, s)        CFSt Ringtransform (str, nil, Kcfstringtransformtolatin, Boolean (0))        cfstringtransform (str, nil, Kcfstringtransformstripcombiningmarks, Boolean (0))        py = ""        if Allfirst {for            x in (str as String). Componentsseparatedbystring ("") {                py + = Pyfirst (x)            }        } else {            py  = (str as nsstring). Substringtoindex (1). Uppercasestring        }    }    return py}/*//Call example, return #var s:string? Pyfirst (s) s = "People's Republic of China @hi wor\r\nld." Call example, return Zhrmghgwpyfirst (s,true)//Call example, return Zpyfirst (s) */

This may be the best present on children's Day.

Ben Five learning iOS: Using Swift to get pinyin initials, support to take one sentence in each word pinyin first letter

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.