How to convert part of a char array into an NSString*? - ios

char arr[] = "abcdefg";
// I know I can do this
NSString *s = [[NSString alloc] initWithCString:arr encoding:NSUTF8StringEncoding];
If I want to convert part of the arr array into an NSString*? say cde rather then the whole string abcdefg? How do I achieve that?

NSString *s = [[NSString alloc] initWithBytes:arr + 2
length:3 encoding:NSUTF8StringEncoding];

Related

How to display the emoji and special characters in UIlabel and UItextviews?

I am trying to display a string in all sorts of items such as UIlabel,UItextview,Uitextfield etc.....I am trying to do like this in a manner like this
NSData *data1 = [title dataUsingEncoding:NSUTF8StringEncoding];
NSString *goodValue = [[NSString alloc] initWithData:data1 encoding:NSNonLossyASCIIStringEncoding];
label.text=goodvalue;
this is working sometimes for me ,but some times it returns null for the string like this "Youtube\ud83d\ude27\ud83d\ude2e\ud83d\ude2f\ud83d".Can anybody guide me on this?
Emoji characters are in unicode plane 1 and thus require more than 16 bits to represent a code point. Thus two UTF8 representations or one UTF32 representation. Unicode is actually a 21-bit system and for plane 0 characters (basically everything except emoji) 16 bits is sufficient and we get by using 16 bits. Emoji need more than 16 bits.
"Youtube\ud83d\ude27\ud83d\ude2e\ud83d\ude2f\ud83d". is invalid, it is part of a utf16 unicode escaped string, the last \ud83d is 1/2 of an emoji character.
Also, inorder to create a literal string with the escape character "\" the escape character must be escaped: "\\".
NSString *emojiEscaped = #"Youtube\\ud83d\\ude27\\ud83d\\ude2e\\ud83d\\ude2f";
NSData *emojiData = [emojiEscaped dataUsingEncoding:NSUTF8StringEncoding];
NSString *emojiString = [[NSString alloc] initWithData:emojiData encoding:NSNonLossyASCIIStringEncoding];
NSLog(#"emojiString: %#", emojiString);
NSLog output:
emojiString: Youtube😧😮😯
The emoji string can also be expressed in utf32:
NSString *string = #"\U0001f627\U0001f62e\U0001f62f";
NSLog(#"string: %#", string);
NSLog output:
string1: 😧😮😯
NSString *str = #"Happy to help you \U0001F431";
NSData *data = [str dataUsingEncoding:NSNonLossyASCIIStringEncoding];
NSString *valueUnicode = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSData *dataa = [valueUnicode dataUsingEncoding:NSUTF8StringEncoding];
NSString *valueEmoj = [[NSString alloc] initWithData:dataa encoding:NSNonLossyASCIIStringEncoding];
_lbl.text = valueEmoj;

add convert UTF8 string to array

i converting Hebrew string and trying yo add it to array , this is the code:
NSMutableArray * myarray;
// #property(strong,nonatomic) NSMutableArray *stringArray;
for (NSString * item in stringArray) {
NSData * UTF8 = [item dataUsingEncoding:NSUTF8StringEncoding];
NSString * myString = [[NSString alloc] initWithData:UTF8 encoding:NSUTF8StringEncoding];
NSLog(#"myString = %#",myString); // Hebrew str log fine
[myarray addObject:myString];
}
NSString * ConvetData = [NSString stringWithFormat:#"%#",myarray];
NSLog(#"ConvetData = %#",ConvetData); // null
newitem.ingredient = ConvetData;
the array coms out as null , how can i crate one string from my convert data?
You need to initialize myarray: NSMutableArray *myarray =[[NSMutableArray alloc] init];

NSString compare returning NSOrderedDescending instead of NSOrderedSame

I am receiving the data in an iPad application from a socket connected.
I am converting the data received to NSString using the method below:
NSString *data = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding].
Then I am creating a substring from the string using the
NSString *substring1 = [data substringFromIndex:length-9]
NSString *substring2 = [data substringFromIndex:length-3]
where length is [data length].
Then I am comparing the substring2 with #"/>" string as below
[substring2 compare:#"/>"]
Here I checked the value of the substring2 while debugging the application the value is #"/>"
but the comparison result is returned as NSOrderedDescending instead of NSOrderedSame.
Can anyone please help?
Your string is having trailing space. The string which you are extracting as length - 3, it must be of length 3.
Now you are comparing it with #"/>" which is having length 2.
You need to do it be below way:
NSString *data = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding].
data = [data stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
Now take the substring and compare.

EMoji Unicode for IOS

I call api and get the string which include the android java unicode in NSDictionary :
\ud83d\ude25\ud83d\ude1e\U2764\ud83d\ude2d
When get the the value above and assign to string, I just will get the string as:
\ud83d\ude25\ud83d\ude1e❤\ud83d\ude2d
I totally can not get the value \U2764 but get the value ❤
So I can not replace the code \U2764 to IOS unicode.
How can I get it \U2764? I convert into UTF-8.
Thanks
Edit:
This is get the directory from api (dic):
{
message = "\ud83d\ude25\ud83d\ude1e\U2764\ud83d\ude2d";
mesgid = "213133";
Thumbnail = "";
}
Then i get the message, but the dic2 string is still get \ud83d\ude25\ud83d\ude1e❤\ud83d\ude2d:
const char *cString = [[dic6 objectForKey:#"Message"] UTF8String];
NSString *dic2 = [[NSString alloc] initWithUTF8String:cString];
if ([dic2 rangeOfString:#"\\U"].location==NSNotFound && [dic2 rangeOfString:#"\\u"].location==NSNotFound)
{
NSLog(#"Substring Not Found");
}
else
{
NSLog(#"Substring Found");
}
I totally can not get the unicode.
Why?
try this
NSString *uniString = [NSString stringWithUTF8String:#"\ud83d\ude25\ud83d\ude1e❤\ud83d\ude2d
"];
Hopefully this will resolve your problem
char cString[] = "\ud83d\ude25\ud83d\ude1e\U2764\ud83d\ude2d";
NSData *data = [NSData dataWithBytes:cString length:strlen(cString)];
NSString *string = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];

How convert string utf-8?

i've an NSString like this:
NSString *word = #"119,111,114,100"
So, what i want to do is to convert this NSString to word
So the question is, in which way can i convert a string to a word?
// I have added some values to your sample input :-)
NSString *word = #"119,111,114,100,32,240,159,145,141";
// Separate components into array:
NSArray *array = [word componentsSeparatedByString:#","];
// Create NSData containing the bytes:
NSMutableData *data = [[NSMutableData alloc] initWithLength:[array count]];
uint8_t *bytes = [data mutableBytes];
for (NSUInteger i = 0; i < [array count]; i++) {
bytes[i] = [array[i] intValue];
}
// Convert to NSString (interpreting the bytes as UTF-8):
NSString *str = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(#"%#", str);
Output:
word 👍
Try this:
NSString *word = #"119,111,114,100";
NSArray *array=[word componentsSeparatedByString:#","];
for (NSString *string in array) {
char character=[string integerValue];
NSLog(#"%c",character);
}
Output:
w
o
r
d
libicu it's an UTF8 library that supports a conversion from an array of bytes as stated here.
The thing is, it offers Java, C or C++ APIs, not obj-c.

Resources