mardi 15 janvier 2019

Random replace using Swift

I am experiencing a problem that I am not sure how to solve and I hope someone here can help me. Currently I have a string variable and later I replace the letters in the string with underscores like the following:

var str = "Hello playground"

let replace = str.replacingOccurrences(of: "\\\S", with: "_", options: .regularExpression)

print(str)

Know I would like to randomly generate 25 % of the characters in str (In this case 16 * 0,25 = 4) so it later prints something like these examples:

str = "H__l_ ___yg_____"

str = "_____ play______"

str = "__ll_ ____g____d"

Does anyone have any ideas of how to do this?

Thanks in advance.




Aucun commentaire:

Enregistrer un commentaire