Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8827887
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T07:28:51+00:00 2026-06-14T07:28:51+00:00

Possible Duplicate: Removing leading zeroes from a string I need to remove leading 0s

  • 0

Possible Duplicate:
Removing leading zeroes from a string

I need to remove leading 0s in an NSString, a quick way I can think of is to convert the string to an NSNumber then convert back to NSString which will give me the clean string, although I’m not sure if it works. Is there any other way to do this?

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T07:28:52+00:00Added an answer on June 14, 2026 at 7:28 am

    So just for fun, I decided to time the various ways of doing this. This was by no means a scientific test, done mostly for my amusement, but some of you might like to see it anyway.

    I created a method and substituted the various routines to process strings representing the numbers 0 – 100,000 all with three leading 0’s.

    Keep in mind however, that even the slowest method is going to be perfectly acceptable if you only have one (or even one hundred) of these strings to trim. (Using the number formatter takes about .000012039905 seconds, or about 1/100th of a millisecond.) Other things such as how easy your code is to read and understand is usually more important unless you really do need to process a large file full of strings like this. My personal favorite is still the regular expression, because it is relatively fast and immediately obvious what you are trying to accomplish, even without documentation.

    Here are the results (from fastest to slowest):

    Looping through the string

    // I tried this by looping through the utf8String and got the same times
    // (actually, ever so slightly longer, probably since it had to create the c string)
    //
    // I did find that using `[string getCharacters:buffer range:range]` and
    // iterating over the character buffer that it took another 0.01 seconds
    // off the time.  Hardly worth it though.  :)
    NSUInteger length = [string length];
    for (NSUInteger i = 0; i < length; i++)
    {
        if ([string characterAtIndex:i] != '0')
        {
            return [string substringFromIndex:i];
        }
    }
    return @"0";
    
    // Time 1: 0.210126
    // Time 2: 0.219159
    // Time 3: 0.201496
    

    Converting to an int and then back to a NSString

    int num = [string intValue];
    return [NSString stringWithFormat:@"%d", num];
    
    // Time 1: 0.322206
    // Time 2: 0.345259
    // Time 3: 0.324954
    
    long long num = [string longLongValue];
    return [NSString stringWithFormat:@"%lld", num];
    
    // Time 1: 0.364318
    // Time 2: 0.344946
    // Time 3: 0.364761
    // These are only slightly slower, but you can do bigger numbers using long long
    

    Using a NSScanner

    NSScanner *scanner    = [NSScanner scannerWithString:string];
    NSCharacterSet *zeros = [NSCharacterSet
                             characterSetWithCharactersInString:@"0"];
    [scanner scanCharactersFromSet:zeros intoString:NULL];
    
    return [string substringFromIndex:[scanner scanLocation]];
    
    // Time 1: 0.505277
    // Time 2: 0.481884
    // Time 3: 0.487209
    

    Using a regular expression

    NSRange range = [string rangeOfString:@"^0*" options:NSRegularExpressionSearch];
    return [string stringByReplacingCharactersInRange:range withString:@""];
    
    // Time 1: 0.610879
    // Time 2: 0.645335
    // Time 3: 0.637690
    

    Using a static number formatter

    static NSNumberFormatter *formatter = nil;
    if (formatter == nil)
    {
        formatter             = [NSNumberFormatter new];
        formatter.numberStyle = NSNumberFormatterDecimalStyle;
    }
    
    NSNumber *number = [formatter numberFromString:string];
    return [formatter stringFromNumber:number];
    
    // Time 1: 1.774198
    // Time 2: 1.753013
    // Time 3: 1.753893
    

    Using a number formatter

    NSNumberFormatter *formatter = [NSNumberFormatter new];
    formatter.numberStyle        = NSNumberFormatterDecimalStyle;
    
    NSNumber *number             = [formatter numberFromString:string];
    return [formatter stringFromNumber:number];
    
    // Time 1: 11.978336
    // Time 2: 12.039905
    // Time 3: 11.904984
    // No wonder Apple recommends reusing number formatters!
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Removing leading zeroes from a string I need to remove 0 from
Possible Duplicate: Removing multiple classes (jQuery) how can we remove multiple classes from one
Possible Duplicate: Removing last comma in PHP? Remove the last Comma from a string
Possible Duplicate: Removing an element from an Array (Java) How to remove specific String
Possible Duplicate: Removing carriage return and new-line from the end of a string in
Possible Duplicate: Removing HTML from a Java String I am having a problem removing
Possible Duplicate: How can I removing escape characters using php? I have a string
Possible Duplicate: How can I convert a list<> to a multi-dimensional array? I want
Possible Duplicate: git - removing a file from source control (but not from the
Possible Duplicate: git - removing a file from source control (but not from the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.