开发者

Problem with NSString and NSRange

开发者 https://www.devze.com 2023-03-09 12:41 出处:网络
I\'m having a rather annoying problem 开发者_开发技巧here. See, I\'m trying to break up a string that I get into individual characters and symbols. The string is always in the form of an equation, lik

I'm having a rather annoying problem 开发者_开发技巧here. See, I'm trying to break up a string that I get into individual characters and symbols. The string is always in the form of an equation, like "3x+4" or "x/7+5". I need to separate the string into an array of individual strings. For example, if I had the first equation, I would want to have an NSMutableArray that has "3", "x", "+", and "4". Here is the section of code that I use:

NSMutableArray* list = [[NSMutableArray alloc] initWithCapacity:10];
for (int i = 0; i < [self.equationToGuess length]; i++) {
    NSRange range = {i, i};
    NSString* string= [[NSString alloc] initWithString:[self.equationToGuess substringWithRange:range]];
    [list addObject:string];
}

I've made sure to check if self.equationToGuess always contains an equation using the debugger, and it does. list is also able to get some of the objects, but the problem is that it just puts the last two characters in one shelf on the list. So if I have that "3x+4" equation, this chunk of code puts "3", "x", and "+4" into the code, and then it crashes because it goes beyond the length of the string. Does anyone know how to fix this?


The two values in NSRange are not the starting and ending index. Rather, the first is the starting index and the second is the length of the range. So instead you want your range to be

NSRange range = NSMakeRange(i, 1);


Let's do this with a bit more panache:

NSInteger numberOfCharacters = [self.equationToGuess length];
NSMutableArray *characterArray = [[NSMutableArray alloc] initWithCapacity:numberOfCharacters];
for (NSUInteger idx = 0; idx < numberOfCharacters; idx++) {
    NSRange range = NSMakeRange(idx, 1);
    [characterArray addObject:[self.equationToGuess substringWithRange:range]];
}

Edit

After a hearty helping of humble pie - this is still not the best way to do it: if your equation has multi-digit coefficients, they will be split up. Have you considered using NSScanner to split the string up instead?


you could also use an alternative solution by getting characters from you string , for that you will have to use the below function of NSString.

- (unichar)characterAtIndex:(NSUInteger)index

.

NSMutableArray* list = [[NSMutableArray alloc] initWithCapacity:10];
NSString* string;
for (int i = 0; i < [self.equationToGuess length]; i++) 
{
    string = [[NSString alloc] initWithFormat:@"%c",[self.equationToGuess characterAtIndex:i]];
    [list addObject:string];
    [string release];
     string  = nil ;
}


NSInteger numberOfCharacters = [self.equationToGuess length];
NSMutableArray *characterArray = [[NSMutableArray alloc] initWithCapacity:numberOfCharacters];
for (NSUInteger idx = 0; idx < numberOfCharacters; idx++) {
[characterArray addObject:[NSString stringWithFormat:@"%c", [self.equationToGuess characterAtIndex:idx]]];
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号