OK here is nsmutablestring
data = [NSMutableString stringWithFormat:#"&cb_games%5B%5D="];
Now when ever I try to print or use this string I get big number instead of %5B and %5D not sure why this is happeing any help would be apritiated
thanks
The reason you get unexpected output is that '%' is used as conversion specifier in printf and obviously NSLog and NSString formattings. You need to escape '%' if you don't want it to be interpreted as a conversion specifier. You can escape '%' by preceding it with another '%' like '%%'.
Your string should look like,
#"&cb_games%%5B%%5D="
And the #August Lilleaas's answer is also noteworthy.
Try this:
NSString * data = [NSMutableString stringWithFormat:#"&cb_games%%5B%%5D="];
NSLog(#"%#",data);
stringWithFormat is basically printf, and it attempts to replace your percentages with values that you haven't provided, which is why wierd stuff happens.
[NSMutableString stringWithFormat:#"Hello: %d", 123];
// #"Hello: 123"
If you want a mutable string from a string, try this:
[NSMutableString stringWithString:#"Abc %2 %3"];
// #"Abc %2 %3"
The % is used for string formatting and stuff. I imagine you need to escape the character or something, possibly with a slash.
Did you mean to write?
data = [NSMutableString stringWithString#"&cb_games%5B%5D="]
Related
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¶ms=%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¶ms=%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¶ms=%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¶ms=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¶ms=%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¶ms=%7B%22competition_id%22%3A",linkId, #"%7D"];
Hi i'm returning a json string as follows:
{data,[{"id":1,"text":"blabla"},{"id":2,"text":"blabla2"}]}
When i try to print with NSLog like so:
NSLog(#"id: %#",[temp objectForKey:#"id"]);
i get large numbers, not the id.
If i enclose the the id's in quotes, things are fine.
Is there a way to decode the string when integers don't have quotes?
thx!
Looks like you have to use integerValue method to convert object to integer.
NSLog(#"id: %d",[[temp objectForKey:#"id"]integerValue]);
thxs for everyone's help.
Your formatting is a bit wonky. Also -objectForKey: takes an NSString for an argument, so be sure to prepend the quotes with # to signify an NSString constant.
NSLog(#"id: %#", [temp objectForKey:#"id"]);
The problem is in your string format. Instead of
NSLog(#"id: %#),[temp objectForKey:#"id"];
Try using this:
NSLog(#"id: %d),[temp objectForKey:#"id"];
%# is only for strings, and in your case, when the ID is not quoted, it's an integer and you should use %d instead.
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.
I have on one string like #"K_h_10_K_d_10_K_c_13_T_c_13_T_s_13"
I separate them by #"_"
using appCardString=[substringAppCard componentsSeparatedByString:#"_"];
then I have to convert them in to char and want to put in char[] ....
how can I do that ..
please help me ....
It's crashing here
appusedFaces[i]=[[NSString stringWithFormat:#"%#",[appCardString objectAtIndex:i]] charValue];
This will work:
appusedFaces[i]=[[appCardString objectAtIndex:i] characterAtIndex:0];
Though you should add a check that the string has at least one character. You should also be aware that char can only hold character codes up to 255 (unichar can handle any Unicode character).
It also looks like you have some numeric codes in your test string. Checking if the string has more than one character and then calling [[appCardString objectAtIndex:i] intValue] for those characters will handle these.
Say I have a string like "123alpha". I can use NSNumber to get the 123 out, but how can I determine the part of the string that NSNumber didn't use?
You can use NSScanner to both get the value and the rest of the string.
NSString *input = #"123alpha";
NSScanner *scanner = [NSScanner scannerWithString:input];
float number;
[scanner scanFloat:&number];
NSString *rest = [input substringFromIndex:[scanner scanLocation]];
If it is important to know exactly what is left after parsing the value this is a better approach than trying to trim characters. While I can't think of any particular bad input at the moment that would fail the solution suggested by the OP in the comment to this answer, it looks like a bug waiting to happen.
if your numbers are always at the beginning or end of a string and you want only the remaining characters, you could trim with a character set.
NSString *alpha = #"123alpha";
NSString *stripped = [alpha stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:#"0123456789"]];
If its starts out as a char * (as opposed to an NSString *), you can use strtol() to get the number and discover where the number ends in a single call.