Im having trouble disecting this RSS feed: http://missing.amberalertnederland.nl/nl/index.rss
I want to display the images in a tableview, but the images arent given a seperate tag. How do I extract these images from the description tag? Scan for < and > ?
answer:
- (NSString *)getImage:(NSString *)imageString{
NSString *urlImage = nil;
NSScanner *scanner = [NSScanner scannerWithString:imageString];
[scanner scanUpToString:#"src=\"" intoString:nil];
if (![scanner isAtEnd]) {
[scanner scanString:#"src=\"" intoString:nil];
NSString *urlImage = nil;
[scanner scanUpToString:#"\"" intoString:&urlImage];
if (![scanner isAtEnd]) {
NSLog(#"%#", urlImage);
}
}
return urlImage;
}
Yes, use NSScanner to san up to src=" than scan to the next " and place the result in a temp string.
That should do the trick.
Related
I get an html back from the server formatted as this
www.mysite.com
it is an NSString
How can i filter my NSString so i only keep "www.mysite.com" between the <a> tags?
Use NSRegularExpression:
NSString *string = #"www.mysite.com";
string = [string stringByReplacingOccurrencesOfString:#"<.+?>" withString:#"" options:NSRegularExpressionSearch range:NSMakeRange(0, string.length)];
For more information about regex in objective-c, and more advanced examples, see the documentation: https://developer.apple.com/library/ios/#documentation/Foundation/Reference/NSRegularExpression_Class/Reference/Reference.html
You can try with NSSCanner,
NSString *mystring = #"<status>SUCCESS</status>";
NSString *neededString = nil;
NSScanner *scanner =[NSScanner scannerWithString:mystring];
[scanner scanUpToString:#">" intoString:&neededString];
[scanner scanString:neededString intoString:NULL];
[scanner scanString:#">" intoString:NULL];
[scanner scanUpToString:#"<" intoString:&neededString];
NSLog(#"%#",neededString)
If you're certain enough of the format, you can get the index of the first > character and then the first < character after that, and then take the substring that starts after the first index and ends after the second.
There are multiple ways of doing this, including
NSArray *a = [ string componentsSeparatedByCharactersInSet: ... ]
and
NSRange r = [ string rangeOfCharacterFromSet: ... ];
I have a string that's like
"Condition: some text which can have a comma in it but not always, Type more text Product: more text, can be NULL sometimes
What I did was:
NSString *condition = #"Condition:";
NSString *type = #", Type";
NSString *product = #"Product:";
NSScanner *scanner = [NSScanner scannerWithString:myString];
[scanner scanString:condition intoString:NULL];
[scanner scanUpToString:type intoString &conditionName];
[scanner scanUpToString:type intoString:NULL];
[scanner scanUpToString:product intoString:&typeName];
[scanner scanString:product intoString:NULL];
// stuck here
I don't know how to scan till the end of the string since scanString:intoString: takes a NSString parameter. I wasn't sure how to put these into a while loop either to scan till the end. Any thoughts? Thanks.
Probably:
while (![scanner isAtEnd]) {
[scanner scanString:condition intoString:NULL];
[scanner scanUpToString:type intoString:&conditionName];
[scanner scanString:type intoString:NULL];
[scanner scanUpToString:product intoString:&typeName];
[scanner scanString:product intoString:NULL];
[scanner scanUpToString:condition intoString:&productName];
// do something with read variables
}
From the docs:
If the search string (stopString) isn't present in the scanner's source string, the remainder of the source string is put into stringValue, the receiver’s scanLocation is advanced to the end of the source string, and the method returns YES.
So you should just scan up to condition again at the end of the string for the next iteration of the loop.
This is a followup to my previous question: Extracting an href's text from an html document
How can I get the number in seekVideo(number)? For example:
<a href="#" class="transcriptLink" onclick="seekVideo(2000); return false;"
I want to get "2000".
I have tried:
NSArray *elements = [xpathParser search:#"//div[#id='transcriptText']/div/p/number(substring-before(substring-after(#onclick, '('), ')'))"];
But that's not right. How can I do this?
If someone can you use the code framework that Kirill Polishchuk provided in this web(https://stackoverflow.com/questions/6723244/iphone-how-to-select-this-label), it would be great.
You can use NSScanner:
int seekVideo = 0;
NSString *testString = #"<a href=\"#\" class=\"transcriptLink\" onclick=\"seekVideo(2000); return false;\"";
NSScanner *scanner = [NSScanner scannerWithString:testString];
[scanner scanUpToString:#"seekVideo(" intoString:nil];
[scanner scanString:#"seekVideo(" intoString:nil];
[scanner scanInt:&seekVideo];
NSLog(#"seekVideo: %d", seekVideo);
2011-08-30 07:47:11.504 Test[56342:707] seekVideo: 2000
How do I get string using NSScanner from a string which contains string as well as numbers too?
i.e. 001234852ACDSB
The result should be 001234852 and ACDSB
I am able to get numbers from the string using NSScanner and characters by using stringByReplacingOccurrencesOfString but I want to know, is that possible to get string from with the use of NSScanner or any other built in methods?
I would like to know the Regex for the same.
If you can guarantee that the string always consists of numbers followed by letters, then you could do the following with NSScanner:
NSScanner *scanner = [NSScanner scannerWithString:#"001234852ACDSB"];
NSString *theNumbers = nil;
[scanner scanCharactersFromSet:[NSCharacterSet decimalDigitCharacterSet]
intoString:&theNumbers];
NSString *theLetters = nil;
[scanner scanCharactersFromSet:[NSCharacterSet letterCharacterSet]
intoString:&theLetters];
A regular expression capturing the same things would look like this:
([0-9]+)([a-zA-Z]+)
Finally after google for the same and go through some information from net, I reached to my destination. With this I'm posting the code, this may help many who are facing the same problem as I have.
NSString *str = #"001234852ACDSB";
NSScanner *scanner = [NSScanner scannerWithString:str];
// set it to skip non-numeric characters
[scanner setCharactersToBeSkipped:[[NSCharacterSet decimalDigitCharacterSet] invertedSet]];
int i;
while ([scanner scanInt:&i])
{
NSLog(#"Found int: %d",i); //001234852
}
// reset the scanner to skip numeric characters
[scanner setScanLocation:0];
[scanner setCharactersToBeSkipped:[NSCharacterSet decimalDigitCharacterSet]];
NSString *resultString;
while ([scanner scanUpToCharactersFromSet:[NSCharacterSet decimalDigitCharacterSet] intoString:&resultString])
{
NSLog(#"Found string: %#",resultString); //ACDSB
}
You don't have to use a scanner to do it.
NSString *mixedString = #"01223abcdsadf";
NSString *numbers = [[mixedString componentsSeparatedByCharactersInSet:[[NSCharacterSet characterSetWithCharactersInString:#"0123456789"] invertedSet]] componentsJoinedByString:#""];
NSString *characters = [[mixedString componentsSeparatedByCharactersInSet:[[NSCharacterSet characterSetWithCharactersInString:#"abcdefghijklmnouprstuwvxyz"] invertedSet]] componentsJoinedByString:#""];
For other possible solution view this question Remove all but numbers from NSString
I'm using this method to find the first <> couple into a string (XML content) :
NSScanner* scanner = [NSScanner scannerWithString:contentToParse];
int startPos = 0;
int endPos = 0;
// Open search
if ([scanner scanString:#"<" intoString:nil]) {
startPos = [scanner scanLocation]-1;
NSLog(#"found '<' at pos %i", startPos);
// close search
if ([scanner scanString:#">" intoString:nil]) {
endPos = [scanner scanLocation]-1;
NSLog(#"found '>' at pos %i", endPos);
NSString* tag = [contentToParse substringWithRange:NSMakeRange(startPos, endPos-startPos)];
NSLog(#"Tag found : %#", tag);
}
}
but only "found '<' at pos 0" is logged.
My XML content contains many many <> items...
Why is that method not working ?
scanString:intoString: tries to scan the string parameter at the current location. If such string is not present at the current location, it simply returns NO.
You may want use scanUpToString:intoString: (reference) instead, which scans advancing the scan location until the given string is encountered.
NSScanner *scanner = [NSScanner scannerWithString:contentToParse];
// open search
[scanner scanUpToString:#"<" intoString:nil];
if (![scanner isAtEnd]) {
[scanner scanString:#"<" intoString:nil];
// close search
NSString *tag = nil;
[scanner scanUpToString:#">" intoString:&tag];
if (![scanner isAtEnd]) {
NSLog(#"Tag found : %#", tag);
}
}