Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

abcdefg12345

macrumors 6502
Original poster
Jul 10, 2013
281
86
i'm trying to copy strings from nspasteboard to nstextfield and exclude everything but numbers and decimals

Code:
NSPasteboard*mypasteboard = [NSPasteboard generalPasteboard];

NSString*mystring=[mypasteboard stringForType:NSPasteboardTypeString];

NSCharacterSet *doNotWant = [NSCharacterSet characterSetWithCharactersInString:@"abcdefghijklmnopqrstuvwxyz"];

mystring = [[mystring componentsSeparatedByCharactersInSet: doNotWant] componentsJoinedByString: @""];

that will exclude only the things i list which will still copy the things i don't list
Code:
NSPasteboard*mypasteboard = [NSPasteboard generalPasteboard];

NSString*mystring=[mypasteboard stringForType:NSPasteboardTypeString];

NSString *newString = [[mystring componentsSeparatedByCharactersInSet:
                            [[NSCharacterSet decimalDigitCharacterSet] invertedSet]]
                           componentsJoinedByString:@""];

that will only allow numbers and i need decimals with it

Code:
NSPasteboard*mypasteboard = [NSPasteboard generalPasteboard];

NSString*mystring=[mypasteboard stringForType:NSPasteboardTypeString];

NSCharacterSet *numbers = [[NSCharacterSet characterSetWithCharactersInString:@"0123456789."] invertedSet];

NSString *newvalue = [mystring stringByTrimmingCharactersInSet:numbers];

that will only allow numbers and decimals but it still copies characters if they are in the middle of a string

is there a way that i can numbers and digits and exclude everything else



edit


don't worry about it i think i solved it

PHP:
NSPasteboard*mypasteboard = [NSPasteboard generalPasteboard];
NSString*mystring=[mypasteboard stringForType:NSPasteboardTypeString];
NSString *newstring = [[mystring componentsSeparatedByCharactersInSet:
                                  [[NSCharacterSet characterSetWithCharactersInString:@"0123456789."]
                                   invertedSet]]
                                 componentsJoinedByString:@""];
 
Last edited:
In an effort to push using NSLocale and supporting multiple Locales I'd recommend

Obj-c
Code:
NSCharacterSet *decimalCharacterSet = [NSCharacterSet decimalDigitCharacterSet].invertedSet;
Swift
Code:
let set: NSCharacterSet = NSCharacterSet.decimalDigitCharacterSet().invertedSet

for the decimal number set and

Obj-c
Code:
NSString *decimalSeparator = [[NSLocale currentLocale] objectForKey:NSLocaleDecimalSeparator];
Swift
Code:
let localeSeparator = NSLocale.currentLocale().objectForKey(NSLocaleDecimalSeparator)

For number separator recognition. I feel its safer to use NSLocale for those kinds of things rather than making assumptions on my user base.

¯\_(ツ)_/¯ but that's just me
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.