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

Ios 删除字符串开头的非字母字符,ios,swift,string,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

我想替换字符串中第一个字母之前的所有非字母数字字符(包括空格)

"\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”。如何仅替换起始字符?

您可以使用字符串类的
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)
仅前导空格:

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

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

extension String {
    func trimLeadingWhitespaces() -> String {
        return self.replacingOccurrences(of: "^\\s+", with: "", options: .regularExpression)
    }
}

let string = "       Some string     abc"
let trimmed = string.trimLeadingWhitespaces()
print(trimmed)


您可以从
extension
(thx@amer)使用两个
func

并使用它:

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."

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

{2,}
“2次或更多”

每个匹配项都替换为换行符

let str=“\r\n一个简单的PDF文件\r\n这是一个小型演示.PDF文件-\r\n仅用于虚拟力学教程。更多文本。更多\r\n文本。”
让newString=str.replacingOccurrences(
其中:“[|\t |\n |\r]{2,}”,
加上“\n”,
选项:。正则表达式
)
印刷品(新闻字符串)
//一个简单的PDF文件
//这是一个小的演示.pdf文件-
//仅供在虚拟力学教程中使用。更多文本。还有更多
//文本。

根据您的问题陈述,您需要删除非字母字符,其中包括
空格、行“@”、“$”、“%”、“^”
等。有很多非字母字符。您可以单独验证字母字符,而不是检查非字母字符。请检查以下问题的解决方案

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
    }
}
输入:-

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."
输出:-


有一个函数
drop
,在填充条件时删除字符

下面是使用它的两种变体

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

我不需要删除尾随空格、中间空格或新行来删除前导spaces@AwaisMobeen添加您期望的输出。您可以通过引用形成您自己的正则表达式:从字面上理解问题正则表达式相当于
“^[^[:alnum:][+”
@Awais Mobeen
请检查此项以解决您的问题您可以使用尾随闭包语法
删除{…}
注意,isLetter不包含数字您可以使用字符属性
isWhitespace
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."
let newString = string.drop { $0.isWhitespace || $0.isPunctuation }
let newString = string.drop { !($0.isLetter || $0.isNumber) }