Raw strings like Python in Objective-C

Are there Objective-C raw strings like Python ?

Explanation: An unprocessed string does not interpret escape sequences such as \n : both slash and "n" are separate characters in the string. From the related Python manual:

 >>> print 'C:\some\name' # here \n means newline! C:\some ame >>> print r'C:\some\name' # note the r before the quote C:\some\name 
+4
source share
5 answers

From your link, explaining what you mean by β€œraw string”, the answer is this: there is no built-in method for what you are asking.

However, you can replace the occurrences of one line with another line, so you can replace @"\n" with @"\\n" , for example. This should help you get closer to what you are looking for.

+3
source

Objective-C is a superset of C. So the answer is yes. You can write

 char* string="hello world"; 

anywhere. Then you can turn it into NSString later

 NSString* nsstring=[NSString stringWithUTF8String:string]; 
+4
source

Objective-C is a strict superset of C, so you can freely use char * and char[] wherever you want (if that's what you call raw strings).

+2
source

As everyone says, ANSI raw strings are very simple. Just use simple C or C ++ std :: string strings if you like to compile Objective C ++.

However, Cocoa's own string format is UCS-2 β€” 2-byte fixed-width characters. NSStrings are stored internally as UCS-2, i.e. e. as arrays of an unsigned short circuit. (As in Win32 and Java, by the way.) The system aliases for this data type are unichar and UniChar. Here where things get tricky.

GCC includes the wchar_t data type and allows you to define a string constant with wide char as follows:

wchar_t * ws = L "This is a wide char string.";

However, by default, this data type is defined as a 4-byte int and therefore does not match Cocoa unichar! You can override this by specifying the following compiler option:

-fshort-wchar

but then you lose the RTL functions with wide char C (wcslen (), wcscpy (), etc.) - RTL was compiled without this option and assumes a 4-byte wchar_t. This is not particularly difficult to implement manually. Your call.

When you have really 2-byte wchar_t raw strings, you can trivially convert them to NSStrings and vice versa:

 wchar_t *ws = L"Hello"; NSString *s = [NSString stringWithCharacters:(const unichar*)ws length:5]; 

Unlike all other [stringWi thanksXX] methods, this is not related to code page conversions.

+2
source

If you mean C-style strings, then yes.

+1
source

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


All Articles