1

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"

luk2302
  • 55,258
  • 23
  • 97
  • 137
Wrenzoe
  • 41
  • 6
  • what will be your input string ? and what output you want ? – Hitendra Solanki Jan 22 '16 at 09:09
  • input: "hello" , "world", "c\Program Files\Example". output: "hello world c\\Program Files\\Example". – Wrenzoe Jan 22 '16 at 09:16
  • I see you edit your question with `map`, be careful `map` return a new array, not change the current one Compiler could remove this useless code if you don't keep the return array – phimage Jan 22 '16 at 13:52

4 Answers4

1

Try stringByReplacingOccurrencesOfString:

var words: String = "c\\Program Files\\Example"
words = words.stringByReplacingOccurrencesOfString("\\", withString: "\\\\")
print("Result: \(words)")
// "Result: c\\Program Files\\Example"

or if you want to do this in a array:

let inputArray: [String] = ["hello", "world", "c\\Program Files\\Example"]
inputArray.map {
    $0.stringByReplacingOccurrencesOfString("\\", withString: "\\\\")
}
print("Result: \(inputArray)")
// "Result: ["hello", "world", "c\\Program Files\\Example"]"

stringByReplacingOccurrencesOfString("\\", withString: "\\\\") line means replace \\ with \\\\ and it's exactly what you want to do. :)

Related Questions: Any way to replace characters on Swift String?

Community
  • 1
  • 1
He Yifei 何一非
  • 2,592
  • 4
  • 38
  • 69
  • 1
    Be careful with escaping characters, it can be tricky: *your answer is right*, but the printed results you show are wrong. Your code gives escaped backslashes, which is ok, but you show double-escaped (like it appears in a Playground pane) instead of simple-escaped (like it appears with `print`). Also we have to escape an escape character on SO for it to appear in text or comments. Tricky indeed. ;) – Eric Aya Jan 22 '16 at 09:49
  • @EricD. thats really tricky :| (I copy the result from playground) – He Yifei 何一非 Jan 22 '16 at 09:50
  • 1
    updated result comment to result from real `print()` – He Yifei 何一非 Jan 22 '16 at 09:57
  • no the code with map is wrong, you edit your answer and copy my response(or code in question copied from my response) without knowing how to use map..., map return a new array (here with same type so we could do `inputArray = inputArrat.map {..}` – phimage Jan 22 '16 at 17:45
  • @phimage but when I try in real project, this code do return correct result :( – He Yifei 何一非 Jan 23 '16 at 01:37
1

Use stringByReplacingOccurrencesOfString instead:

words.stringByReplacingOccurrencesOfString("\\", withString: "\\\\")
luk2302
  • 55,258
  • 23
  • 97
  • 137
  • Awesome, this worked. It took me a while to get it to run in terminal. you need to import Foundation before you can use stringByReplacingOccurrencesOfString. – Wrenzoe Jan 22 '16 at 19:03
  • @Wrenzoe argh, yes, sorry for that one :/ I always forget about the imports. – luk2302 Jan 22 '16 at 19:08
0

You can do map on your array and use stringByReplacingOccurrencesOfString

let new Array = inputArray.map { 
    $0.stringByReplacingOccurrencesOfString("\\", withString: "\\\\")
}
phimage
  • 274
  • 1
  • 6
0

You can call enumerate() on any sequence that is a SequenceType to get the index along with the current item so you can do something like: for (index, char) in words.enumerate() Although you should also avoid modifying an array while looping it. Think maybe about having another array where you add normal characters to it, and in the case of \, you add \\

Ramy Kfoury
  • 937
  • 5
  • 8