In C + +, Swift removes the trailing spaces of the string and specifies the string program

Source: Internet
Author: User

@import url/cutesoft_client/cuteeditor/load.ashx?type=style&file=syntaxhighlighter.css);
@import URL (/css/cuteeditor.css);

Give a call to an instance directly:

@import url/cutesoft_client/cuteeditor/load.ashx?type=style&file=syntaxhighlighter.css);
@import URL (/css/cuteeditor.css);

var str = "\ r \ n \ \ r \ n \ n \ n \ playground \ t \ \ \n\r \ n"
Let length = (str as nsstring). length
Let A = Str.escapeheadtailspace ()
Let B = str.stringescapeheadtail (strs:["\ r", "\ n", "T", "\ r \ n", ""])

corresponding Result:

A::\t Hello, \ Playground \ n
B::hello, \ Playground

The principle is: For a string, skip the given string one byte at a time, and return the newly obtained string, iterative until the given string is not found in the original string. In this way, the characters contained in the middle are not removed.

Reverses the resulting new string and processes it again. So the end of the tail of the set of excluded strings can be. The result is then reversed to get the original order of the string, which is the result we need.

Simply remove all of the character sets, there are native methods, do not reconsider
This is an extension for 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 cycle
}
}
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 ()
}
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.