Re: perl string handle equivilents in cocoa
Re: perl string handle equivilents in cocoa
- Subject: Re: perl string handle equivilents in cocoa
- From: Sherm Pendley <email@hidden>
- Date: Sat, 6 Aug 2005 14:53:41 -0400
On Aug 6, 2005, at 1:43 PM, Jeff Childers wrote:
in perl
$string = "aB,,c";
$string =~s/[^A-Za-z]//g;
$string = uc($string);
@str = split(//, $string);
$string = join(",",@str);
print $string;
>>A,B,C
Of the 5 divide methods in NSString I don't see one the will divide
each character to an array. I could bepass the regex with ifs' then
stringByAppendingString.
#import <Foundation/Foundation.h>
int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
// $string = "aB,,c"
NSString *string = [NSMutableString stringWithString:@"aB,,c"];
// $string =~ s/[^A-Za-z]//g
NSCharacterSet *theSet = [[NSCharacterSet
alphanumericCharacterSet] invertedSet];
NSRange theRange = [string rangeOfCharacterFromSet:theSet];
while (theRange.location != NSNotFound) {
[(NSMutableString*)string replaceCharactersInRange:theRange
withString:@""];
theRange = [string rangeOfCharacterFromSet:theSet];
}
// $string = uc($string)
string = [string uppercaseString];
// @str = split(//, $string)
const char *strChars = [string UTF8String];
NSMutableArray *str = [NSMutableArray arrayWithCapacity:[string
length]];
while (*strChars != 0) {
[str addObject:[NSString stringWithFormat:@"%c", *strChars]];
strChars++;
}
// $string = join(",", @str)
string = [str componentsJoinedByString:@","];
NSLog(@"%@", string);
[pool release];
return 0;
}
It seems like alot of work for something so easy in perl.
Heh, that's nothing. You should try using libregex (man 3 regex) some
time. :-/
sherm--
Cocoa programming in Perl: http://camelbones.sourceforge.net
Hire me! My resume: http://www.dot-app.org
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden