Convert string to UTF8 [duplicate] - ios

This question already has answers here:
ios UTF8 encoding from nsstring
(5 answers)
Closed 6 years ago.
How to convert NSString to UTF8Encode ?
My NSLog print the same result as input string
NSString* username =#"MYUSERNAME";
username=[username stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSString* Password =#"MYUSERNAMEPASS";
Password=[Password stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSLog(#"username=%#",username);
NSLog(#"Password=%#",Password);

You need to use stringByAddingPercentEscapesUsingEncoding instead of stringByReplacingPercentEscapesUsingEncoding.
NSString *encodeUserName = [username stringByAddingPercentEscapesUsingEncoding:NSUTF32StringEncoding];
Also stringByAddingPercentEscapesUsingEncoding is deprecated so batter to use stringByAddingPercentEncodingWithAllowedCharacters like this
NSString *encodeUserName = [username stringByAddingPercentEncodingWithAllowedCharacters: [NSCharacterSet URLQueryAllowedCharacterSet]];

Related

Convert NSString to NSUTF32StringEncoding [duplicate]

This question already has answers here:
NSString to treat "regular english alphabets" and characters like emoji or japanese uniformly
(2 answers)
Closed 8 years ago.
I have an NSString that internally uses UTF16 encoding. I want to covert it to use UTF32 , so that
😄 or q both take single index. Currenty 😄 takes 2.
How to do this ?. Even if I can convert to some other type from NSString it will work. Bottom line is to have 😄 or q take equal number of indexes in an array.
Did you try :
NSData *data = [string dataUsingEncoding:NSUTF32StringEncoding];
NSString *convertedString = [[NSString alloc] initWithData:data encoding:NSUTF32StringEncoding];
Have you tried [[NSString alloc]initWithData:encoding:]?
Example would be:
NSData *data = [sourceStr dataUsingEncoding:NSUTF16StringEncoding];
Create your NSUTF32StringEncoded string from the data above
NSString *encodedStr = [[NSString alloc]initWithData:data encoding:NSUTF32StringEncoding];

How to remove characters from an NSString and replace with spaces? [duplicate]

This question already has answers here:
String replacement in Objective-C
(6 answers)
Closed 8 years ago.
I have a NSString:
NSString *string1 = #"http://example.com/this-is-an-example-post"
By removing a range of characteres I have substringed it to:
NSString *string2 = #"this-is-an-example-post"
But I am struggling to get it into the form of:
NSString *string3 = #"this is an example post"
Any help? Much appreciated.
For example this way
string3 = [string2 stringByReplacingOccurrencesOfString:#"-" withString:#" "];

How to convert NSString to NSMutableString in Objective-C? [duplicate]

This question already has answers here:
How to initialize NSString to NSMutableString?
(4 answers)
Closed 8 years ago.
Is there any way to convert an NSString to NSMutableString in Objective-C?
NSString *string = #"A string.";
NSMutableString *mutableString = [string mutableCopy];

NSString to NSData conversion [duplicate]

This question already has an answer here:
Converting hex string to hex data
(1 answer)
Closed 9 years ago.
I am having following NSString:
8270be11a217f1420863d76ea7d24820
and i want to convert in following format:
NSData : <8270be11 a217f142 0863d76e a7d24820>
Please help!
Thanks
Try to do this..
NSString* str = #"teststring";
NSData* data = [str dataUsingEncoding:NSUTF8StringEncoding];

How to initialize NSString as text with double quotes [duplicate]

This question already has answers here:
Is it possible to include a quotation mark as part of an nsstring?
(7 answers)
Closed 8 years ago.
i want to initialize NSString is as "hai" with double qoutes..is it possible? any help please?
but NSString *str = #"hai"; i want to convert it as "hai" without direct initialization?any help pls?
If you want to convert an existing string please use this:
NSString *newString = [NSString stringWithFormat:#"\"%#\"",str];
NSString* foo = #"\"hai\"";

Resources