I'm trying to use inertContentsOf to loop through and find all "\" characters and insert another "\" character in front of it. The problem is I won't always know the index if the character I need to insert.
For example, I know I want to insert "\" at index 3:
myString.insertContentsOf("\\".characters, at: myString.startIndex.advancedBy(3))
How would I do this without knowing the index? I've tried the following for loops but haven't had any luck.
for words in inputArray {
for char in words {
if char == "\\" {
words.insertContentsOf("\\".characters, at: char)
}
}
}
at: char
yells at me for trying to convert a character to an index, which I get but don't know how to fix it.
Edit: For some reason when I try and put it in a function inputArray.map
doesn't get called.
func GenerateString(inputArray:[String])->String {
inputArray.map {
$0.stringByReplacingOccurrencesOfString("\\", withString: "\\\\")
}
let joiner = "|"
let joinerString = inputArray.joinWithSeparator(joiner)
return ("Result: \(joinerString)")
}
let example = ["hello", "world", "c:\Program File\Example"]
GenerateString(example)
Result:
"Hello|world|c:\Program File\Example"