How is stringWithFormat used here? - iphone

NSString *html="html page to parse";
NSString *text="some html text";
html = [html stringByReplacingOccurrencesOfString:
[NSString stringWithFormat:#"%#>", text] withString:#""];
My question is what will #"%#>" will do in stringwithFormat.
thanks

%# tells NSString you will be including an object in your string, so it will try to parse it as a string. According to Apple, %#:
"Objective-C object, printed as the string returned by descriptionWithLocale: if available, or description otherwise. Also works with CFTypeRef objects, returning the result of the CFCopyDescription function."
The first # symbol simply denotes a NSString.
Apple documentation

The code
html = [html stringByReplacingOccurrencesOfString:
[NSString stringWithFormat:#"%#>", text] withString:#""];
will replace occurence of some html text> in html page to parse with empty string.
So the result will be html page to parse only.
Using stringWithFormat You can easily perform many operation such as converting an int/float value to string,etc.,
int age=18;
NSSring *myage=[NSString stringWithFormat:#"My age is %d", age];
Here the value of myage is My age is 18.

Related

iPhone : insert a character inside a dynamic string

I have a dynamic string suppose
NSString *originalString = #"Hello my Phone : 123123123 abc 987";
In the above string first i have to check were numbers are present inside the string which i got it by doing below code :-
NSString *newString = [[MessageStr componentsSeparatedByCharactersInSet:
[[NSCharacterSet decimalDigitCharacterSet] invertedSet]]
componentsJoinedByString:#""];
result i got in newString is 123123123987
But my problem is not getting out numbers from string, i just want to add a special character after every number inside the string which is dynamic.
like i want to make my string like this :-
NSString *originalString = #"Hello my Phone : 1,2,3,1,2,3,1,2,3 abc 9,8,7";
So, please any one can suggest me, what's best possible way to achieve above string.
Thanks in advance.
I finally succeed by doing the following code :-
NSRegularExpression *regexp = [NSRegularExpression regularExpressionWithPattern:#"([0-9])" options:0 error:NULL];
NSString *newString = [regexp stringByReplacingMatchesInString:MessageStr options:0 range:NSMakeRange(0, MessageStr.length) withTemplate:#"$0,"];
NSLog(#"Changed %#", newString);
Thanks #nsgulliver
by posting answer in the link :-

How to encode URL in objective c xcode?

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 /.

NSString like '\uf003" replace '\' with some other string in in obj c

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"];

How do I strip HTML tags from my feed before displaying them in table cells?

I am reading a feed which gives the data with some html tags like
<p> this is a test string </p>.
I need to display this in UITableViewCell. The problem I have is , the html tags also gets displayed. How can I just show the text without html tag .??
Any help is greatly appreciated …!!!
Thanks
Just run the string through some stripping code before adding it to your tableview.
NSString *str = #"<p> this is a test string </p>";
str = [str stringByReplacingOccurrencesOfString:#"<p>" withString:#""];
str = [str stringByReplacingOccurrencesOfString:#"</p>" withString:#""];
str = [str stringByReplacingOccurrencesOfString:#"<h1>" withString:#""];
str = [str stringByReplacingOccurrencesOfString:#"</h1>" withString:#""];
//etc....
//new string equals #"this is a test string"
Depending on your needs you can search & replace those tags, or put the string you get from the server into UIWebView. It should present the html as it is.

How to decoding the string in iphone

I want to decode my string. I have used parsing and get a string from RSS feed. In a string these special characters are not allowed &,<,> in my app. In server side encoding those characters and give it to the string. So now i got the string like,
Actual String : <Tom&Jerry> (only these characters are not allowed in node data & < >).
After Encoding: %3CTom%26Jerry%3E.
But i need to display the string is
<Tom&Jerry>
So how can i decode the string.
Please help me out.
Thanks.
Use the -stringByReplacingPercentEscapesUsingEncoding: method.
[#"%3CTom%26Jerry%3E"
stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
Look for
- (NSString *)stringByReplacingPercentEscapesUsingEncoding:(NSStringEncoding)encoding
Or by example:
NSString *input = #"Hello%20World";
NSString *output = [text stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSLog(#"%# becomes %#",input,output);
Log: Hello%20World becomes Hello World
I got the answer and my code is,
NSString *currentString =#"%3CTom%26Jerry%3E";
NSString * decodeString = [currentString stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
lblTitle.text = decodeString;
Thanks.