Husein Behboodi Rad Husein Behboodi Rad - 2 months ago 17
iOS Question

can not replace all occurrence of quatation (")

I use below code to replace all occurrence of the quatation (") with another string:

NSString *cluasesText = @"آخرین حقوق کارگر را به‌عنوان \"‌حق سنوات\" به وی ";
cluasesText = [cluasesText
stringByReplacingOccurrencesOfString:@"\"" withString:@"*****"];


but the code only replace one of the quotations and do not replace the second one.

I also checked that both of the signs is same character but I am wondering why it is not replacing the second one. Actually the result after running the code is:

آخرین حقوق کارگر را به‌عنوان "‌حق سنوات***** به وی


while we expect:

آخرین حقوق کارگر را به‌عنوان *****حق سنوات***** به وی


I am wondering what is the problem that the second one is not replacing?

Answer

Try below code to replace the characters you want,

NSString *cluasesText = @"آخرین حقوق کارگر را به‌عنوان \"‌حق سنوات\" به وی ";

NSCharacterSet *charsToBeReplace = [NSCharacterSet characterSetWithCharactersInString:@"/:.\""];

cluasesText = [[cluasesText componentsSeparatedByCharactersInSet: charsToBeReplace] componentsJoinedByString: @""];

Hope it helps you...