I store some files in file manager and I need three data string in their name that I can do the necessary modification, they data I need are two ids and one timestamp, something like that
"hdh3npHvjjkdfydlz-jfoabcotmdbnadp-1657155181"
I want to read each of them separately, those three data is separated by one - and the number of the characters may be not the same for different files. Can anyone help me to do that?
CodePudding user response:
If I understand you correctly, you want to extract each piece of info from the string using a hyphen as a delimiter? If so, you could use:
import UIKit
let myString = "hdh3npHvjjkdfydlz-jfoabcotmdbnadp-1657155181"
let components = myString.components(separatedBy: "-")
for c in components {
print(c)
}
Or alternatively:
let items = myString.split(separator: "-")
for i in items {
print(i)
}
Either one will separate the string into individual pieces using the hyphen as a delimiter.
CodePudding user response:
You can split a String
variable in parts by using the components
function, like this:
let example = "hdh3npHvjjkdfydlz-jfoabcotmdbnadp-1657155181"
let parts = example.components(separatedBy: "-")
for part in parts {
// Do your thing
}