检查字符是小写还是大写
Check if a character is lowerCase or upperCase
我正在尝试编写一个程序,将字符串存储在名为 input
的变量中。
使用这个 input
变量,然后我尝试将其转换为数组,然后使用 for 循环测试数组中的每个字符是否为小写。我怎样才能做到这一点?
这是我的进展:
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
}
*/
}
您可以检查每个字符的ascii值,因为大写和小写是不同的值,因为大写是65-90,小写是97-122
您应该使用正则表达式:grep [A-Z] 与 grep [a-z]。
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
}
}
Swift 3
static func isLowercase(string: String) -> Bool {
let set = CharacterSet.lowercaseLetters
if let scala = UnicodeScalar(string) {
return set.contains(scala)
} else {
return false
}
}
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([=10=]) {
print("upper: \([=10=])")
} else if lowers.contains([=10=]) {
print("lower: \([=10=])")
}
}
这是写在 Swift 4 上的答案,无论输入字符串是单个字母还是多个字母,它都有效:
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
}
}
如果必须的话,我可能会编写如下代码:
extension Character {
func isMember(of set: CharacterSet) -> Bool {
guard let scalar = UnicodeScalar(String(self)) else { return false }
return set.contains(scalar)
}
var isUppercase: Bool {
return isMember(of: .uppercaseLetters)
}
// var isLowercase, isWhitespace, etc.
}
let result = "The quick BroWn fOX jumpS Over tHe lazY DOg"
.map { [=10=].isUppercase ? String([=10=]).lowercased() : String([=10=]).uppercased() }
.joined()
print(result)
// "tHE QUICK bROwN Fox JUMPs oVER ThE LAZy doG"
基于字符比较的解决方案
let input = "The quick BroWn fOX jumpS Over tHe lazY DOg"
for char in input {
if char >= "A" && char <= "Z" {
print("upper: \(char)")
} else if char >= "a" && char <= "z" {
print("lower: \(char)")
} else {
print("something else: '\(char)'")
}
}
我正在尝试编写一个程序,将字符串存储在名为 input
的变量中。
使用这个 input
变量,然后我尝试将其转换为数组,然后使用 for 循环测试数组中的每个字符是否为小写。我怎样才能做到这一点?
这是我的进展:
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
}
*/
}
您可以检查每个字符的ascii值,因为大写和小写是不同的值,因为大写是65-90,小写是97-122
您应该使用正则表达式:grep [A-Z] 与 grep [a-z]。
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
}
}
Swift 3
static func isLowercase(string: String) -> Bool {
let set = CharacterSet.lowercaseLetters
if let scala = UnicodeScalar(string) {
return set.contains(scala)
} else {
return false
}
}
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([=10=]) {
print("upper: \([=10=])")
} else if lowers.contains([=10=]) {
print("lower: \([=10=])")
}
}
这是写在 Swift 4 上的答案,无论输入字符串是单个字母还是多个字母,它都有效:
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
}
}
如果必须的话,我可能会编写如下代码:
extension Character {
func isMember(of set: CharacterSet) -> Bool {
guard let scalar = UnicodeScalar(String(self)) else { return false }
return set.contains(scalar)
}
var isUppercase: Bool {
return isMember(of: .uppercaseLetters)
}
// var isLowercase, isWhitespace, etc.
}
let result = "The quick BroWn fOX jumpS Over tHe lazY DOg"
.map { [=10=].isUppercase ? String([=10=]).lowercased() : String([=10=]).uppercased() }
.joined()
print(result)
// "tHE QUICK bROwN Fox JUMPs oVER ThE LAZy doG"
基于字符比较的解决方案
let input = "The quick BroWn fOX jumpS Over tHe lazY DOg"
for char in input {
if char >= "A" && char <= "Z" {
print("upper: \(char)")
} else if char >= "a" && char <= "z" {
print("lower: \(char)")
} else {
print("something else: '\(char)'")
}
}