Check if character is lowerCase or upperCase

I am trying to create a program that stores a string in a variable called input .

With this input variable, I am trying to convert it to an array, and then check with a for loop whether each character in the lowerCase array will be or not. How can i achieve this?

Here is how far I got:

 var input = "The quick BroWn fOX jumpS Over tHe lazY DOg" var inputArray = Array(input) for character in inputArray { /* if character is lower case { make it uppercase } else { make it lowercase } */ } 
+5
source share
6 answers
  var input = "The quick BroWn fOX jumpS Over tHe lazY DOg" var inputArray = Array(input) for character in inputArray { var strLower = "[az]"; var strChar = NSString(format: "%c",character ) let strTest = NSPredicate(format:"SELF MATCHES %@", strLower ); if strTest .evaluateWithObject(strChar) { // lower character } else { // upper character } } 
+4
source

Swift 3

 static func isLowercase(string: String) -> Bool { let set = CharacterSet.lowercaseLetters if let scala = UnicodeScalar(string) { return set.contains(scala) } else { return false } } 
+3
source

You should use regexp: grep [AZ] compared to grep [az].

0
source

Swift 4:

 var input = "The quick BroWn fOX jumpS Over tHe lazY DOg" let uppers = CharacterSet.uppercaseLetters let lowers = CharacterSet.lowercaseLetters input.unicodeScalars.forEach { if uppers.contains($0) { print("upper: \($0)") } else if lowers.contains($0) { print("lower: \($0)") } } 
0
source

Here, the answer written in Swift 4, which works with the input string, is one or more letters:

  extension String { static func isLowercase(string: String) -> Bool { let set = CharacterSet.lowercaseLetters for character in string { if let scala = UnicodeScalar(String(character)) { if !set.contains(scala) { return false } } } return true } static func isUppercase(string: String) -> Bool { let set = CharacterSet.uppercaseLetters for character in string { if let scala = UnicodeScalar(String(character)) { if !set.contains(scala) { return false } } } return true } } 
0
source

You can check the ascii value for each character, because the upper and lower case are different values, since the upper ones are 65-90, and the lower ones 97-122

-3
source

Source: https://habr.com/ru/post/1213895/


All Articles