Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

can not replace all occurrences of quotation (")

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?

like image 253
Husein Behboudi Rad Avatar asked Sep 22 '16 11:09

Husein Behboudi Rad


2 Answers

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.

like image 195
Bharat Modi Avatar answered Sep 30 '22 00:09

Bharat Modi


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

 آخرین حقوق کارگر را به‌عنوان "حق سنوات" به وی
آخرین حقوق کارگر را به‌عنوان *****حق سنوات***** به وی
like image 38
jignesh Vadadoriya Avatar answered Sep 29 '22 23:09

jignesh Vadadoriya