How to Identify Uppercase and Lowercase Characters in a String with Swift

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

In Swift 5, we can now check for character properties per Unicode standard.

For your question, chr.isUppercase and chr.isLowercase is the answer.

Check if a character is lowerCase or upperCase


 var input = "The quick BroWn fOX jumpS Over tHe lazY DOg"

var inputArray = Array(input)

for character in inputArray {

var strLower = "[a-z]";

var strChar = NSString(format: "%c",character )
let strTest = NSPredicate(format:"SELF MATCHES %@", strLower );
if strTest .evaluateWithObject(strChar)
{
// lower character
}
else
{
// upper character
}
}

Check if a string contains at least a upperCase letter, a digit, or a special character in Swift?

Simply replace your RegEx rule [A-Z]+ with .*[A-Z]+.* (and other RegEx rules as well)

Rules

[A-Z]+ matches only strings with all characters capitalized

Examples: AVATAR, AVA, TAR, AAAAAA

Won't work: AVATAr

.* matches all strings (0+ characters)

Examples: 1, 2, AVATAR, AVA, TAR, a, b, c

.*[A-Z]+.* matches all strings with at least one capital letter

Examples: Avatar, avataR, aVatar

Explanation:

I. .* will try to match 0 or more of anything

II. [A-Z]+ will require at least one capital letter (because of the +)

III. .* will try to match 0 or more of anything

Avatar [empty | "A" | "vatar"]

aVatar ["a" | "V" | "atar"]

aVAtar ["a" | "VA" | "tar"]

Working Code

func checkTextSufficientComplexity(var text : String) -> Bool{


let capitalLetterRegEx = ".*[A-Z]+.*"
var texttest = NSPredicate(format:"SELF MATCHES %@", capitalLetterRegEx)
var capitalresult = texttest!.evaluateWithObject(text)
println("\(capitalresult)")


let numberRegEx = ".*[0-9]+.*"
var texttest1 = NSPredicate(format:"SELF MATCHES %@", numberRegEx)
var numberresult = texttest1!.evaluateWithObject(text)
println("\(numberresult)")


let specialCharacterRegEx = ".*[!&^%$#@()/]+.*"
var texttest2 = NSPredicate(format:"SELF MATCHES %@", specialCharacterRegEx)

var specialresult = texttest2!.evaluateWithObject(text)
println("\(specialresult)")

return capitalresult || numberresult || specialresult

}

Examples:

checkTextSufficientComplexity("Avatar") // true || false || false
checkTextSufficientComplexity("avatar") // false || false || false
checkTextSufficientComplexity("avatar1") // false || true || false
checkTextSufficientComplexity("avatar!") // false || false || true

Swift 2 : Iterating and upper/lower case some characters

String has a upperCaseString method, but Character doesn't.
The reason is that in exotic languages like German, converting a single
character to upper case can result in multiple characters:

print("ß".uppercaseString) // "SS"

The toupper/tolower functions are not Unicode-safe and not
available in Swift.

So you can enumerate the string characters, convert each character to
a string, convert that to upper/lowercase, and concatenate the results:

func lowercaseDestination(str : String) -> String {
var result = ""
for c in str.characters {
let s = String(c)
if condition {
result += s.lowercaseString
} else {
result += s.uppercaseString
}
}
return result
}

which can be written more compactly as

func lowercaseDestination(str : String) -> String {
return "".join(str.characters.map { c -> String in
let s = String(c)
return condition ? s.lowercaseString : s.uppercaseString
})
}

Re your comment: If the condition needs to check more than one
character then you might want to create an array of all characters
first:

func lowercaseDestination(str : String) -> String {

var result = ""
let characters = Array(str.characters)
for i in 0 ..< characters.count {
let s = String(characters[i])
if condition {
result += s.lowercaseString
} else {
result += s.uppercaseString
}
}
return result
}

SwiftUI: search for lower and upper case letters

There is a dedicated API

return names.filter { $0.localizedCaseInsensitiveContains(searchText) }

How to use uppercaseString, lowercaseString in Swift

You can use Set for that.

let uppers  = " Hello*im ; + Upper+String "
let lowers = " Hello*im ; + Lower+String "
let allowChars : Set<Character> = Set("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLKMNOPQRSTUVWXYZ1234567890".characters)
let upperStr = String(uppers.characters.filter {allowChars.contains($0) }).uppercaseString
let lowerStr = String(lowers.characters.filter {allowChars.contains($0) }).lowercaseString
print(upperStr)
print(lowerStr)

Edit: To allow space also add space inside the Set characters with other character

let allowChars : Set<Character> = Set("abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLKMNOPQRSTUVWXYZ1234567890".characters)


Related Topics



Leave a reply



Submit