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

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];

Related

Convert string to UTF8 [duplicate]

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]];

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:#" "];

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 convert NSMutableArray into NSString? [duplicate]

This question already has answers here:
How to convert elements of NSMutableArray to NSString
(3 answers)
Closed 9 years ago.
I am trying to convert (or copy?) a NSMutableArray into a NSString. I guess my problem is that I don't really understand the structure of a NSString. In my limited knowledge, a string could look like this: in iphone
Try this code:-
NSString *string = [array componentsJoinedByString:#","];
take the NSMutableString and append every array string into your string
like
string = [string appendString:[NSString stringWithFormat:"%#", [array objectAtIndex:i]]];

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