How to replace &ndash with Objective-C - objective-c

I'm getting JSON back from an API, and when the article text returns, alot of times there will be a dash. But the dash comes thru as &ndash instead how the actual -...
How do I convert the &ndash to just the actual -?
Thanks for the help!

NSString *myString = #"Here is &ndash some text";
myString = [myString stringByReplacingOccurrencesOfString:#"&ndash" withString:#"-"];

Related

Decode String with Emoji characters such as \ud83d

I have following string, and i want to show it in a UILabel properly with emojies and the new lines. Also I want to draw it using drawInRect method. How do I get them converted/Encoded/Decoded properly?
This string will change on runtime so should show any unicode character/ emoji or special characters such as \n or &
I'm sorry that I do not know proper terms to use to ask this question. Which makes it difficult for me to find an answer online. My knowledge about this topic is very low.
\ud83d\ude02\ud83d\ude02\ud83d\ude02\ud83d\ude02\ud83d\ude02\ud83d\ude05\ud83d\ude06
\u0db8\u0da0\u0d82
\u0d91\u0d9a\u0dca\u0d9a\n#set_with_machan\nkunuharapa na \n#Follow
#lankan_machan\nhttps://www.instagram.com/lankan_machan
after encoding the text should look like this with emojis, unicode characters & new lines.
I was able to find a solution and Edited it a bit. This un escapes the unicode characters perfectly and shows them properly. Shows the new lines too. Thanks #DanZimm for help.
- (NSString*) unescapeUnicodeString2:(NSString*)string
{
NSString* esc1 = [string stringByReplacingOccurrencesOfString:#"\\u" withString:#"\\U"];
NSString* esc2 = [esc1 stringByReplacingOccurrencesOfString:#"\"" withString:#"\\\""];
NSString* quoted = [[#"\"" stringByAppendingString:esc2] stringByAppendingString:#"\""];
NSData* data = [quoted dataUsingEncoding:NSUTF8StringEncoding];
NSString* unesc = [NSPropertyListSerialization propertyListWithData:data options:NSPropertyListImmutable format:NULL error:NULL];
assert([unesc isKindOfClass:[NSString class]]);
return unesc;
}
Solution found here I had to edit it because one of the methods were deprecated.

How to Use An Escape URL String With Formatting in Objective-C

I need to gather data from a website based on the user's input.
searchString is the user inputted value, such as "search this string".
NSString *withoutSpaces = [searchString stringByReplacingOccurrencesOfString:#" " withString:#"%20"];
Here, I need to replace spaces with %20
Next, I need to put the new string without spaces (replaced with %20) into another string.
NSString *unescapedSearchString = [NSString stringWithFormat:
#"website.com/query?=%22%#%22", withoutSpaces];
The site I need is not really "website.com", but that's just an example. I also need the %22 to remain at the beginning and end.
As you can see, I need the %# to format the new withoutSpaces user input into the website URL.
I did a search and found examples but I could not find any with formatting such as in my case using %#.
What's the best way to "escape" the characters and keep my formatted string? Currently, when I try to access data from the website, it comes back as null. However, when I try a string without the %# formatting and an actual value, I successfully retrieve the data from a website.
Any help is greatly appreciated.
You should do things this way:
NSString *searchString = ... // the raw search string with spaces and all
NSString *quoted = [NSString stringWithFormat:#"\"%#\"", searchString];
NSString *escaped = [quoted stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSString *urlString = [NSString stringWithFormat:#"website.com?query=%#&value=all", escaped];
BTW - the URL seems a little off. There should be a variable name before the = and after the ?.

Parsing arabic text with SBJson in Xcode

i have a JSON file that i'm trying to parse using SBJson.
the response string that i'm receiving is displaying arabic characters correctly when i do a
NSLog(#"%#",responseString);
but whenever i use the SBJson parser
NSDictionary *myDictionary = [responseString JSONValue];
and try
NSLog(#"%#", myDictionary);
my arabic characters are transformed to something weird:
\U0633\U0627\U0642\U064a\U0629 \U0627\U0644\U0645\U0633\U0643
please can anyone help
Don't worry. All is well.
\u0633
is equivalence to
س
try this in C++:
std::wstring tStr = L"\u0633";
tStr will equal to س
Just NSLog is not converting
use this :
NSStringEncoding Arabicencoding = CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingWindowsArabic);
NSString *yourstring = [[NSString alloc]initWithData:yourdata encoding:Arabicencoding];
"\U0633\U0627\U0642\U064a\U0629 \U0627\U0644\U0645\U0633\U0643" is actually just the encoding that Objective-C uses for none ascii characters. Try adding a subview/label on whatever you're testing with which will display the text. First try with "responseString" and then "myDictionary" it could display the same.

\n does not skip to next line in NSString

NSMutableString *a = #"Hi";
NSMutableString *b =[a stringByAppendingString:#"\n\n Hi Again"];
The above doesn't give an error but does not put "Hi Again" on the next line. Why?
EDIT2
I realised after posting, that the OP had NSString in the title but put NSMutableString in the code. I have submitted an edit to change the NSMutableString to NSString.
I will leave this as it still maybe helpful.
Well I am surprised that does not give an error, because you are giving a NSMutableString a NSString.
You need to read the Documentation on NSMutableStrings.
to give you an idea
//non mutable strings
NSString *shortGreetingString = #"Hi";
NSString *longGreetingString = #"Hi Again";
/*mutable string - is created and given a character capacity The number of characters indicated by capacity is simply a hint to increase the efficiency of data storage. The value does not limit the length of the string
*/
NSMutableString *mutableString= [NSMutableString stringWithCapacity:15];
/*The mutableString, now uses an appendFormat to construct the string
each %# in the Parameters for the appendFormat is a place holder for values of NSStrings
listed in the order you want after the comma.
Any other charactars will be included in the construction, in this case the new lines.
*/
[mutableString appendFormat:#"%#\n\n%#",shortGreetingString,longGreetingString];
NSLog (#"mutableString = %#" ,mutableString);
[pool drain];
I think this might help you. You'd rather to use '\r' instead of '\n'
I also had a similar problem and found \n works in LLDB but not in GDB
Try using NSString. You could use:
NSString *a = [NSString stringWithFormat:#"%#\n\n%#", #"Hi", #"Hello again"]
If your string is going in a UIView (e.g a UILabel), you also need to set the number of lines to 0
myView.numberOfLines=0;

simple question concerning NSString adding multiple strings

I have a fairly simple question concerning NSString however it doesn't seem to do what I want.
this is what i have
NSString *title = [NSString stringWithformat: character.name, #"is the character"];
This is a line in my parser takes the charactername and inserts in into a plist , however it doesn't insert the #"is the character" is there something I'm doing wrong?
Your code is wrong. It should be :
NSString *title
= [NSString stringWithformat:#"%# is the character", character.name];
assuming that character.name is another NSString.
Read the Formatting String Objects paragraph of the String Programming Guide for Cocoa to learn everything about formatting strings.
stringWithFormat takes a format string as the first argument so, assuming character.name is the name of your character, you need:
NSString *title = [NSString stringWithformat: #"%s is the character",
character.name];
What you have is the character name as the format string so, if it's #"Bob" then Bob is what you'll get. If it was "#Bob %s", that would work but would probably stuff up somewhere else that you display just the character name :-)
Note that you should use "%s" for a C string, I think "%#" is the correct format specifier if character.name is an NSString itself.