The iOS_Swift- intercept string specifies the string between the same characters / different characters (three optimizations for the one time method)

Throw question:

Let fileStr = < Folder_A> < Folder_B>: File_A:: File_B:< Folder_C>: File_C"

“&lt”; “&gt”; the middle string represents the folder name; “:” the middle string represents the file name. When browsing the U disk file, it will parse such a string, extract the folder name and file name, and the intermediate folder and file are interspersed, requiring order can not be confused. A total of three optimization methods, and the last time should be thinking and code, the amount is the most concise, here to do a comparative analysis and share.

The first realization idea:

1. find "<" and ">" and ":" in the string position, is stored in three array 2. traversing arrays "<" and ">" get their position in the string, a cut out the name of the folder traversal 3. ":" the same name, defects get around file: Although this can get all the files and folders in the name, but later found that the results will upset the order, when the files and folders with appear, the output result will be in front of all folders, behind all files. So I thought of second thoughts:

Second times the realization of ideas:

1. find "<" and ">" and ":" in the string position, were saved out of three arrays in 2. position in the array of corresponding, save each file or folder into a position from beginning to end in the array, sort the array, then the string. Will solve the problem in the train of thought. Flaw: this method is undoubtedly more complex and has an array of sorting

So it’s better to intercept it than this

Third times the realization of ideas:

Meet "<" began interception, encounter ">" stops ", for" as compared to the previous two ideas, this method only needs to traverse a string, the first two methods need to traverse several times, greatly improving the computational efficiency. Attach the ideas three code: override func (viewDidLoad) super.viewDidLoad (let) {fileStr = < Folder_A> < Folder_B> File_A:: File_B:< Folder_C> File_C:: "if let arr = decomposeStr (originalStr: fileStr) {print (arr}else) {print (" analytic fruit ")}} x third thinking method to implement func decomposeStr (originalStr: String) -> [String] {if originalStr.contains?! (" < & "); & originalStr.contains! (": ") {return nil} var FolderStr String =" var ": fileStr: String =" var begainFile = false var begainFolder = false var results: [String] = Array (for) C in original Str.characters file if {/ / C = = "begainFile = {begainFile} if {if begainFile! C! =" {fileStr.append} {(c)}else if fileStr.characters.count > results.append (fileStr) {0}} = "fileStr" folder if / / C = = "& lt;" || = = "C > begainFolder =" {begainFolder} if {if begainFolder! C! = "<" (C}else) {FolderStr.append} if {FolderStr.characters.count {> 0; Results.append (FolderStr)}}} = "FolderStr" / / if there is a true represents the "<" less corresponding ">", "less contrast": "if begainFile begainFolder || {print (" invalid file "return nil if results.count >}) 0; results}else {return} nil {return}