Skip to content

Instantly share code, notes, and snippets.

@fitomad
Created April 19, 2018 07:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fitomad/239bfb214c2488ba372befef7b5278ec to your computer and use it in GitHub Desktop.
Save fitomad/239bfb214c2488ba372befef7b5278ec to your computer and use it in GitHub Desktop.
var texto: String = " de cuyo nombre no quiero acordarme "
let antes: String = "En un lugar de Mancha"
let despues: String = "no ha mucho tiempo que vivía"
// Insertamos al principio
texto.insert(contentsOf: antes, at: texto.startIndex)
print("1. \(texto)")
// Insertamos al final
texto.insert(contentsOf: despues, at: texto.endIndex)
print("2. \(texto)")
// Insertamos a mitad del String.
// Tenemos que corregir el texto para que en lugar de
// *..lugar de Mancha...* sea lea *..un lugar de la Mancha...*
if let index: String.Index = texto.index(of: "M")
{
texto.insert(contentsOf: "la ", at: index)
print("3. \(texto)")
}
/*
1. En un lugar de Mancha de cuyo nombre no quiero acordarme
2. En un lugar de Mancha de cuyo nombre no quiero acordarme no ha mucho tiempo que vivía
3. En un lugar de la Mancha de cuyo nombre no quiero acordarme no ha mucho tiempo que vivía
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment