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 鈥淎ct 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")