Uppercase All String According to Locale - Swift

前端 未结 3 1193
梦谈多话
梦谈多话 2021-01-02 15:54

I\'m trying to uppercase all string according to \'Locale\' but it is not working.

var text = \"istanbul, izmir\"

println(text.uppercaseStringWithLocale(NSL         


        
3条回答
  •  日久生厌
    2021-01-02 16:34

    NSLocale.currentLocale() is the locale which was selected in the Settings of the device. If that is "en_US" then

    text.uppercaseStringWithLocale(NSLocale.currentLocale())
    

    will use the english language rules and the result is "ISTANBUL, IZMIR".

    You can either select the turkish language in the device settings, or specify the turkish locale explicitly:

    let text = "istanbul, izmir"
    text.uppercaseStringWithLocale(NSLocale(localeIdentifier: "tr"))
    // İSTANBUL, İZMİR
    

    Swift 3:

    text.uppercased(with: Locale(identifier: "tr"))
    

提交回复
热议问题