Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Swift 3中迭代和操作字符串_Swift_String_Indexing - Fatal编程技术网

在Swift 3中迭代和操作字符串

在Swift 3中迭代和操作字符串,swift,string,indexing,Swift,String,Indexing,我试图做的应该很简单,但由于对字符串索引和字符的快速处理,我只能想出详细的解决方案。基本上,我有一个很大的指令列表,存储为字符串数组。示例值为: "1) Do this thing" "2) Do that thing" 我想从这些字符串中删除编号(例如,“1”),以便以后在将字符串添加到UITextView时创建自己的格式 我已经能够使用以下代码: func removeNumbering(from instruction: inout String) { var indexOfFi

我试图做的应该很简单,但由于对字符串索引和字符的快速处理,我只能想出详细的解决方案。基本上,我有一个很大的指令列表,存储为字符串数组。示例值为:

"1) Do this thing"
"2) Do that thing"
我想从这些字符串中删除编号(例如,
“1”
),以便以后在将字符串添加到
UITextView
时创建自己的格式

我已经能够使用以下代码:

func removeNumbering(from instruction: inout String) {
    var indexOfFirstLetter = 0

    for (index, character) in instruction.unicodeScalars.enumerated() {
        if NSCharacterSet.letters.contains(character) {
            indexOfFirstLetter = index
            break
        }
    }

    let range = instruction.startIndex ..< instruction.index(instruction.startIndex, offsetBy: indexOfFirstLetter)
    instruction.removeSubrange(range)
}
func重新编号(来自指令:inout字符串){
var indexOfFirstLetter=0
用于指令.unicodeScalars.enumerated()中的(索引、字符){
如果NSCharacterSet.letters.contains(字符){
indexOfFirstLetter=索引
打破
}
}
让range=instruction.startIndex..
有没有更简洁的方法?似乎我应该能够删除字符,直到循环找到第一个字母,但要从循环中访问
unicodeScalar
字符串.Index
却很困难


关于这个函数有什么建议或改进吗?

在不了解字符串的更多信息的情况下,假设它们都遵循相同的模式,在第一个字母前面有一个空格,这个方法是一种解决方案:

extension String {
    var strip: String {
        var copy = self
        for c in copy.characters {
            guard c == " " else { continue }
            copy.removeSubrange(copy.startIndex...copy.characters.index(of: c)!)
            break
        }
        return copy
    }
}

"1) Do something".strip    // "Do something"
"1234567890 Text".strip    // "Text"

显然,如果字符串的模式与上述假设相冲突,则此方法是不安全的。

如果不了解字符串的更多信息,假定它们都遵循相同的模式,且在第一个字母前有空格,则此方法是一种解决方案:

extension String {
    var strip: String {
        var copy = self
        for c in copy.characters {
            guard c == " " else { continue }
            copy.removeSubrange(copy.startIndex...copy.characters.index(of: c)!)
            break
        }
        return copy
    }
}

"1) Do something".strip    // "Do something"
"1234567890 Text".strip    // "Text"
显然,如果字符串的模式与上述假设相冲突,则此方法是不安全的。

这样如何:

var s1 = "1) Do this thing"
var s2 = "2) Do that thing"

s1.characters.removeFirst(3)
print(s1) // prints "Do this thing"

s2.characters.removeFirst(3)
print(s2) // prints "Do this thing"
如果需要更精细的修剪,请使用正则表达式:

s1.replacingOccurrences(of: "[0-9]\\) ", with: "", options: .regularExpression)
print(s1) // prints "Do this thing"
这个怎么样:

var s1 = "1) Do this thing"
var s2 = "2) Do that thing"

s1.characters.removeFirst(3)
print(s1) // prints "Do this thing"

s2.characters.removeFirst(3)
print(s2) // prints "Do this thing"
如果需要更精细的修剪,请使用正则表达式:

s1.replacingOccurrences(of: "[0-9]\\) ", with: "", options: .regularExpression)
print(s1) // prints "Do this thing"

使用正则表达式

    let s1 = "1) Do this thing"
    let s2 = "201) Do that thing"

    func stripNumber(_ s:String) -> String? {
        let reg = try! NSRegularExpression(pattern: "^\\d+\\) ")
        if let tm = reg.firstMatch(in: s, range:NSMakeRange(0,s.utf16.count)) {
            return (s as NSString).substring(from: tm.range.length)
        }
        return nil
    }

    print(stripNumber(s1)!) // Do this thing
    print(stripNumber(s2)!) // Do that thing

使用正则表达式

    let s1 = "1) Do this thing"
    let s2 = "201) Do that thing"

    func stripNumber(_ s:String) -> String? {
        let reg = try! NSRegularExpression(pattern: "^\\d+\\) ")
        if let tm = reg.firstMatch(in: s, range:NSMakeRange(0,s.utf16.count)) {
            return (s as NSString).substring(from: tm.range.length)
        }
        return nil
    }

    print(stripNumber(s1)!) // Do this thing
    print(stripNumber(s2)!) // Do that thing

所有这些答案都很好,但我会继续接受这个答案,因为它很简短。使用您的
replacingOccurrences()
答案,我可以将编号的指令数组映射到一个新数组。谢谢所有这些答案都很好,但我会继续接受这个答案,因为它很简短。使用您的
replacingOccurrences()
答案,我可以将编号的指令数组映射到一个新数组。谢谢