Showing posts with label NSString. Show all posts
Showing posts with label NSString. Show all posts

Tuesday, August 14, 2012

How to use NSScanner on NSString.

This is quiet useful in terms of parsing a simple static webpage, also pick certain kind of words from the NSString.

Lets say we have following snippet from a html file.


NSString *htmlString=


< div id="topLeft" class="news" style="padding-top:3px"
onMouseOver="style.color='white';style.backgroundColor='#F0ABFF'" onMouseOut="style.color='black';style.backgroundColor='#000000'"><a style="text-decoration:none;color:black" onMouseOver="style.color='white'" onMouseOut="style.color='black'"href="reetu/test/news.htm" target="_blank">Latest News</a>


Now I want to extract the url, text and div id from this html file snippet. Following code will give you that.


     NSScanner *rScanner = [NSScanner scannerWithString:string];
     NSString *divID=@"";
    [rScanner scanString:@"<div id=\"" intoString:NULL];
    [rScanner scanUpToString:@"class=\"news\"" intoString:&courseID];

     divID=[divID stringByReplacingOccurrencesOfString:@" " withString:@""];
     divID=[divID stringByReplacingOccurrencesOfString:@"\"" withString:@""]; 
     divID=[divID stringByReplacingOccurrencesOfString:@"<divid=" withString:@""]; 
     NSLog(@"divID=%@",divID);           

/*Instead of above 3 lines you could also do [rScanner scanUpToString: instead of scanString:
    

    NSString *rURL=@"";
    [rScanner scanUpToString:@"href=\"" intoString:NULL];
    [rScanner scanUpToString:@">" intoString:&rURL];
     rURL=[rURL stringByReplacingOccurrencesOfString:@"href=\"" withString:@""];
     rURL=[rURL stringByReplacingOccurrencesOfString:@"target=\"_blank\"" withString:@""];
     rURL=[rURL stringByReplacingOccurrencesOfString:@"\" " withString:@""];
     NSLog(@"rURL=%@",rURL);






   NSString *rName=@"";
   [rScanner scanUpToString:@"</a>" intoString:&rName];
    rName=[courseName stringByReplacingOccurrencesOfString:@">" withString:@""];
    NSLog(@"rName=%@",rName);
    

    


    




It will print following results:


divID=topLeft
rURL=reetu/test/news.htm
rName=Latest News



Read More: https://developer.apple.com/library/ios/#documentation/Cocoa/Conceptual/Strings/Articles/Scanners.html


Thursday, March 22, 2012

Find size of rect to fit a string?

This has been a popular question of all time. Developers will face it sooner or later. You have a string which you want to display in your UILabel, but you want to keep the size of UILabel dynamic, you don't want to hard code its bounding rectangle because say, your string is dynamic.

NSString *myString=@"This is a dynamic string";// It will probably change after some event happens.


  UIFont *myFont=[UIFont fontWithName:@"Helvetica" size:10];
  CGSize size = [myString sizeWithFont:myFont forWidth:300 lineBreakMode:UILineBreakModeWordWrap];



Create the CGRect with the size calculated above.

  CGRect a=CGRectMake(50, 50, size.width, size.height);



Now you can create your label with size received above.


  UILabel *myLabel=[[UILabel alloc]initWithFrame:a];
  myLabel.text=myString;
  myLabel.font=[UIFont fontWithName:@"Helvetica" size:10];

Monday, June 13, 2011

Conversion between NSString and CFStringRef ?

Its a simple typecast conversion !


Convert NSString to CFStringRef


NSString *test = @"Everyone is created equal.";
CFStringRef string =  (CFStringRef) test;


Convert  CFStringRef to NSString

NSString *backToNSString =  (NSString *) string;


Tuesday, May 24, 2011

How to find if NSString has specific characters


 NSCharacterSet * set = [[NSCharacterSet characterSetWithCharactersInString:@"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLKMNOPQRSTUVWXYZ"] invertedSet];
    
    if ([strValue rangeOfCharacterFromSet:set].location == NSNotFound) 
        NSLog(@"character found");
    else

        NSLog(@"no character found");

Code above will find out whether NSString strValue has characters from set (abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLKMNOPQRSTUVWXYZ) . You can create your own own characters set with any values like even special characters(? >  etc).

Tuesday, April 5, 2011

Difference between NSString: isEqualTo and NSString: compare

isEqualTo is used for comparing the value of string.

for e.g.

NSString *a=@"Reetu";
NSString *b=@"Mike";
NSString *c=@"Mike";


if([a isEqualTo:b]) -> It will return No;
if([c isEqualTo:b]) -> It will return YES;

compare is used comparing(generally for sorting the string) by lexical order(in dictionary which string will come first).

if([a compare:b]) -> It will return 1;
if([b compare:a]) -> It will return -1;