Cannot replace all occurrences of quotes (")

I use the code below to replace all occurrences of a quote (") with another line:

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

but the code replaces only one of the quotes and does not replace the second.

I also checked that both signs are the same, but I wonder why it does not replace the second. The actual result after running the code:

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

while we expect:

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

I wonder what the problem is that the second one does not replace?

+4
source share
3 answers

Try entering the code to replace the desired characters,

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

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

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

Hope this helps you ...

Change and update:

As @Husein Behboodi Rad asked, let me explain what went wrong,

escape char ie\ " \u005c\u0022, \" \u005c\u0022\u200c, escape-, .. \ ", char ie \" char unicode, , .

, ,

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

, stringByReplacingOccurrencesOfString , ( Unicode). ,

, , ,

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

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

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

, characterSetWithCharactersInString, , IMHO, characterSetWithCharactersInString unicode , stringByReplacingOccurrencesOfString withString .

, .

+6

\ "حق سنوات" \last, , , .

var string : NSString = "آخرین حقوق کارگر را به‌عنوان \"حق سنوات\" به وی"
    print(string)
    string = string.stringByReplacingOccurrencesOfString("\"", withString: "*****")
    print(string)

out put

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

, حق سنوات.

:

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

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

Source: https://habr.com/ru/post/1655467/


All Articles