David Seek David Seek - 1 month ago 8
Swift Question

How to fix issue with String capitalization

My goal is quiet simple:

enter image description here

2 capitalized letters from a

Date()
.

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

}


The problem. The 2 print statements result in:

weekday = Fr
weekday.capitalized = Fr


Same does the
label.text
. What am I missing? Help is very appreciated.

(Expected of course would be
FR
)

Answer Source

From the 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.

I think you'd be happier with one of these properties mentioned in the “See Also” section of the same page:

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.