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];
Related
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];
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];
First off, I'm pretty new to coding so bear with me if this is a dumb question.
I don't know how to convert soemthing like int array[10] into a single NSString.
I've been searching for solutions, but so far, all i've seen is converting from an array to an string array.
First thing I tried was something like
NSString *String=[NSString stringWithFormat #"%i",array];
But the thing is, i know that I can do something like
NSString *String[NSString stringWithFormat #"%i%i%i...",array[0],array[1],array[2],.....];
But i'm trying to avoid that since I will be doing this for more than 10 variables and I will be doing it quite a bit with seperately named variables
int i[] = {1,2,3,4,5,6,7};
int len = sizeof(i) / sizeof(int);
NSMutableString * str = [NSMutableString string];
for (int j = 0; j<len; j++) {
[str appendFormat:#"%i ", i[j]];
}
NSLog(#"%#",str);
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]]];
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\"";