Ios 不区分大小写的拆分swift 4?
尝试在swift中拆分值,虽然有效,但区分大小写。在swift 4忽略的情况下,有没有办法将它们分开Ios 不区分大小写的拆分swift 4?,ios,swift,Ios,Swift,尝试在swift中拆分值,虽然有效,但区分大小写。在swift 4忽略的情况下,有没有办法将它们分开 "Hello There!" "hello there!" 我目前使用的是String.components(以“Th”分隔),但这只会分割第二个字符串“您好!”。有没有办法将它们分开?您可以先将整个字符串小写: let s = "Hello There!".lowercased().components(separatedBy: "th") 你可以这样写: import Foundatio
"Hello There!"
"hello there!"
我目前使用的是
String.components(以“Th”分隔)
,但这只会分割第二个字符串“您好!”
。有没有办法将它们分开?您可以先将整个字符串小写:
let s = "Hello There!".lowercased().components(separatedBy: "th")
你可以这样写:
import Foundation
let str1 = "Hello There!"
let str2 = "hello there!"
extension String {
func caseInsensitiveSplit(separator: String) -> [String] {
//Thanks for Carpsen90. Please see comments below.
if separator.isEmpty {
return [self] //generates the same output as `.components(separatedBy: "")`
}
let pattern = NSRegularExpression.escapedPattern(for: separator)
let regex = try! NSRegularExpression(pattern: pattern, options: .caseInsensitive)
let matches = regex.matches(in: self, options: [], range: NSRange(0..<self.utf16.count))
let ranges = (0..<matches.count+1).map { (i: Int)->NSRange in
let start = i == 0 ? 0 : matches[i-1].range.location + matches[i-1].range.length
let end = i == matches.count ? self.utf16.count: matches[i].range.location
return NSRange(location: start, length: end-start)
}
return ranges.map {String(self[Range($0, in: self)!])}
}
}
print( str1.caseInsensitiveSplit(separator: "th") ) //->["Hello ", "ere!"]
print( str2.caseInsensitiveSplit(separator: "th") ) //->["hello ", "ere!"]
<代码>导入基础
让str1=“你好!”
让str2=“你好!”
扩展字符串{
func caseInsensitiveSplit(分隔符:String)->[String]{
//感谢Carpsen90。请参阅下面的评论。
如果separator.isEmpty{
return[self]//生成与`.组件相同的输出(分隔符:“”)`
}
let pattern=NSRegularExpression.escapedPattern(for:separator)
让regex=try!NSRegularExpression(模式:模式,选项:。不区分大小写)
让matches=regex.matches(in:self,options:[],range:NSRange(0..[“Hello”,“ere!”)
打印(str2.caseInsensitiveSplit(分隔符:“th”)/->[“您好”,“ere!”]
但是我想知道你想用“hello”
和“ere!”
做什么。
(如果与“th”
、“th”
、“th”
、“th”
或“th”
匹配,则会丢失分隔符的大小写信息)
如果你能解释你真正想做什么,有人会给你一个更好的解决方案。不确定我是否理解你想要实现的目标,但你可以试试这个
var str = "Hello There!"
var str2 = "hello there!"
override func viewDidLoad() {
super.viewDidLoad()
let split1 = str.lowercased().replacingOccurrences(of: "th", with: " ")
let split2 = str2.lowercased().replacingOccurrences(of: "th", with: " ")
print(split1) //hello ere!
print(split2) //hello ere!
}
一种可能的解决方案是使用
选项获取分隔符的范围
,不区分大小写
并从其下限
和上限
提取子字符串
extension StringProtocol where Index == String.Index {
func caseInsensitiveComponents(separatedBy separator: String) -> [SubSequence]
{
var result = [SubSequence]()
var currentIndex = startIndex
while let separatorRange = self[currentIndex...].range(of: separator, options: .caseInsensitive) {
result.append(self[currentIndex..<separatorRange.lowerBound])
currentIndex = separatorRange.upperBound
}
return result + [self[currentIndex...]]
}
}
扩展StringProtocol,其中Index==String.Index{
func caseInsensitiveComponents(由分隔符分隔:字符串)->[子序列]
{
var结果=[子序列]()
var currentIndex=startIndex
而让separatorRange=self[currentIndex…]。范围(of:分隔符,选项:。不区分大小写){
result.append(self[currentIndex..如果要筛选包含特定关键字的项目列表,拆分字符串是错误的方法。只需简单检查该关键字是否出现在项目中:
let queryStr = "th"
let items = [
"Hello There!",
"hello there!"
]
let filterdItems = items.filter {
return $0.range(of: queryStr, options: .caseInsensitive) != nil
}
只是为了好玩,使用NSRegularExpression但扩展StringProtocol的另一个版本:
Swift 4或更高版本
extension StringProtocol {
func caseInsensitiveComponents<T>(separatedBy separator: T) -> [SubSequence] where T: StringProtocol, Index == String.Index {
var index = startIndex
return ((try? NSRegularExpression(pattern: NSRegularExpression.escapedPattern(for: String(separator)), options: .caseInsensitive))?.matches(in: String(self), range: NSRange(location: 0, length: utf16.count))
.compactMap {
guard let range = Range($0.range, in: String(self))
else { return nil }
defer { index = range.upperBound }
return self[index..<range.lowerBound]
} ?? []) + [self[index...]]
}
}
我知道,但我需要它们保持原样,而不是小写。我试图做的是创建一个搜索引擎。例如,用户键入“he”,它应该返回“herman”、“henk”、“hey”、“he”.然后我会将用户键入的部分加粗:herman,henk,嘿,he@FrankHeijden,您最好在问题中注意。不区分大小写的拆分似乎远远不是您的最佳解决方案。@在尝试之前,请检查分隔符字符计数!
如下:保护分隔符.count>0 else{return[self]}
或抛出一个错误。@OOPer并可能筛选出空的组件:return ranges.map{String(self[Range($0,in:self)!])}.filter{$0.count>0}
@leodbus,我想表示结果数组有匹配的.count+1
元素。我知道后者。你在下面说过“嗯,我想做的是创建一个搜索引擎。"这是一个典型的X/Y问题。您会问如何实现问题特定解决方案的一小部分,而不是问如何解决问题。不区分大小写的拆分有点棘手,拆分长字符串以进行搜索可能不是一个很好的方法。您应该根据实际的您试图解决的问题,而不是如何实现您选择的解决方案。克诺比将军。在Swift 4或更高版本中,最好扩展StringProtocol并将索引约束到字符串。Index@LeoDabus太棒了,我想我们现在就结束文件。
let str1 = "Hello There!"
let str2 = "hello there!"
str1.caseInsensitiveComponents(separatedBy: "Th") // ["Hello ", "ere!"]
str2.caseInsensitiveComponents(separatedBy: "Th") // ["Hello ", "ere!"]