-
-
Save fitomad/ff2780c7a7507650a5aeb8ab506d50bd to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
var numeroTelefono: String = "+34555_111_222" | |
// Borra el primer caracter | |
numeroTelefono.removeFirst() | |
print("1. \(numeroTelefono)") | |
// Tambien podemos indicar el número de caracteres | |
// que queremos borrar desde el inicio | |
numeroTelefono.removeFirst(2) | |
print("2. \(numeroTelefono)") | |
// Borra el último caracter | |
numeroTelefono.removeLast() | |
print("3. \(numeroTelefono)") | |
// Tambien podemos indicar el número de caracteres | |
// que queremos borrar desde el final | |
numeroTelefono.removeLast(2) | |
print("4. \(numeroTelefono)") | |
// Podemos borrar un rango de caracteres... | |
if let range = numeroTelefono.range(of: "111_") | |
{ | |
numeroTelefono.removeSubrange(range) | |
print("5. \(numeroTelefono)") | |
} | |
// ...borrar un caracter dentro del String... | |
if let index = numeroTelefono.index(of: "_") | |
{ | |
numeroTelefono.remove(at: index) | |
print("6. \(numeroTelefono)") | |
} | |
// ...o borrarlo todo | |
numeroTelefono.removeAll() | |
print("7. \(numeroTelefono)") | |
/* | |
1. 34555_111_222 | |
2. 555_111_222 | |
3. 555_111_22 | |
4. 555_111_ | |
5. 555_ | |
6. 555 | |
7. | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment