2017-05-06 3 views
1

Ich möchte in der Lage sein, Vorkommen einer Teilzeichenfolge in einer nativen Swift-Zeichenfolge zu suchen und zu ersetzen, ohne mit der NS-Klasse zu überbrücken. Wie kann ich das erreichen?Zeichenfolge ersetzen Teilzeichenfolge ohne NSString-API

Dies ist kein Duplikat von this question, da es sich bei dieser Frage um das Ersetzen eines einzelnen Zeichens handelt. In dieser Frage geht es darum, eine Teilzeichenfolge zu finden und zu ersetzen, die viele Zeichen enthalten kann.

+2

Mögliche Duplikat [Swift: Zeichen in Zeichenkette ersetzen - ohne stringByReplacingOccurrencesOfString] (http://stackoverflow.com/questions/24529612/swift-replace-character-in-string-without-stringbyreplacingoccurrencesofstrin) – Adrian

+0

wirklich? Warum alle Down-Stimmen? Dies ist kein Duplikat und basiert auf den Antworten, nicht trivial –

+1

Ich habe nicht abgestimmt, aber ich bin neugierig, den Grund für die Verwendung von Foundation zu wissen? Sicher, es ist kein reiner Swift, aber gibt es einen Grund, eine Codezeile für 20 zu tauschen? – Adrian

Antwort

2

allgemeiner und reiner Swift Ansatz

func splitBy<T: RangeReplaceableCollection>(_ s:T, by:T)->[T] where T.Iterator.Element:Equatable { 
    var tmp = T() 
    var res = [T]() 
    var i:T.IndexDistance = 0 
    let count = by.count 

    var pc:T.Iterator.Element { 
     get { 
      i %= count 
      let idx = by.index(by.startIndex, offsetBy: i) 
      return by[idx] 
     } 
    } 

    for sc in s { 
     if sc != pc { 
      i = 0 
      if sc != pc { 
      } else { 
       i = i.advanced(by: 1) 
      } 
     } else { 
      i = i.advanced(by: 1) 
     } 
     tmp.append(sc) 
     if i == count { 
      tmp.removeSubrange(tmp.index(tmp.endIndex, offsetBy: -i)..<tmp.endIndex) 
      res.append(tmp) 
      tmp.removeAll() 
     } 
    } 

    res.append(tmp) 
    return res 
} 

func split(_ s:String, by:String)->[String] { 
    return splitBy(s.characters, by: by.characters).map(String.init) 
} 


extension RangeReplaceableCollection where Self.Iterator.Element: Equatable { 
    func split(by : Self)->[Self] { 
     return splitBy(self, by: by) 
    } 
} 

wie es zu benutzen?

let str = "simple text where i would like to replace something with anything" 
let pat = "something" 
let rep = "anything" 

let s0 = str.characters.split(by: pat.characters).map(String.init) 
let res = s0.joined(separator: rep) 
print(res) // simple text where i would like to replace anything with anything 

let res2 = split(str, by: pat).joined(separator: rep) 
print(res2) // simple text where i would like to replace anything with anything 

let arr = [1,2,3,4,1,2,3,4,1,2,3] 
let p = [4,1] 
print(arr.split(by: p)) // [[1, 2, 3], [2, 3], [2, 3]] 
3

Methode ohne Foundation:

extension String { 
    func replacing(_ oldString: String, with newString: String) -> String { 

     guard !oldString.isEmpty, !newString.isEmpty else { return self } 

     let charArray = Array(self.characters) 
     let oldCharArray = Array(oldString.characters) 
     let newCharArray = Array(newString.characters) 

     var matchedChars = 0 
     var resultCharArray = [Character]() 

     for char in charArray { 
      if char == oldCharArray[matchedChars] { 
       matchedChars += 1 
       if matchedChars == oldCharArray.count { 
        resultCharArray.append(contentsOf: newCharArray) 
        matchedChars = 0 
       } 
      } else { 
       for i in 0 ..< matchedChars { 
        resultCharArray.append(oldCharArray[i]) 
       } 
       if char == oldCharArray[0] { 
        matchedChars = 1 
       } else { 
        matchedChars = 0 
        resultCharArray.append(char) 
       } 
      } 
     } 

     return String(resultCharArray) 

    } 
} 

Verwendungsbeispiel:

let myString = "Hello World HelHelloello Hello HellHellooo" 
print(myString.replacing("Hello", with: "Hi")) 

Output:

Hi World HelHiello Hi HellHioo 

Verfahren mit Foundation:

Sie können die replacingOccurrences Methode auf der String Struktur.

let myString = "Hello World" 
let newString = myString.replacingOccurrences(of: "World", with: "Everyone") 
print(newString) // prints "Hello Everyone" 
Verwandte Themen