I have a string as "12:66 PM".Now i want to replace string 66 with 60.So i want to check if that after ":" two characters should be replaced with 60 or any value.
please suggest
You can use regexp-search-and-replace:
string.replacingOccurrences(of: "regexp", with: "replacement", options: .regularExpression)
So, in your case:
var time = "12:66 PM"
time.replacingOccurrences(of: ":\\d\\d", with: ":60", options: .regularExpression)
// > "12:60 PM"
Abstracting this into a function is straight-forward:
func replaceMinutes(in time: String, with minutes: String) -> String {
return time.replacingOccurrences(of: ":\\d\\d",
with: ":\(minutes)",
options: .regularExpression)
}
If necessary, you may want to check that the input strings match what you expect:
nil != time.range(of: "^\\d\\d:\\d\\d [AP]M$", options: .regularExpression)
&& nil != minutes.range(of: "^\\d\\d$", options: .regularExpression)
If you have trouble understanding how any of this works, I recommend you read up on a) basics of the Swift language and b) regular expressions.
:, or are you trying to change a date / time value? If it's the latter, there are better ways to handle this