No tienes acceso a esta clase

¡Continúa aprendiendo! Únete y comienza a potenciar tu carrera

Prefijos y Sufijos

13/33
Recursos

Aportes 8

Preguntas 0

Ordenar por:

Los aportes, preguntas y respuestas son vitales para aprender en comunidad. Regístrate o inicia sesión para participar.

.hasPrefix() 👉🏽Si empieza con un cadena

.hasSuffix() 👉🏽si finaliza con una cadena

let collection = [
    "\"Swift\", One language to rule them all", "\"Swift\", One language to rule them all", "\"Swift\", One language to rule them all", "\"Swift\", One language to rule them all", "\"Swift\", One language to rule them all", "\"Swift\", One language to rule them all", "\"Swift\", One language to rule them all", "\"Swift\", One language to rule them all", "\"Swift\", One language to rule them all", "\"Swift\", One language to rule them all",
]
var rings = 0
for ring in collection{
        if ring.hasPrefix("\"Swift\""){
        rings += 1
    }
}
print("We created \(rings) rings")```
let avion = ["Fila 1 Columna 1", "Fila 1 Columna 2", "Fila 1 Columna 3", "Fila 1 Columna 4", "Fila 1 Columna 5",
             "Fila 2 Columna 1", "Fila 2 Columna 2", "Fila 2 Columna 3", "Fila 2 Columna 4",
             "Fila 3 Columna 1", "Fila 3 Columna 2", "Fila 3 Columna 3"]
var cantidad = 0
for indice in avion {
    if indice.hasPrefix("Fila 2"){
        cantidad += 1
    }
}
print("El avion tiene \(cantidad) Columnas en la Fila 2")

😃

Interesante esto del prefijo y sufijo, son funciones que seguramente en un futuro serviran de algo.

Una variante podría ser

var ac1SceneCount = 0
for scene in collection {
//    if scene.hasPrefix("Act 1") {
//        ac1SceneCount += 1
//    }
    ac1SceneCount += scene.hasPrefix("Act 1") ? 1: 0
}

print("Number of scenes for act 1 are: \(ac1SceneCount)")
let greeting = "Hello world"
greeting.hasPrefix("Hello")
greeting.hasSuffix("world")
greeting.hasSuffix("english")