Questions tagged [characterview]

A view of a string’s contents as a collection of characters.

In Swift, every string provides a view of its contents as characters. In this view, many individual characters—for example, “é”, “김”, and “”—can be made up of multiple Unicode code points. These code points are combined by Unicode’s boundary algorithms into extended grapheme clusters, represented by the Character type. Each element of a Character​View collection is a Character instance.

let flowers = "Flowers "
for c in flowers.characters {
    print(c)
}
// F
// l
// o
// w
// e
// r
// s
//
// 

You can convert a String.Character​View instance back into a string using the String type’s init(_:​) initializer.

let name = "Marie Curie"
if let firstSpace = name.characters.index(of: " ") {
    let firstName = String(name.characters.prefix(upTo: firstSpace))
    print(firstName)
}
// Prints "Marie"

https://developer.apple.com/reference/swift/string.characterview

2 questions
2
votes
2 answers

Extract numbers from a String and save them to an array

Hello my Friends i need ur help! :) Im an absolute beginner in swift and have absolutely no idea what i'm doing. I need to extract all numbers from a txt file which i converted into a string. I want to save all the numbers into a new Array and i…
Tabax
  • 43
  • 1
  • 6
1
vote
2 answers

how to write function to evaluate if String is only characters?

I've seen other answers to this question, but I'm just trying to do it differently. Yet whatever I do I can't make my types match. func ContainsOnlyAlphabets(_ word : String) -> Bool{ let letters = CharacterSet.letters // Set let…
mfaani
  • 33,269
  • 19
  • 164
  • 293