Swift how to filter an array


import UIKit

let weekDays:[String] = [
    "Sunday",
    "Monday",
    "Tuesday",
    "Wednesday",
    "Thursday",
    "Friday",
    "Saturday",
    "SUNDAY",
    "MONDAY",
    "TUESDAY",
    "WEDNESDAY",
    "THURSDAY",
    "FRIDAY",
    "SATURDAY"
]

var myDays = weekDays.filter({ $0.hasSuffix("nday") })
print(myDays)
//Result = ["Sunday", "Monday"]

myDays = weekDays.filter({$0.caseInsensitiveCompare("tuesday") == .orderedSame})
print(myDays)
//Result = ["Tuesday", "TUESDAY"]

myDays = weekDays.filter({$0.lowercased().contains("sday")})
print(myDays)
//Result = ["Tuesday", "Wednesday", "Thursday", "TUESDAY", "WEDNESDAY", "THURSDAY"]


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.