0

I want to replace some characters in my string and I run into this problem.

This is a sample string: "Chan is a beautiful woman."

I want to replace character "ch" with "C" only so my string will become "Can is a beautiful ...." However, when I type "ch" the text changes to "X". I know it is because of "kh" and "c" rules in my code but I need them in there. Do you have any suggestions?

let textContent = textView.text
var replace = textContent?.replacingOccurrences(of: "Gi", with: "Z")

replace = replace?.replacingOccurrences(of: "C" , with: "K")
replace = replace?.replacingOccurrences(of: "c" , with: "k")

replace = replace?.replacingOccurrences(of: "KH" , with: "X")
replace = replace?.replacingOccurrences(of: "Kh" , with: "X")
replace = replace?.replacingOccurrences(of: "kh" , with: "x")
replace = replace?.replacingOccurrences(of: "kH" , with: "x")

replace = replace?.replacingOccurrences(of: "Ch" , with: "C")
replace = replace?.replacingOccurrences(of: "CH" , with: "C")
replace = replace?.replacingOccurrences(of: "ch" , with: "c")
replace = replace?.replacingOccurrences(of: "cH" , with: "c")
jscs
  • 63,694
  • 13
  • 151
  • 195
  • If this are the only rules you want to apply, just change order. – Retterdesdialogs Nov 29 '17 at 13:55
  • You probably want a regular expression. https://stackoverflow.com/questions/9661690/use-regular-expression-to-find-replace-substring-in-nsstring – Alex Nov 29 '17 at 13:57
  • I did moved replace = replace?.replacingOccurrences(of: "C" , with: "K") replace = replace?.replacingOccurrences(of: "c" , with: "k") below replace = replace?.replacingOccurrences(of: "ch" , with: "c") replace = replace?.replacingOccurrences(of: "cH" , with: "c") but the result comes out "K" always 1st. –  Nov 29 '17 at 13:58
  • just do all the kh,KH,Kh and so on replacements before the c,C to k,K replacements – Retterdesdialogs Nov 29 '17 at 13:59
  • I wonder is there a way to force "C" with "K" change after "CH" to "C" –  Nov 29 '17 at 14:02
  • The problem you´re facing is related to the fact that when you write a C its change to K, so i think that you need keep track of your original wrote string – Reinier Melian Nov 29 '17 at 14:04
  • Yeah. It's the problem. C changes to K and when I type H, it becomes KH instead of C –  Nov 29 '17 at 14:05

0 Answers0