(iPhone)URL formatting question - iphone

I am trying to format a URL but am getting a bug out of it. My code is below.
NSString *twitterURL = [NSString stringWithFormat:#"http://twitter.com/?status=My%20score%20is:%i%20and%20CharsPerMin%20is:%#", currentScore, charPerMin.text];
When calling the method it doesn't do a thing. I think the issue is with %20. %20 is being used to space each word in the URL.

You need to escape your % signs by doubling them:
NSString *twitterURL = [NSString stringWithFormat:#"http://twitter.com/?status=My%%20PracticeTyper%%20score%%20is:%i%%20and%%20CharsPerMin%%20is:%#", currentScore, charPerMin.text];

Related

String includin Escape character

In textfield when user enter value with escape character, I want to work this same as user enter but it behaves like a string for \e too.
In more detail.
suppose user have entered \e[3~ but when i print it in console it shows \e[3~. Actually this should print like ¿[3~ so it will work perfect for me.
I have tried by this line and it works.
NSString * str=[textField.text stringByReplacingOccurrencesOfString:#"\\e" withString:#"\e"];
But suppose if user enter value in caps like \E[3~ then its not replacing. so i have tried with like
NSString * str=[textField.text stringByReplacingOccurrencesOfString:#"\\e" withString:#"\e"];
str=[textField.text stringByReplacingOccurrencesOfString:#"\\E" withString:#"\E"];
But this is not working in case while user have entered text in small \e[3~. With this when first line will execute to replace \e it will give me perfect result like ¿[3~ but when it will execute next line it gives me same \e[3~ string.
Please suggest me how can i check for both letters and if i can check for all escaping characters at once.
The error in your code is that the second line takes the original string textField.text
instead of the result from the first line. What you probably meant is:
NSString *str = [textField.text stringByReplacingOccurrencesOfString:#"\\e" withString:#"\e"];
str = [str stringByReplacingOccurrencesOfString:#"\\E" withString:#"\E"];
Alternatively, you can do the replacement in a single step by using the
NSCaseInsensitiveSearch option:
NSString *str = [textField.text stringByReplacingOccurrencesOfString:#"\\e"
withString:#"\e"
options:NSCaseInsensitiveSearch
range:NSMakeRange(0, [textField.text length])];

I lost % in html source when using stringWithContentsOfUrl

NSString *htmlSource = [NSString stringWithContentsOfURL:[NSURL URLWithString:urlString] encoding:0x80000000 + kCFStringEncodingDOSKorean error:nil];
NSLog(htmlSource);
I get html result tag like (col width="16"/)~~~ but the real html tag is (col width="16%"/)
% character disappears. what is problem?
The string might still contain a %; NSLog() itself gives special significance to a % sign (consider what happens if you use %#, %d, etc.).
Try doing this: NSLog(#"%#", htmlSource); that will log only an object (the string) and keep it out of the formatting argument.

stringWithFormat and % not working

i have this string
NSString *jsonString = #"http://www.soccerway.com/a/block_home_matches?block_id=block_home_matches_14&callback_params=%7B%22date%22%3A%222012-07-31%22%2C%22display%22%3A%22all%22%7D&action=showMatches&params=%7B%22competition_id%22%3A721%7D";
NSLog(#"%#",jsonString);
the output is
http://www.soccerway.com/a/block_home_matches?block_id=block_home_matches_14&callback_params=%7B%22date%22%3A%222012-07-31%22%2C%22display%22%3A%22all%22%7D&action=showMatches&params=%7B%22competition_id%22%3A721%7D
when i use
NSString *linkId = #"448";//not a constant value only for example
NSString *jsonString = [NSString stringWithFormat:#"http://www.soccerway.com/a/block_home_matches?block_id=block_home_matches_14&callback_params=%7B%22date%22%3A%222012-07-31%22%2C%22display%22%3A%22all%22%7D&action=showMatches&params=%7B%22competition_id%22%3A%#%7D",linkId];
the output is
http://www.soccerway.com/a/block_home_matches?block_id=block_home_matches_14&callback_params=7 37040ate23A222㿠 37040isplay23A0x1.21800000507cp-1027ll27D&action=showMatches&params=7 –ompetition_id23A(null) 0
as you see not the same.My question is how to use stringWithFormat to get this result:
http://www.soccerway.com/a/block_home_matches?block_id=block_home_matches_14&callback_params=%7B%22date%22%3A%222012-07-31%22%2C%22display%22%3A%22all%22%7D&action=showMatches&params=%7B%22competition_id%22%3A448%7D
so the value (721) just at the and is replaced by (448)
thanks in advance.
It's because all those % characters inside your format string are being potentially used to used the format arguments, much like %# (see here for details).
This can be seen (for one instance) where:
callback_params=%7B%22date
is transformed into:
callback_params=7 37040ate
In that case, I'm not sure what the %7B is doing since it's not a valid format specifier, but the %22date is resulting in a 22-character decimal value, from %22d, followed by the literal ate.
You need to use %% in your format string if you want a single % in the output string.
The other way of looking at it is that the thing you're giving it as a format string is really data, not purely a format.
To be safe from those spurious conversions, you'd want:
NSString *jsonString = [NSString stringWithFormat:#"%#%#%#", #"http://www.soccerway.com/a/block_home_matches?block_id=block_home_matches_14&callback_params=%7B%22date%22%3A%222012-07-31%22%2C%22display%22%3A%22all%22%7D&action=showMatches&params=%7B%22competition_id%22%3A",linkId, #"%7D"];

NSString encoding conversion

i have a problem with this particular NSString B and B . As you can see the encoding is different. So I am wondering if there is any way to convert B to B so that the computer can standardize the character.
Does this work?
NSString *newString = [oldString stringByReplacingOccurrencesOfString:#"B" withString: #"B"];

NSString removing single quote in string

This should be simple but it's not working. I am trying to strip single quote marks from an NSString named parms using the following (stripped of non-relevant vars in the format string):
NSString *newVar =[[NSString alloc] initWithFormat:#"%#", [parms stringByReplacingOccurrencesOfString:#"'" withString:#""]];
So if parms contains "Mike's Hat" I would expect that newVar would contain "Mikes Hat". Instead it contains "Mike's Hat".
There must be more to your code than you are proving, but the following works perfectly:
NSString *parms = #"Mike's Hat";
NSString *newVar =[parms stringByReplacingOccurrencesOfString:#"’" withString:#""];
NSLog(#"%#",newVar);
Output: Mikes Hat
There could be a possibility that the character ' may not be the same character in your parms string if the above does not work for you.
Turns out, you are using the wrong character copy/paste this character into your string: ’
Just my two cents on this same problem I had in my code.... When I used the single quote on the keyboard to type ' into my code it didn't work. But I was printing string values to the console. When I copied and pasted the ' character from the console into my code it then worked. What is weird is that I'm using the same key on the keyboard to enter the string into a UITextField so I really don't know why the same key gets turned into something different but that's how I solved it.