swift - 如何解决字符串大写问题

标签 swift string

我的目标很简单:

enter image description here

Date() 中的 2 个大写字母。

func getWeekday(booking: ClientBooking) {

    let formatter = CoreServices.getDateFormatter()
    formatter.dateFormat = "EEEEEE"

    let weekday = formatter.string(from: booking.date)

    print("weekday = \(weekday)")
    print("weekday.capitalized = \(weekday.capitalized)")

    self.weekdayLabel.text = weekday.capitalized

}

问题。 2 个打印语句导致:

weekday = Fr
weekday.capitalized = Fr

label.text 也是如此。我错过了什么?非常感谢您的帮助。

(当然应该是 FR)

最佳答案

来自NSString.capitalized documentation :

A capitalized string is a string with the first character in each word changed to its corresponding uppercase value, and all remaining characters set to their corresponding lowercase values.

我认为您会对同一页面的“另请参阅”部分中提到的这些属性之一感到满意:

var uppercased: String

An uppercase representation of the string.

var localizedUppercase: String

Returns a version of the string with all letters converted to uppercase, taking into account the current locale.

关于swift - 如何解决字符串大写问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45766982/

相关文章:

c - printf 上附加了奇怪的字符串

ios - 将文本拆分为数组,同时保持 Swift 中的标点符号

java - Java 中除了字符串还有不可变对象(immutable对象)吗?

ios - TableView 的 UISearchController

ios - 更改导航栏背景颜色后,self.view 的 y 位置发生更改

perl - 在 Perl 中,如何调用名称在字符串中的方法?

string - 使用 Excel-VBA 从字符串中提取 5 位数字

ios - 如何在循环中快速将值插入多维数组

ios - 自动布局在自定义 UITableViewCell 中被忽略

ios - 将唯一值添加到解析服务器中的数组(swift 3)