NSString is not appearing properly - iphone

I have the following NSString:
NSString* searchURL = [NSString stringWithFormat:#"http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20yahoo.finance.quotes%20where%20symbol%20in%20(%22%#%22)%0A%09%09&format=json&env=http%3A%2F%2Fdatatables.org%2Falltables.env&callback=",symbol];
NSLog(#"URL IS: %#", searchURL);
Looks like the %22 is not being included when it is being printed:
URL IS: http://query.yahooapis.com/v1/public/yql?q=select220from2ahoo.finance.quotes2here `º≠ymbol 813020n22#20X1.000982B6P-1042009&format=json&env=http0X1.8CFB8P-1023-1.9907460.000000datatables.org-1.990746alltables.env&callback=
How can I make sure the %22 is included in my string?

If you want to include a "%" sign in a format string use "%%"
Just like in printf et al.
Read the full documentation of stringWithFormat to avoid other bad surprises ...

% is a special character in format strings. Use %% to escape literal percent signs.

% character are used in encoding space in the url formation.If you want to request any url you need to encoding that with stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding
. So the % sign that are appearing are the encoded form of space.So implement accordingly by clearing this fundamental

Related

Getting weird characters when converting NSArray to JSON

I am getting some weird characters when converting a NSArray containing NSDictionaries to a JSON string.
I tried using both SBJson and NSJSONSerialization with the same result.
The NSDictionary is populated with the content of the address book, with the contact name, email and phone number, and are mostly in hebrew.
The characters look like this:
\327\237
I could not find any information about this, help anyone?
Thanks in advance!
EDIT *
Here is a snippet of the JSON:
[
{"fname":"סתם טקסט"},
{"fname":"סתם טקסט"},
{"fname":"נ\327\231ר"}
]
its supposed to be:
[
{"fname":"סתם טקסט"},
{"fname":"סתם טקסט"},
{"fname":"ניר"}
]
And i am getting the JSON by using the following code:
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:ContactsArray options:NSJSONReadingMutableLeaves error:&err];
NSLog(#"JSON: %#", [NSString stringWithUTF8String:[jsonData bytes]]);
These characters are octal escape codes. I prefer to look at things in hex. \327 and \237 are 0xD7 and 0x9F in hex.
I looked up U+00D7 and U+009F (unicode characters). They are MULTIPLICATION SIGN and APPLICATION PROGRAM COMMAND. That doesn't make sense in this context, so a straight conversion is not the way to go.
Next, I thought UTF-8 encoding. D7 9F decodes as U+05DF. This is HEBREW LETTER FINAL NUN. That makes sense in this context.
So, I'm guess the data you are seeing in UTF-8 characters that are not understood and octal escaped. JSON doesn't support octal escapes, so I'm guessing it's NSLog() or whatever you are using to print the JSON that is doing the escaping.

ios UTF8 encoding from nsstring

I am receiving a nsstring that is not properly encoded like "mystring%201, where must be "mystring 1". How could I replace all characters that could be interpreted as UTF8? I read a lot of posts but not a full solution. Please note that nsstring is already encoded wrong and I am not asking about how to encode char sequence. Thank you.
- (NSString *)stringByReplacingPercentEscapesUsingEncoding:(NSStringEncoding)encoding is what you want. basically use it like so:
newString = [myString stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
[urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]
Check the Strings and Non-ASCII Characters section of Formatting String Objects:
NSString *s = [NSString stringWithUTF8String:"Long \xe2\x80\x94 dash"];
lblDate.text=[NSString stringWithCString:[[arrTripDetail valueForKey:Param_vCurrencySymbol] UTF8String] encoding:NSUTF8StringEncoding];
U can Convert & get Custom Emoji to string
eg :
input : \U20b9
Output: ₹
Do you want just percent encoding/decoding or full URL encoding/decoding? -(NSString*)stringByReplacingPercentEscapesUsingEncoding: will work if it is just percent encoding, but if there is full URL encoding there (so, for example a space could be either %20 or +) then you'll need something like the url decoding in the three20 library (or search on there, there are lots of examples of how to do it such as URL decoding/encoding NSString).

Convert first Letter of each word of a sentence to capital in iPhone

I have the string:
i am a bad boy.
I want to convert that string into:
I Am A Bad Boy.
What I have tried only has worked for the first character of a word.
Here is Solution:
NSString *str=#"i am a bad Boy";
str=[str capitalizedString];
NSLog(str);
output will be:
I Am A Bad Boy.
use- capitalizedString method.
This method returns:
A string with the first character from each word in the receiver
changed to its corresponding uppercase value, and all remaining
characters set to their corresponding lowercase values.
You can check NSString documentation for more details -
https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/Reference/NSString.html

stringByAddingPercentEscapesUsingEncoding not working with NSStrings with ' 0'

I have been having some problem with the stringByAddingPercentEscapesUsingEncoding: method.
Here's what happens:
When I try to use the method to convert the NSString:
"..City=Cl&PostalCode=Rh6 0Nt"
I get this this..
"City=Cl&PostalCode=Rh62t"
It should be:
"..City=Cl&PostalCode=Rh6%200Nt"
What can I do about this? Thanks in advance !!
For me, this:
NSString *s=[#"..City=Cl&PostalCode=Rh6 0Nt" stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSLog(#"s=%#",s);
... outputs:
s=..City=Cl&PostalCode=Rh6%200Nt
You're most likely using the wrong encoding.
This happens when you're trying to encode to NSASCIIStringEncoding a string with characters not supported by ASCII.
Make sure you're encoding to NSUTF8StringEncoding, if the string can contain UTF8 characters or the method would return nil.

Using strings and variables in the iPhone SDK

I have a string here like so:
textbox.text =#"Your name is"
then I want to add right after "your name is" a variable that displays text.
so in Visual Basic I learned it like this:
textbox.text =#"Your name is" & variable1.
But now I can see that it doesn't work like that in Cocoa.
textbox.text = [NSString stringWithFormat:#"Your name is %#", variable1];
Read the documentation for stringWithFormat: to learn about string format specifiers. Basically, you have a format string that contains codes like %#, and the following arguments are put in place of those escape codes.
It has the same syntax as the old C-style printf() function. Cocoa's logging function, NSLog(), also works the same way.
If you need to combine a lot of strings together, try also reading about NSMutableString.
You could also do:
textbox.text = [#"Your name is " stringByAppendingString:variable1];
But if you have to concatenate more than two things, stringWithFormat: is much more concise.
Use NSString's stringByAppendingString: method:
textbox.text = [#"Your name is" stringByAppendingString:variable1];