删除字符串开头的非字母字符

时间:2019-12-24 11:32:14

标签: ios swift string

我要替换字符串中所有非字母数字字符(包括空格),直到第一个字母为止

"\r\n                A Simple PDF File \r\n                   This is a small demonstration .pdf file - \r\n                   just for use in the Virtual Mechanics tutorials. More text. And more \r\n                   text."

我用过

 let string = txtView.text
 let newString = string.replacingOccurrences(of: "\n", with: "")

,但是它将替换所有的字符串“ \ n”。我们如何只替换起始字符?

7 个答案:

答案 0 :(得分:1)

我能够使用正则表达式删除前导空格。这是代码:

extension String {
    func removingLeadingSpaces() -> String {
        guard let index = firstIndex(where: { !CharacterSet(charactersIn: String($0)).isSubset(of: .whitespaces) }) else {
            return self
        }
        return String(self[index...])
    }
}

答案 1 :(得分:0)

您可以使用字符串类的trimmingCharacters方法。它将删除字符串中的所有前导和尾随空格和\ n。

let str = "\r\n                A Simple PDF File \r\n                   This is a small demonstration .pdf file - \r\n                   just for use in the Virtual Mechanics tutorials. More text. And more \r\n                   text."

let result = str.trimmingCharacters(in: .whitespacesAndNewlines)
print(result)

答案 2 :(得分:0)

仅前导空格:

==

答案 3 :(得分:0)

您可以使用func中的两个extension(thx @amer):

extension String {
    func replacingFirstOccurrence(of string: String, with replacement: String) -> String {
        guard let range = self.range(of: string) else { return self }
        return replacingCharacters(in: range, with: replacement)
    }

    func removingLeadingSpaces() -> String {
        guard let index = firstIndex(where: { !CharacterSet(charactersIn: String($0)).isSubset(of: .whitespaces) }) else {
            return self
        }
        return String(self[index...])
    }
}

并使用它:

let myString = "\r\n                A Simple PDF File \r\n                   This is a small demonstration .pdf file - \r\n                   just for use in the Virtual Mechanics tutorials. More text. And more \r\n                   text."


let charactersRemoved = myString.replacingFirstOccurrence(of: "\r\n", with: "")
let whiteSpacesRemoved = charactersRemoved.removingLeadingSpaces()
print(whiteSpacesRemoved) // "A Simple PDF File \r\n                   This is a small demonstration .pdf file - \r\n                   just for use in the Virtual Mechanics tutorials. More text. And more \r\n                   text."

答案 4 :(得分:0)

[ |\t|\n|\r]匹配空格,换行符,制表符或回车符

{2,}“ 2次或更多”

每个匹配项都用换行符代替

let str = "\r\n                A Simple PDF File \r\n                   This is a small demonstration .pdf file - \r\n                   just for use in the Virtual Mechanics tutorials. More text. And more \r\n                   text."

let newString = str.replacingOccurrences(
    of: "[ |\t|\n|\r]{2,}",
    with: "\n",
    options: .regularExpression
)
print(newString)
// A Simple PDF File
// This is a small demonstration .pdf file -
// just for use in the Virtual Mechanics tutorials. More text. And more
// text.

答案 5 :(得分:0)

根据您的问题陈述,您需要删除包括spaces, line,'@','$','%','^'等的非字母字符。有很多非字母字符。无需检查非字母字符,您可以仅验证字母字符。请检查以下问题解决方案。

extension String{
    func findFirstAlphabetic() -> String.Index?{
        for index  in self.indices{
            if String(self[index]).isAlphanumeric == true{
                return index
            }
        }
        return nil
    }
    var isAlphanumeric: Bool {
        return !isEmpty && range(of: "[^a-zA-Z0-9]", options: .regularExpression) == nil
    }
    func alphabetic_Leading_SubString() -> String?{
        if let startIndex =  self.findFirstAlphabetic(){
            let newSubString = self[startIndex..<self.endIndex]
            return String(newSubString)
        }
        return nil
    }
}

用法:-

print("Output" + string.alphabetic_Leading_SubString())

输入:-

let string = "\r\n@ # $ % & ^ * () -+ #####@@@                A Simple PDF File \r\n                   This is a small demonstration .pdf file - \r\n                   just for use in the Virtual Mechanics tutorials. More text. And more \r\n                   text."

输出:-

enter image description here

答案 6 :(得分:0)

有一个drop函数,用于在满足条件时删除字符。

这里有两种使用方式

let newString = string.drop { $0.isWhitespace || $0.isPunctuation }
let newString = string.drop { !($0.isLetter || $0.isNumber) }