Swift split a multiline text or a large string by linefeed


import UIKit

let myString =
"""
Product: La Noix D'Argental
Brand: FROMAGERIE BAECHLER
Description 1: While aged for six weeks in humid cellars, this cheese is washed three times with walnut liqueur from the Distillerie du Périgord in Sarlat.
Description 2: This process imparts a subtle walnut flavor and gives the edible rind its light brown color.
"""

let myList = myString.components(separatedBy: CharacterSet.newlines)
for  (index,value) in myList.enumerated() {
    print("\(index) = \(value)")
}


Result

0 = Product: La Noix D'Argental
1 = Brand: FROMAGERIE BAECHLER
2 = Description 1: While aged for six weeks in humid cellars, this cheese is washed three times with walnut liqueur from the Distillerie du Périgord in Sarlat.
3 = Description 2: This process imparts a subtle walnut flavor and gives the edible rind its light brown color.

No comments:

Post a Comment

NO JUNK, Please try to keep this clean and related to the topic at hand.
Comments are for users to ask questions, collaborate or improve on existing.