[Swift]判斷連續重複,遞增,遞減的英數字
想要判斷連續重複,遞增,遞減的英數字例如abcd 1234時
我們用把字串第一位轉為uniCode後再依字串長度去產生新的字串
最後比對元字串與新字串是否一致來判斷
以下例子為判斷字串中是否含有四位連續相同或是遞增遞減之英數字
private func checkHasFourContinuousOrSameWord(str:String)->Int{
let newPwsd = newCustPswdChg.value
for i in 0..<newPwsd.count{
let start = newPwsd.index(newPwsd.startIndex, offsetBy: i)
var sameStr = ""
var raisingStr = ""
var descendingStr = ""
for k in 0...3{
let indexNowChar = newPwsd[start]
//連續重複
sameStr += newPwsd[start...start]
//第一位unicode不用加或減
if k == 0 {
raisingStr += String(indexNowChar)
descendingStr += String(indexNowChar)
continue
}
//升冪
if let firstChar = indexNowChar.unicodeScalars.first {
let nextUnicode = firstChar.value + UInt32(k)
if let nextChar = UnicodeScalar(nextUnicode) {
var nextString = ""
nextString.append(Character(UnicodeScalar(nextChar)))
print(nextString)
raisingStr += nextString
}
}
//降冪
if let firstChar = indexNowChar.unicodeScalars.first {
let nextUnicode = firstChar.value - UInt32(k)
if let nextChar = UnicodeScalar(nextUnicode) {
var nextString = ""
nextString.append(Character(UnicodeScalar(nextChar)))
print(nextString)
descendingStr += nextString
}
}
}
if newPwsd.contains(sameStr){// 字串包含四位以上的重複字元
return 1
}
if newPwsd.contains(raisingStr) || newPwsd.contains(descendingStr){// 字串包含四位以上的遞增或遞減字元
return 2
}
}
return -1// 字串不包含四位以上的連續重複、相同、遞增或遞減字元
}
}