StringByReplacingOccurrencesOfString does not give me the desired result

I have an NSString like:

test = @"this is %25test%25 string";

I am trying to replace the test with some kind of Arabic text, but it does not replace it exactly like this:

[test stringByReplacingOccurrencesOfString:@"test" withString:@"اختبار"];

and the result:

this is %25 اختبار %25 string

In some places where I read, there may be problems with the encoding or alignment of the text. There you need to perform additional configuration for operations with Arabic strings.

EDIT : I used the NSMutable row insert property, but still the same result.

+4
source share
2 answers

EDIT 2:

, , . . , % 25. . - . , ( stringByAddingPercentEscapesUsingEncoding:). " ". " % اختبار%" ( ), (, ?). , .


EDIT:

, ( ) URL-. URL- , . , , . "اختبار" URL-. (اختبار , IDN).

URL this is a %25<arabic>%25 string:

this%20is%20a%20%2525%D8%A7%D8%AE%D8%AA%D8%A8%D8%A7%D8%B1%2525%20string

, :

this is a %25اختبار%25 string

, ( , ). (, ):

this   - this    (ALPHA)
%20    - <space> (encoded)
is     - is      (ALPHA)
%20    - <space> (encoded)
a      - a       (ALPHA)
%20    - <space> (encoded)
%25    - %       (encoded)
25     - 25      (DIGIT)
%D8%A7 - ا       (encoded)
%D8%AE - خ       (encoded)
%D8%AA - ت       (encoded)
%D8%A8 - ب       (encoded)
%D8%A7 - ا       (encoded)
%D8%B1 - ر       (encoded)
%25    - %       (encoded) 
25     - 25      (DIGIT)
%20    - <space> (encoded)
string - string  (ALPHA)

Unicode BIDI , ; , . , . (, LRO), URL-, - .

, , , , URL-, ? URL-, URL-, ( , , , ). , . , BIDI.

, ( , URL-, ). , . , , URL-, UIPasteboard copy:, . , . ( , Safari "stackoverflow.com" , , "/fooobar.com/..." .


, .

, stringByReplacingOccuranceOfString: . , . characterAtIndex:, , :

% 2 5 ا ...

, " ". , , "% 25" . , .

. -, Unicode 6.3 tr9-29 . , . , . Unicode 6.3 iOS OS X, , ( ) "".

FSI (FIRST STRONG ISOLATE U + 2068) PDI (POP DIRECTIONAL ISOLATE U + 2069). RLI (RIGHT-TO-LEFT ISOLATE), . FSI " , ".

, :

NSString *test = @"this is a %25\u2068test\u2069%25 string";
NSString *arabic = @"اختبار";
NSString *result = [test stringByReplacingOccurrencesOfString:@"test" withString:arabic];

, , ( , FSI PDI). , :

NSString * const FSI = @"\u2068";
NSString * const PDI = @"\u2069";

NSString *test = @"this is %25test%25 string";
NSString *arabic = @"اختبار";
NSString *replaceString = [@[FSI, arabic, PDI] componentsJoinedByString:@""];
NSString *result = [test stringByReplacingOccurrencesOfString:@"test" withString:replaceString];

, " " . UILabel, , , -, Core Text. NSLog "placeholder":

enter image description here

. UIWebView, .

, . . . LRM RLM . , LRM ( ), . , , , ( ).

NSString * const LRM = @"\u200e";
NSString *test = @"this is a %25test%25 string";
NSString *replaceString = [@[LRM, arabic, LRM] componentsJoinedByString:@""];
NSString *result = [test stringByReplacingOccurrencesOfString:@"test" withString:replaceString];

BTW, . , . .

- . " , ", "" Unicode. , . , (<RLO>elgoog<PDF>.com google.com ). .

LRO/PDF, . RLO/PDF, -. , , :

NSString * const LRO = @"\u202d";
NSString * const RLO = @"\u202e";
NSString * const PDF = @"\u202c";

NSString *test = [@[LRO, @"this is a %25test%25 string", PDF] componentsJoinedByString:@""];
NSString *arabic = @"اختبار";
NSString *replaceString = [@[RLO, arabic, PDF] componentsJoinedByString:@""];
NSString *result = [test stringByReplacingOccurrencesOfString:@"test" withString:replaceString];

, Explicit Directional Embedding, (, RLE RLO , LRO).

, , . . TR9 . , . Cal Henderson excellent (BIDI) .

+8

:

NSString *test = @"this is %25test%25 string";

NSString *test2 = [[[test stringByReplacingPercentEscapesUsingEncoding:NSStringEncodingConversionAllowLossy] componentsSeparatedByString:@"test"] componentsJoinedByString:@"اختبار"];
0

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


All Articles