En Swift 4 con extensiones
Mi extensión-ejemplo contiene 3 funciones: marcar hacer un String que comience con una subString, hacer un String end a un subString y hacer un String que contenga un subString.
Establezca el parámetro isCaseSensitive en false, si desea ignorar los caracteres "A" o "a", de lo contrario, configúrelo en true.
Vea los comentarios en el código para obtener más información sobre cómo funciona.
Código:
import Foundation
extension String {
// Returns true if the String starts with a substring matching to the prefix-parameter.
// If isCaseSensitive-parameter is true, the function returns false,
// if you search "sA" from "San Antonio", but if the isCaseSensitive-parameter is false,
// the function returns true, if you search "sA" from "San Antonio"
func hasPrefixCheck(prefix: String, isCaseSensitive: Bool) -> Bool {
if isCaseSensitive == true {
return self.hasPrefix(prefix)
} else {
var thePrefix: String = prefix, theString: String = self
while thePrefix.count != 0 {
if theString.count == 0 { return false }
if theString.lowercased().first != thePrefix.lowercased().first { return false }
theString = String(theString.dropFirst())
thePrefix = String(thePrefix.dropFirst())
}; return true
}
}
// Returns true if the String ends with a substring matching to the prefix-parameter.
// If isCaseSensitive-parameter is true, the function returns false,
// if you search "Nio" from "San Antonio", but if the isCaseSensitive-parameter is false,
// the function returns true, if you search "Nio" from "San Antonio"
func hasSuffixCheck(suffix: String, isCaseSensitive: Bool) -> Bool {
if isCaseSensitive == true {
return self.hasSuffix(suffix)
} else {
var theSuffix: String = suffix, theString: String = self
while theSuffix.count != 0 {
if theString.count == 0 { return false }
if theString.lowercased().last != theSuffix.lowercased().last { return false }
theString = String(theString.dropLast())
theSuffix = String(theSuffix.dropLast())
}; return true
}
}
// Returns true if the String contains a substring matching to the prefix-parameter.
// If isCaseSensitive-parameter is true, the function returns false,
// if you search "aN" from "San Antonio", but if the isCaseSensitive-parameter is false,
// the function returns true, if you search "aN" from "San Antonio"
func containsSubString(theSubString: String, isCaseSensitive: Bool) -> Bool {
if isCaseSensitive == true {
return self.range(of: theSubString) != nil
} else {
return self.range(of: theSubString, options: .caseInsensitive) != nil
}
}
}
Ejemplos de uso:
Para verificar, la cadena comienza con "TEST":
"testString123".hasPrefixCheck(prefix: "TEST", isCaseSensitive: true) // Returns false
"testString123".hasPrefixCheck(prefix: "TEST", isCaseSensitive: false) // Returns true
Para verificar, la Cadena comienza con "prueba":
"testString123".hasPrefixCheck(prefix: "test", isCaseSensitive: true) // Returns true
"testString123".hasPrefixCheck(prefix: "test", isCaseSensitive: false) // Returns true
Para comprobar, finalice la cadena con "G123":
"testString123".hasSuffixCheck(suffix: "G123", isCaseSensitive: true) // Returns false
"testString123".hasSuffixCheck(suffix: "G123", isCaseSensitive: false) // Returns true
Para comprobar, finalice la cadena con "g123":
"testString123".hasSuffixCheck(suffix: "g123", isCaseSensitive: true) // Returns true
"testString123".hasSuffixCheck(suffix: "g123", isCaseSensitive: false) // Returns true
Para verificar, la cadena contiene "RING12":
"testString123".containsSubString(theSubString: "RING12", isCaseSensitive: true) // Returns false
"testString123".containsSubString(theSubString: "RING12", isCaseSensitive: false) // Returns true
Para verificar, la cadena contiene "ring12":
"testString123".containsSubString(theSubString: "ring12", isCaseSensitive: true) // Returns true
"testString123".containsSubString(theSubString: "ring12", isCaseSensitive: false) // Returns true