最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
C++中SWIFT 去除字符串首尾的空格,以及指定字符串程序
时间:2022-06-25 08:08:02 编辑:袖梨 来源:一聚教程网
@import url/CuteSoft_Client/CuteEditor/Load.ashx?type=style&file=SyntaxHighlighter.css);
@import url(/css/cuteeditor.css);
直接给一段调用实例:
@import url/CuteSoft_Client/CuteEditor/Load.ashx?type=style&file=SyntaxHighlighter.css);
@import url(/css/cuteeditor.css);
var str = " n r rn t Hello, n playground n t t n nr n "
let length = (str as NSString).length
let a = str.escapeHeadTailSpace()
let b = str.stringEscapeHeadTail(strs:["r", "n", "t", "rn", " "])
对应结果:
a::t Hello, n playground n
b::Hello, n playground
处理原理是:针对一个字符串,每次按一个字节跳过给定的字符串,并返回新得到的字符串,反复迭代,直到原字符串中已经找不到给定的字符串。这样,包含在中间的字符是不去除的。
将得到的新字符串反转,同样在处理一遍。这样头尾部的排除字符串集都可以了。然后再将结果反转,得到原来顺序的字符串,即我们需要的结果.
单纯去除其中所有的某个字符集,有原生方法,不再次考虑
这是针对String的扩展:
extension String{
func escapeSpaceTillCahractor()->String{
return self.stringEscapeHeadTail(strs:["r", " ", "n"])
}
func escapeHeadStr(str:String)->(String, Bool){
var result = self as NSString
var findAtleastOne = false
while( true ){
var range = result.rangeOfString(str)
if range.location == 0 && range.length == 1 {
result = result.substringFromIndex(range.length)
findAtleastOne = true
}else{
break
}
}
return (result as String, findAtleastOne)
}
func escapeSpaceTillCahractor(#strs:[String])->String{
var result = self
while( true ){
var findAtleastOne = false
for str in strs {
var found:Bool = false
(result, found) = result.escapeHeadStr(str)
if found {
findAtleastOne = true
break //for循环
}
}
if findAtleastOne == false {
break
}
}
return result as String
}
func reverse()->String{
var inReverse = ""
for letter in self {
println(letter)
inReverse = "(letter)" + inReverse
}
return inReverse
}
func escapeHeadTailSpace()->String{
return self.escapeSpaceTillCahractor().reverse().escapeSpaceTillCahractor().reverse()
}
func stringEscapeHeadTail(#strs:[String])->String{
return self.escapeSpaceTillCahractor(strs:strs).reverse().escapeSpaceTillCahractor(strs:strs).reverse()
}
}