\n for NSString not work for string from NSData - ios

I retrieve NSData from a server containing \n. When I convert it into an NSString, \n is not working. It prints "\n".
My code is like this:
NSString *st=[NSString stringWithFormat:#"%#",data];
Thanks

Try with this:
NSString * string = [[NSString alloc] initWithData:yourData encoding:NSUTF8StringEncoding];

That may help you.
myString = [myString stringByReplacingOccurrencesOfString:#"\n" withString:#" "];

Related

How to show special character in UILabel iOS

I am trying to implement an app where I would like to show some text in Spanish format. For example I would like to show "España" but in my label it shows "Espa√ɬ±a" and also it changes the text for some of other text.
How to get rid of these. If anybody could help. Thanks.
Edit: When i am getting my response it logs that Below result
Message = (
"Espa\U221a\U00c9\U00ac\U00b1a:1.3\U221a\U00c7\U00ac\U00a2/min"
);
But when i extract the value according to key from Dictionary it shows
España:1.3¢/min
It means when i am getting the value from dictionary it cant do proper decoding.
how to resolve this. Any idea..?
First convert your response String to NSData using NSUTF8StringEncoding encoding, then again convert the same data to finalString like below.
NSString *string = #"España"; //Your response String goes here
NSData *data = [string dataUsingEncoding:NSUTF8StringEncoding];
NSString *finalString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
lblTemp.text = finalString;
UPDATE 1
I think there is some error from your response, Please see below
NSString *string = #"Nu\\u0161a Florjan\\u010di\\u010d";
NSString *finalString = [NSString
stringWithCString:[string cStringUsingEncoding:NSUTF8StringEncoding]
encoding:NSNonLossyASCIIStringEncoding];
NSLog(#"finalString = %#", finalString);
Output of above code is,
finalString = Nuša Florjančič
UPDATE 2
If you want output string like "España", your desired response should be "Espa\u00F1a", Find below,
NSString *string = #"Espa\\u00F1a";
NSString *finalString = [NSString
stringWithCString:[string cStringUsingEncoding:NSUTF8StringEncoding]
encoding:NSNonLossyASCIIStringEncoding];
NSLog(#"%#",finalString);
Output is España

want to have this string "مرحبا 😀 Hello" save it to database and display back to uilabel

I am looking for solution where i want to store English + Arabic + Emoji Character to store to Database and retrieve it back while display.
Below is the code what i have used to support Emoji, after that Arabic text is not showing.
+(NSString *)emojiToSave:(NSString *)str
{
NSData *dataForEmoji = [str dataUsingEncoding:NSNonLossyASCIIStringEncoding];
NSString *encodevalue = [[NSString alloc]initWithData:dataForEmoji encoding:NSUTF8StringEncoding];
return encodevalue;
}
+(NSString *)emojiToDisplay:(NSString *)str
{
NSData *msgData = [str dataUsingEncoding:NSUTF8StringEncoding];
NSString *goodMsg = [[NSString alloc] initWithData:msgData encoding:NSNonLossyASCIIStringEncoding];
return goodMsg;
}
Can anyone pls suggest to give support for Arabic what change i should do?
Thanks in advance.
Try convert it into base64 code, then insert base64 code to database:
//Original string to base64 string
NSString *emojiString = #"مرحبا 😀 Hello";
NSData *emojiData = [emojiString dataUsingEncoding:NSUTF8StringEncoding];
NSString *base64String = [emojiData base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];
//Base64 string to original string
NSData *base64Data = [[NSData alloc] initWithBase64EncodedString:base64String options:NSDataBase64DecodingIgnoreUnknownCharacters];
NSString *originalString =[[NSString alloc] initWithData:base64Data encoding:NSUTF8StringEncoding];
NSLog(#"Result: %#",originalString);
Output:
You have to use an encoding that supports emoji and arabic characters. ASCII doesn't support that.
You should use NSUTF8StringEncoding everywhere, and you're fine.
Why are you using ASCII anyways? Why are you converting a string to an NSData and then back to NSString again? It doesn't make sense.

How do I remove new line characters from a string?

I need to strip away any new line characters from a string. I tried the stringByReplacingOccurencesOfString method below. What is the correct method to do this in Objective C?
[textLine stringByReplacingOccurrencesOfString:#"\n" withString:#""];
NSString *textWithNewLinesRemoved = [textLine stringByTrimmingCharactersInSet:[NSCharacterSet newlineCharacterSet]];
stringByReplacingOccurrencesOfString does not modify textLine
NSString *strippedTextLine = [textLine stringByReplacingOccurrencesOfString:#"\n" withString:#""];
Or
NSString *textLine = #"My cool text\n";
textLine = [textLine stringByReplacingOccurrencesOfString:#"\n" withString:#""];

stringByReplacingOccurrencesOfString: doesn't work

I want my output to be "23", but it still shows "123".
NSString *valorTextField = [[NSString alloc] initWithFormat:#"123"];
[valorTextField stringByReplacingOccurrencesOfString:#"1" withString:#""];
NSLog(#"%#", valorTextField);
stringByReplacingOccurrencesOfString returns a new string:
NSString *valorTextField = [[NSString alloc] initWithFormat:#"123"];
NSString *replaced = [valorTextField stringByReplacingOccurrencesOfString:#"1" withString:#""];
NSLog(#"%#", replaced);
Alternatively, you can work with a NSMutableString and use the
replaceOccurrencesOfString:withString:options:range: method.
You are not assigning returned string to valorTextField again. You are just executing function. Actual string is unchanged. Instead write:
valorTextField = [valorTextField stringByReplacingOccurrencesOfString:#"1" withString:#""];
NSLog(#"%#", valorTextField);

Convert NSString to UTF8String?

Currently I am download data from a server and I have this line to get that NSData into a NSString.
NSString *txt = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
How would I go from here to convert that NSString into a UTF8 string?
Thanks!
NSData *utf8Data = [txt dataUsingEncoding:NSUTF8StringEncoding];
Change NSASCIIStringEncoding to NSUTF8StringEncoding .
I ran a quick test now with some dummy data and seemed to work fine with just that one line.

Resources