Husein Behboudi Rad
Husein Behboudi Rad

Reputation: 5462

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?

Upvotes: 5

Views: 148

Answers (3)

Bharat Modi
Bharat Modi

Reputation: 4178

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.

Upvotes: 6

Ronak Chaniyara
Ronak Chaniyara

Reputation: 5435

Try below string in which removed space after حق سنوات.

Copy below code and Check:

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

cluasesText = [cluasesText
                   stringByReplacingOccurrencesOfString:@"\"" withString:@"*****"];

Upvotes: 0

jignesh Vadadoriya
jignesh Vadadoriya

Reputation: 3310

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

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

Upvotes: 2

Related Questions