yeyo yeyo - 4 months ago 8
Swift Question

How can I identify uppercase and lowercase characters in a string with swift?

This is what I have so far (for testing purpose):

let string = "The quick BroWn fOX jumpS Over tHe lazY DOg"

for chr in string {
if isupper(String(chr)) {
print(String(chr).lowercaseString)
continue
}
print(chr)
}


how can I test for uppercase and lowercase characters?

I know I can call C functions from swift, but this does not seems to be correct for me. How can I do this with swift only?

Answer

You could always see if the lowercase representation is different from the current value;

let string = "The quick BroWn fOX jumpS Over tHe lazY DOg"
var output = ""

for chr in string {
    var str = String(chr)
    if str.lowercaseString != str {
        output += str
    }
}
print(output)

>>> TBWOXSOHYDO
Comments