এক্সটেনশন সহ সুইফট 4 এ
আমার এক্সটেনশন-উদাহরণে 3 টি ফাংশন রয়েছে: একটি সাবস্ট্রিং দিয়ে স্ট্রিং শুরু করুন , সাবস্ট্রিংয়ের জন্য একটি স্ট্রিং শেষ করুন এবং কোনও স্ট্রিংয়ে একটি সাবস্ট্রিং রয়েছে তা পরীক্ষা করুন।
IsCaseSensitive- প্যারামিটারটিকে মিথ্যাতে সেট করুন, আপনি যদি অগ্রাহ্য করতে চান তবে "A" বা "a" অক্ষর, অন্যথায় এটি সত্য হিসাবে সেট করুন।
এটি কীভাবে কাজ করে তার আরও তথ্যের জন্য কোডটিতে মন্তব্যগুলি দেখুন।
কোড:
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
}
}
}
কীভাবে ব্যবহার করবেন উদাহরণ:
স্ট্রিংটি "টেস্ট" দিয়ে শুরু করার জন্য পরীক্ষা করুন:
"testString123".hasPrefixCheck(prefix: "TEST", isCaseSensitive: true) // Returns false
"testString123".hasPrefixCheck(prefix: "TEST", isCaseSensitive: false) // Returns true
স্ট্রিংটি পরীক্ষা করার জন্য "পরীক্ষা" দিয়ে শুরু করুন:
"testString123".hasPrefixCheck(prefix: "test", isCaseSensitive: true) // Returns true
"testString123".hasPrefixCheck(prefix: "test", isCaseSensitive: false) // Returns true
স্ট্রিংটি "G123" দিয়ে শেষ করার জন্য পরীক্ষা করুন:
"testString123".hasSuffixCheck(suffix: "G123", isCaseSensitive: true) // Returns false
"testString123".hasSuffixCheck(suffix: "G123", isCaseSensitive: false) // Returns true
স্ট্রিংটি "g123" দিয়ে শেষ করার জন্য পরীক্ষা করুন:
"testString123".hasSuffixCheck(suffix: "g123", isCaseSensitive: true) // Returns true
"testString123".hasSuffixCheck(suffix: "g123", isCaseSensitive: false) // Returns true
চেক করার জন্য স্ট্রিংয়ে "RING12" রয়েছে:
"testString123".containsSubString(theSubString: "RING12", isCaseSensitive: true) // Returns false
"testString123".containsSubString(theSubString: "RING12", isCaseSensitive: false) // Returns true
স্ট্রিংয়ে "রিং 12" রয়েছে তা পরীক্ষা করার জন্য:
"testString123".containsSubString(theSubString: "ring12", isCaseSensitive: true) // Returns true
"testString123".containsSubString(theSubString: "ring12", isCaseSensitive: false) // Returns true