Remove all characters from string except 0-9 and decimals

Discussion in 'Mac Programming' started by abcdefg12345, Oct 2, 2014.

  1. abcdefg12345, Oct 2, 2014
    Last edited: Oct 2, 2014

    abcdefg12345 macrumors 6502

    abcdefg12345

    Joined:
    Jul 10, 2013
    #1
    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:@""];
     
  2. SomeWhores macrumors newbie

    Joined:
    May 11, 2011
    #2
    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
     

Share This Page