nscanner taking value from a string - iphone

how can i get the value of pickey from this string.this string as such is stored in coredata and i need to extract the value of pickey.how can i do this using nscanner.which method should i use?
#"http://myserverIP/showpicture.php?email=mymail#yahoo.com&key=442205212&hash=63b201cacb5c07f6adbc8f3dcb408099d3450548&pickey=21342342342342341231"

NSScanner *scanner = [NSScanner scannerWithString:myString];
[scanner scanUpToString:#"pickey=" intoString:NULL];
if ([scanner scanString:#"pickey=" intoString:NULL]) {
long long pickeyValue = 0;
if ([scanner scanLongLong:&pickeyValue]) {
// Successfully found an integer value at this position
...
}
}

Related

NSScanner, reaching the end of parsing string

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.

Find characters from the given string with numbers.

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

iPhone - NSScanner does not parse

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);
}
}

NSScanner never reaches "isAtEnd" or don't scann full string

I've got an issue making scanner working for this particular string.
here comes the code :
tempString = #"30.15 in. Hg (1021 hPa)";
scanner = [NSScanner scannerWithString:tempString]; //setting the scanning location,
[scanner setCharactersToBeSkipped:[[NSCharacterSet characterSetWithCharactersInString:#"0123456789.,-+ "]invertedSet]];
value = 0;
_i = 0;
while([scanner isAtEnd] == NO)
{
[scanner scanFloat:&value];
if(_i == 1)
{
pressure = value;
}
_i++;
}
NSLog(#"pressure = %f hpa",pressure);
this infinity loop...
if I change the string with : tempString = #"30.15 in 8.8 Hg (1021 hPa)";
then it works fine
also if I change with : tempString = #"30.15 in Hg (1021 hPa)";
it also works fine.
the issue comes from the "." (dot)
any clean solution to make this work ?
thanks a lot.
You can check if -scanFloat: returns YES to check if a valid float is scanned. Skip the character if it returns NO.
while (![scanner isAtEnd]) {
if ([scanner scanFloat:&value]) {
if(_i == 1) {
pressure = value;
}
_i++;
} else {
[scanner setScanLocation:[scanner scanLocation] + 1];
}
}
tempString = #"30.15 in. Hg (1021 hPa)";
scanner = [NSScanner scannerWithString:tempString];
[scanner scanUpToString:#"(" intoString:nil];
[scanner scanString:#"(" intoString:nil];
[scanner scanFloat:&value];

iPhone: Parse Integer out of NSURL/NSString (NSScanner?)

I am trying to build an iPhone App, where I read an RSS feed and have to parse out the id of an article from the URL.
The links provided by the RSS feed are like the following:
http://example.com/variable/path/components/352343/index.html
I need 352343.
So basically I need to search for at least one digit between slashes (path components could also contain digits). Regexp would be easy: "//(\d+)//". But how can I do it with NSScanner?
Thanks,
Ernesto
You can split your url string into parts separated by "/" and check if any part is a valid integer.
NSArray* components = [urlString componentsSeparatedByString:#"/"];
int myId;
for (NSString *comp in components){
NSScanner *scanner = [NSScanner scannerWithString:comp];
if ([scanner scanInt: &myId])
return myId;
}
or you can simply use NSString method:
int myId;
for (NSString *comp in components)
if (myId = [comp intValue]) // intValue returns 0 if comp is not an integer
return myId;
You need to use it in combination with a Character set.
NSCharacterSet *charSet = [NSCharacterSet decimalDigitCharacterSet];
NSScanner *scanner = [NSScanner scannerWithString:url];
[scanner scanUpToCharactersFromSet:charSet intoString:nil];
int urlId;
[scanner scanInt:&urlId];
Of course, this is only if you know that numbers won't appear in the URL path before the ID. If they might, you'd need to get a little more robust than this, but you could use this snippet as a starting point.