Swift find and replace a String



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.
"""

//case sensitive find and replace
let newString = myString.replacingOccurrences(of: "Description", with: "Product Information")
print(newString)
print("=====================")

//case insensitive find and replace
let newString2 = myString.replacingOccurrences(of: "description", with: "Product Information",
                                               options: .caseInsensitive)
print(newString2)
print("=====================")

//find and replace partial String using range
let startIndex = myString.index(myString.startIndex, offsetBy: 0)
let endIndex = myString.index(myString.startIndex, offsetBy: 100)
let replaceRange = startIndex..<endIndex
let newString3 = myString.replacingOccurrences(of: "description", with: "Product Information",
                                               options: .caseInsensitive, range: replaceRange)
print(newString3)
print("=====================")



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.