views:

24

answers:

2

HI all! I am using message deleteCharactersInRange from NSMutableString. And there is a problem that this finction deletes range in a wrong way. Here is a sample of code that works wrong:

-(void) btnClick { NSRange deleteRange = NSMakeRange(0, 1); [valueStr deleteCharactersInRange:deleteRange]; [self ShowNumber]; }

I have a mutable string: "-21.256" and when I press the button btnClick it must delete "-" from the begining but it does it only after the 5th presses time. Ealier it worked fine, but now no. Help please, or what can I use instead this function? Thanx!

A: 

Your code should work just fine:

NSMutableString *string = [NSMutableString stringWithString:@"-21.256"];
NSLog(@"%@", string);
[string deleteCharactersInRange:NSMakeRange(0, 1)];
NSLog(@"%@", string);

results in:

-21.256
21.256

Your problem must be elsewhere.

greg
A: 

I think it will be interesting to you. I itinialized my string in a such way:

NSString *buf = nil;
buf = [NSString stringWithFormat:@"%14.5f", myCalculator.calcValue];

after that I add this string to my NSMutableString. And with string I did operations with the help of func:

[string deleteCharactersInRange:NSMakeRange(0, 1)];

But charachters were deleted only after the 6 or 7 cycle of clicking.

Solution:

The problem is in @"%14.5f" in this string we have: " -2.00000"instead of "-2.00000" so function does her work well, but it deleted white spaces instead of "-".

So we need to convert in such way: @"%f"

yozhik