In Swift 4 con estensioni
Il mio esempio di estensione contiene 3 funzioni: spuntare fare un inizio stringa con una sottostringa, fare una fine stringa a una sottostringa e fare una stringa contiene una sottostringa.
Impostare il parametro isCaseSensitive su false, se si desidera ignorare i caratteri "A" o "a", altrimenti impostarlo su true.
Vedi i commenti nel codice per maggiori informazioni su come funziona.
Codice:
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
}
}
}
Esempi su come usare:
Per il controllo, iniziare la stringa con "TEST":
"testString123".hasPrefixCheck(prefix: "TEST", isCaseSensitive: true) // Returns false
"testString123".hasPrefixCheck(prefix: "TEST", isCaseSensitive: false) // Returns true
Per il controllo, iniziare la stringa con "test":
"testString123".hasPrefixCheck(prefix: "test", isCaseSensitive: true) // Returns true
"testString123".hasPrefixCheck(prefix: "test", isCaseSensitive: false) // Returns true
Per il controllo, la stringa termina con "G123":
"testString123".hasSuffixCheck(suffix: "G123", isCaseSensitive: true) // Returns false
"testString123".hasSuffixCheck(suffix: "G123", isCaseSensitive: false) // Returns true
Per il controllo, la stringa termina con "g123":
"testString123".hasSuffixCheck(suffix: "g123", isCaseSensitive: true) // Returns true
"testString123".hasSuffixCheck(suffix: "g123", isCaseSensitive: false) // Returns true
Per il controllo, la stringa contiene "RING12":
"testString123".containsSubString(theSubString: "RING12", isCaseSensitive: true) // Returns false
"testString123".containsSubString(theSubString: "RING12", isCaseSensitive: false) // Returns true
Per il controllo, la stringa contiene "ring12":
"testString123".containsSubString(theSubString: "ring12", isCaseSensitive: true) // Returns true
"testString123".containsSubString(theSubString: "ring12", isCaseSensitive: false) // Returns true