No tienes acceso a esta clase

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

Prefijos y Sufijos

13/33
Recursos

Aportes 10

Preguntas 0

Ordenar por:

¿Quieres ver más aportes, preguntas y respuestas de la comunidad?

o inicia sesión.

.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")```

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

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")

😃

He hecho el ejemplo del acto recortando de un elemento de la colección el string “Act 1”… practicada, probablemente 0 pero sirvió para reforzar

let act1 = collectionScenes[0]
let rangeAct = act1.index(act1.endIndex, offsetBy: -8)..<act1.endIndex
var act1String = String(act1)
act1String.removeSubrange(rangeAct)

print("\nThe number of scenes of \(act1String) is: \(act1SceneCount)")
<code> 
let family = ["Giovanny Jukopila","Yango Jukopila", "Nubia Rueda", "John Jukopila"]
var hasSurname = 0
for surname in family{
    if surname.hasSuffix("Jukopila"){
        hasSurname += 1
    }
}
print ("somos \(hasSurname) Jukopilas en la familia")

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")