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

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\"";

Related

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

Convert NSString to char array [duplicate]

This question already has answers here:
Convert NSString into char array
(6 answers)
Closed 9 years ago.
const char arr[]="Hello There";
I am trying to create a character array as above.
Below, is the code which I use. Is this a correct way to create a char array.
-(void) createCharArray:(NSString*) text{
char arr[text.length];
for(int i=0;i<text.length;i++){
arr[i]=[text characterAtIndex:i];
}
}
Do we have to worry about null termination?
Do we have any other way to achieve the same?
You can use UTF8String. Eg: [myString UTF8String].
No, that is not the correct way on several levels. NSString supports unicode, which far exceeds the number of possible characters that will fit in a byte array.
You need to use an encoding like UTF8 if yo want to convert unicode to 8 bit characters.
Take a look at the method getCString:maxLength:encoding:, and try using NSUTF8StringEncoding.
That method should do what you want to do with a single call.
Use,
NSString *str = #"Hello There";
const char *c = [str cStringUsingEncoding:NSUTF8StringEncoding];

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

Changing variable from int to string [duplicate]

This question already has answers here:
How to convert int to NSString?
(4 answers)
Closed 8 years ago.
Good morning,
First of all excuse me for my english.
A partner has made an app that place an image in the ios display depending the placement id you write on a textfield. He defined the textfield and the variable as a integer, but when he finished he realized that it should be an string. Now its all coded for a int, is there an easy way to parse it to string?
See this other SO question: How can I convert an int to an NSString?
In short, with the code NSString *string = [NSString stringWithFormat:#"%d", theinteger];
int yourIntVar = ??;
NSString *intIntoString = [NSString stringWithFormat:#"%d", yourIntVar];
use this:
myTextfield.text = [NSString stringWithFormat:#"%d",myInt];
int num = 2;
NSString *yourString = [NSString stringWithFormat:#"%d",num];

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

Resources