I am getting userid through parsing a link.Again i have to parse it with userid to get the access.
What i am doing
NSString *str=[[NSString alloc]initWithFormat:#"http://abc.com/fb_redirect_mobile/?accessToken=4546"];
This gives me the userid,now i want to use that userid to parse it again such as:
NSString *str=[[NSString alloc]initWithFormat:#"http://abc.com/user/userid/bookmarks"];
In other languages I have seen they are just using:
NSString *str=[[NSString alloc]initWithFormat:#"http://abc.com/user/"+userid+"/bookmarks"];
The userid variable takes user id.how i can do this in iPhone.I know my question is lengthy but i tried to make it clear what i want.please help..please also tell me how to store a parsing id into string,such as userid i am going to get after parsing the url./now how i can save it in form of string.
NSString *userId = #"123456";
NSString *str=[[NSString alloc]initWithFormat:#"http://abc.com/user/%#/bookmarks", userId];
initWithFormat:/stringWithFormat: follow the general format convention set by printf/scanf
The way you concatenated your userid is not valid syntax in Obj-C
NSString *str=[[NSString alloc]initWithFormat:#"http://abc.com/user/"+userid+"/bookmarks"];
What you'd probably want to do is use a format specifier for an Obj-C object (in your case NSString), and use that within your URL (assuming userid is an NSString, which it probably is. If it's a C based string, use %s as your format specifier instead).
NSString *str=[[NSString alloc]initWithFormat:#"http://abc.com/user/%#/bookmarks", userid];
Refer to these on how to use stringWithFormat: on an NSString:
Formatting String Objects
String Format Specifiers
you can give like this,
NSString *str=[NSString stringWithFormat:#"http://abc.com/user/%#/bookmarks",userid];
Instead of
NSString *str=[[NSString alloc]initWithFormat:#"http://abc.com/user/"+userid+"/bookmarks"];
You would use
NSString *str=[[NSString alloc] initWithFormat:#"http://abc.com/user/%#/bookmarks",userid];
Well you almost got it:
NSString *usrid = #"4546";
NSString *str=[NSString stringWithFormat:#"http://abc.com/fb_redirect_mobile/?accessToken=%#", userid];
You can use the stringWithFormat: method of NSString for this.
Related
I'm doing this to encode my URL in this way,
but its not working,
i got the result in NSLog but its the same url nothing is changing.
Please help me to sort this issue.
below is my code :
NSString *unencodedUrlString =
[#"http://www.demii.com/demo/dooponz/admin/index.php/chat/new_message/4/1/you/2,7"
stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSLog(#" %#", unencodedUrlString);
Thanks in advance
The comma is a legal URL character, therefore stringByAddingPercentEscapesUsingEncoding leaves "2,7" as it is and does not replace it by "2%2C7".
If you want the comma to be replaced by a percent escape (as I understand from your
comment to the question), you can use CFURLCreateStringByAddingPercentEscapes
instead:
NSString *str = #"http://www.demii.com/demo/dooponz/admin/index.php/chat/new_message/4/1/you/2,7";
NSString *encoded = CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault,
(__bridge CFStringRef)(str), NULL, CFSTR(","), kCFStringEncodingUTF8));
NSLog(#"%#", encoded);
Output:
http://www.demii.com/demo/dooponz/admin/index.php/chat/new_message/4/1/you/2%2C7
The fourth parameter CFSTR(",") specifies that the comma should be replaced by
a percent escape even if it is a legal URL character.
Use this
NSString *str = [NSString stringWithFormat:#"http://www.demii.com/demo/dooponz/admin/index.php/chat/new_message/4/1/you/2,7"];
NSString *path = [str stringByReplacingOccurrencesOfString:#"," withString:#"/"];
NSLog(#"%#",path);
This will do nothing but will make , to /.
i need to send smiley to other user through iphone app ,so i need to replace \ string with some unique string in obj c.
here if your string is #"\ud83d\ude04" then it is give error "Invalid Character" so put this ' special character and then use it ..
NSString *str = #"\'ud83d\'ude04";//// here if your string is #"\ud83d\ude04" then it is give error "Invalid Character" so put this ' special character and then use it
NSString *smileWithString = [str stringByReplacingOccurrencesOfString:#"\'" withString:#":)"];
[smileWithString retain];
NSLog(#"\n\n SmileString %# Str %#",smileWithString);
Update:
Here’s how to convert NSString to NSData – it’s really simple:
NSString *myString = #"Some String";
NSData *myData = [myString dataUsingEncoding:NSUTF8StringEncoding];
And what about the reverse conversion, i.e. how to convert NSData to NSString? Here’s one quick way:
NSString *myString = [NSString stringWithFormat:#"%.*s",[myData length], [myData bytes]];
Use encoding of NSString and when need to use or show string decode it.
Refer base64-encoding link.
Your looking for stringByReplacingOccurrencesOfString that should do the trick.
NSString *newString = [oldString stringByReplacingOccurrencesOfString:#"\" withString:#"uniqueString"];
I want to create JSON string like {"search_element": "New York"} this. I used following code for that.
NSString *JSONString = [NSString stringWithFormat:#"{\"search_element\":""\"%#\"""}",
[searchName stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
After doing this I am getting value like {"search_element":"New%20York"} this. I want it should be New York instead New%20York. I am not getting How to format it for expected result.Please help me.
Can't you just use it like that?
NSString *JSONString = [NSString stringWithFormat:#"{\"search_element\":""\"%#\"""}", searchName];
So basically without the stringByAddingPercentEscapesUsingEncoding: method
And I don't really understand why do you use that:
#"{\"search_element\":""\"%#\"""}"
I'd do it with less quotation marks:
#"{\"search_element\":\"%#\"}"
Did a quick test:
NSString *test = #"{\"search_element\":\"%#\"}";
NSLog(test, #"New York");
output:
{"search_element":"New York"}
Hope it helps
you can simply call next
NSString *newString = [JSONString stringByReplacingOccurrencesOfString:#"%20" withString:#""];
However look into JSONKit and SBJSON ..they have methods to make valid JSON in iOS
I have this:
partenaire_lat = 48.8160525;
partenaire_lng = 2.3257800;
And obtain a NSString like this:
NSString *endPoint =[NSString stringWithFormat:#"%#,%#", partenaire_lat, partenaire_lng];
and after using this NSString in some context I get this stupid error:
Variable is not a CFString.
But if I create the NSString like this:
endPoint = #"48.8160525,2.3257800" it then works perfect!
For this error Variable is not a CFString I tried the following:
NSString *endPoint1 =[NSString stringWithFormat:#"%#,%#", partenaire_lat, partenaire_lng];
CFStringRef endPoint =(CFStringRef)endPoint1;
and tried to use endPoint but not working neither this way.Anyone any miraculous idea?Thx
EDIT:partenaire_lat and partenaire_lng are both NSString!!
You have
partenaire_lat = 48.8160525;
partenaire_lng = 2.3257800;
You keep saying that the two variables are NSStrings but you aren't assigning NSStrings to them. You need to assign NSString objects to NSString variables - they aren't created for you automatically.
So the answers which are telling you to use formatted strings are correct. You really should be doing it like this:
partenaire_lat = [[NSString stringWithFormat:#"%f", 48.8160525] retain];
partenaire_lng = [[NSString stringWithFormat:#"%f", 2.3257800] retain];
what are lat and lng? i'm assuming float or double..so you should use [NSString stringWithFormat:#"%f,%f", lat, lng]; (or however you want the floats to be formatted)
You code has several potential problems:
%# format specifier expects object parameter, while it looks like you pass plain float (I may be wrong here as there's not enough context to be sure). Change format to %f to fix your problem if that's really the case:
NSString *endPoint1 =[NSString stringWithFormat:#"%f,%f", partenaire_lat, partenaire_lng];
Your endPoint1 string is autoreleased and may become invalid outside of current scope if you don't retain it. So if you try to use your variable in another method you probably should retain it.
All you need to do
NSString *latStr=[[NSNumber numberWithFloat:partenaire_lat] stringValue];
NSString *lngStr=[[NSNumber numberWithFloat:partenaire_lng] stringValue];
and do whatever you want to do with these two string :)
I'm getting an NSString from a dictionary that needs to have a variable integer, something like:
"You have %i objects."
How do I put the calculated integer value into the string? I would like to do something like
NSString *string = [NSString stringWithFormat:[dictionary objectForKey:#"string"]
But I don't know how to pass in an argument to stringWithFormat when the %i is tucked away in the dictionary.
Note: I can work around this by using stringByReplaceOccurenceOfString, but I'd like to know if it's possible to do it in the above way.
You can pass a comma separated list of arguments to methods like stringWithFormat:
NSString *string = [NSString stringWithFormat:
[dictionary objectForKey:#"string"] , 7 , 8 , 9];
Only the 7 would be used in your sample format string.
You can access it using this
NSString *string = [NSString stringWithFormat:[dictionary objectForKey:#"string"],YOURINTNUMBER];
It will work