I use below code to replace all occurrences of the quotation (") 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?
Try below code to replace the characters you want,
NSString *cluasesText = @"آخرین حقوق کارگر را بهعنوان \"حق سنوات\" به وی ";
NSCharacterSet *charsToBeReplace = [NSCharacterSet characterSetWithCharactersInString:@"/:.\""];
cluasesText = [[cluasesText componentsSeparatedByCharactersInSet: charsToBeReplace] componentsJoinedByString: @""];
Hope it helps you...
Edit and Update:
As asked by @Husein Behboodi Rad, let me explain what was going wrong,
The first occurrence of escape char i.e \" in your Arabic text has different unicode number \u005c\u0022 than the second \" one \u005c\u0022\u200c, If you copy and paste the first escape character i.e \" and then the second escape char i.e \" in char to unicode converter it will show you different unicode as they both were different.
So in your first line of string replacement,
cluasesText = [cluasesText
stringByReplacingOccurrencesOfString:@"\"" withString:@"*****"];
you have entered the normal English language double quote as the first occurrence of \" matches and the stringByReplacingOccurrencesOfString method replace just the string which has matched the exact string (with same Unicode). The second double quote could be a Left or Right double quotation mark,
So if you have to give it a try, just add below code,
NSString *cluasesText = @"آخرین حقوق کارگر را بهعنوان \"حق سنوات\" به وی ";
cluasesText = [cluasesText
stringByReplacingOccurrencesOfString:@"\"" withString:@"*****"];
cluasesText = [cluasesText
stringByReplacingOccurrencesOfString:@"\"" withString:@"*****"];
and you will get the output as
آخرین حقوق کارگر را بهعنوان *****حق سنوات***** به وی
So now how come characterSetWithCharactersInString was working is the question, So i'm not an expert of character set but IMHO, characterSetWithCharactersInString method replaces occurrence string without considering the unicode of the strings which are partially same whereas the stringByReplacingOccurrencesOfString withString replaces string with the exact unicode matching string.
This is what i could explain you for now.
There is one space between \"حق سنوات"\ last one , just remove the space then try same code it's work.
var string : NSString = "آخرین حقوق کارگر را بهعنوان \"حق سنوات\" به وی"
print(string)
string = string.stringByReplacingOccurrencesOfString("\"", withString: "*****")
print(string)
out put are
آخرین حقوق کارگر را بهعنوان "حق سنوات" به وی
آخرین حقوق کارگر را بهعنوان *****حق سنوات***** به وی
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With