-3

I am receiving data in the form of a string from a database like so:

NSString *strTemp = @"000014";

I need to add 1 to the above,
like 000014 + 1 = 000015 000015 + 1 = 000016 depending on some count.

like:

for(int i =1; i < 5; i++)
{
   int iTemp = [strTemp intValue] +i;  // here i am getting 15 as int no not like 000015, 000016, 000017 ETC
} 

I even tried NSString *strWarNo = [NSString stringWithFormat:@"%07d", iTemp]; but leading numbers may not be constant ...like string from db will be anything @"0023" or @"0000056".

I need to add one to while number, not only integer number.

How I can achieve this?

user2813740
  • 517
  • 1
  • 7
  • 29

4 Answers4

1

You can have a category over NSString to achieve this. I tried following code and its working fine.

.h of the category

#import <Foundation/Foundation.h>

@interface NSString (Arithematic)
- (NSString*)stringByAddingNumber:(NSInteger)number;
@end

.m of the category

#import "NSString+Arithematic.h"

@implementation NSString (Arithematic)
- (NSString*)stringByAddingNumber:(NSInteger)number
{
    //To keep the width same as original string
    int numberLength = (int)self.length;

    NSInteger originalNumber = self.integerValue;

    originalNumber += number;

    NSString *resultString = [NSString stringWithFormat:@"%0*ld",numberLength,originalNumber];

    return resultString;
}
@end

Usage:

NSString *strTemp = @"000014";
for(int i =1; i < 5; i++)
{
    strTemp = [strTemp stringByAddingNumber:i];
}
NSLog(@"result %@",strTemp);

Hope this helps.

Anoop Nyati
  • 339
  • 2
  • 11
0

There is a simple way of doing this by converting string into NSInteger and then add your desired value into it and then convert the integer into string by appending you desired amount of zeros before then number.

-(NSString *)addNumber:(NSInteger)number toMyString:(NSString)myNum{
    NSInteger numb = myString.integerValue;
    numb += myNum;
    NSString myNewString = [NSString stringWithFormat:@"0%d",myNewString];
    return myNum;
}
Syed Qamar Abbas
  • 3,637
  • 1
  • 28
  • 52
0

If you want to format numbers with leading zeros then use NSNumberFormatter.

Store the number as a number (not a String) and then treat it like a number.

When you need to display it use an NSNumberFormatter to add leading zeros.

You can read more about NSNumberFormatter and adding leading zeros here... NSNumberFormatter leading 0's and decimals

Community
  • 1
  • 1
Fogmeister
  • 76,236
  • 42
  • 207
  • 306
-1

Add which number you have to add in "num" and pass your number string "strNumber"

EX . [self addNumber:10 andYourString:@"000014"];

-(void)addNumber:(int)num andYourString:(NSString *)strNumber {

    NSString * str_your_number = strNumber;

    NSString * prevous_char = [NSString stringWithFormat:@"%c", 
[str_your_number characterAtIndex:0]];

    int number_zero = 0;

    BOOL is_leading_zero = true;

    for(int i = 0 ; i < str_your_number.length ; i++){
        NSString *theCharacter = [NSString stringWithFormat:@"%c", [str_your_number characterAtIndex:i]];
        if(is_leading_zero == true){
            if([prevous_char isEqualToString:theCharacter]) {
                number_zero++;
                prevous_char = theCharacter;
            }
            else {
                is_leading_zero = false;
            }
        }
        NSLog(@"%@",theCharacter);
    }

    NSLog(@"%d",number_zero);

    NSInteger you_numb = strNumber.integerValue;
    you_numb += num;

    NSString * str_zero = @"";
    for(int j = 0 ; j < number_zero ; j++) {
        str_zero = [str_zero stringByAppendingString:@"0"];
    }

    NSString * final_str = [NSString stringWithFormat:@"%@%ld",str_zero,(long)you_numb];
    NSLog(@"%@",final_str);
}

it work...

Ashley Mills
  • 50,474
  • 16
  • 129
  • 160
yogesh
  • 138
  • 1
  • 8