How to copy a string in Swift

6 Answers

0 votes
// Copy string using simple assignment

import Foundation

let src = "Programming is fun"
let dest = src

print(dest)



/*
run:

Programming is fun

*/

 



answered 1 day ago by avibootz
0 votes
// Copy string using string interpolation

import Foundation

let src = "Programming is fun"

let dest = "\(src)"

print(dest)



/*
run:

Programming is fun

*/

 



answered 1 day ago by avibootz
0 votes
// Copy string using String initializer

import Foundation

let src = "Programming is fun"

let dest = String(src)

print(dest)



/*
run:

Programming is fun

*/

 



answered 1 day ago by avibootz
0 votes
// Copy string using map and joined

import Foundation

let src = "Programming is fun"

let dest = src.map { String($0) }.joined()

print(dest)



/*
run:

Programming is fun

*/

 



answered 1 day ago by avibootz
0 votes
// Copy string using Array and String

import Foundation

let src = "Programming is fun"

let characters = Array(src)
let dest = String(characters)

print(dest)




/*
run:

Programming is fun

*/

 



answered 1 day ago by avibootz
0 votes
// Copy string using reduce

import Foundation

let src = "Programming is fun"

let dest = src.reduce("") { $0 + String($1) }

print(dest)





/*
run:

Programming is fun

*/

 



answered 1 day ago by avibootz

Related questions

7 answers 18 views
8 answers 27 views
8 answers 24 views
7 answers 19 views
11 answers 26 views
...