I'm trying to learn more about string manipulation and want to be able to extract a single character from a string. I think I need to use subStringWithRange (exacting from and to the same character location) but I'm not sure and I'm also unsure how to code it.

Basically I'm after the equivalent of the old-fashioned BASIC command MID$ [ i.e. MID$("This is the sentence to check",4,1) to get character 4. ]

The following code works fine except for the..

NSLog(@"Character %i of %@ is %@",i,pass1,[pass1 substringWithRange: 1,2]);

...line simply because I don't know how to code that line. I think it's something to do with how to set the range that I'm getting wrong.

Can anyone please advise what than line is supposed to be to pull out the correct character?

Thanks.

Phil


{
        NSMutableString *pass1;
        int i;
        
        pass1=@"This is the sentence to check";
        
        NSLog(@"length of %@ is %i",pass1,[pass1 length]);
        
        for (i=1;i<=[pass1 length];i++)
        {
                // This displays characters UP to character 'i'
NSLog(@"Characters up to %i of %@ are %@",i,pass1,[pass1 substringToIndex:i]);
                
                // This is SUPPOSED to display character number 'i' ONLY
NSLog(@"Character %i of %@ is %@",i,pass1,[pass1 substringWithRange:i,i]);
        }
        


}
_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to [EMAIL PROTECTED]

Reply via email to