Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Read different sections of a string

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading