Objective c ObjectiveC和substringToIndex。与dot有关的问题
我对目标C不熟悉,在substringToIndex方面有一个奇怪的问题 背景: 该代码是从计算器显示中删除最后一个字符的函数的一部分。我需要能够在删除点时进行标记 我的代码是:Objective c ObjectiveC和substringToIndex。与dot有关的问题,objective-c,Objective C,我对目标C不熟悉,在substringToIndex方面有一个奇怪的问题 背景: 该代码是从计算器显示中删除最后一个字符的函数的一部分。我需要能够在删除点时进行标记 我的代码是: NSString *currentDisplay = self.display.text; NSString *lastChar = [currentDisplay substringToIndex: 1]; NSLog(@"lastChar -->%@<--",lastChar); NSString*c
NSString *currentDisplay = self.display.text;
NSString *lastChar = [currentDisplay substringToIndex: 1];
NSLog(@"lastChar -->%@<--",lastChar);
NSString*currentDisplay=self.display.text;
NSString*lastChar=[currentDisplay substringToIndex:1];
NSLog(@“lastChar-->%@如果要判断最后一个字符是否为“点”,请使用
如果要判断最后一个字符是否为“点”,请使用
这将有助于您:
BOOL flagIfDotIsDeleted = NO;
unichar leftmostChar = [currentDisplay characterAtIndex: currentDisplay.length - 1];
if (leftmostChar == '.')
{
flagIfDotIsDeleted = YES;
currentDisplay = [currentDisplay substringToIndex: currentDisplay.length - 2];
}
这将有助于您:
BOOL flagIfDotIsDeleted = NO;
unichar leftmostChar = [currentDisplay characterAtIndex: currentDisplay.length - 1];
if (leftmostChar == '.')
{
flagIfDotIsDeleted = YES;
currentDisplay = [currentDisplay substringToIndex: currentDisplay.length - 2];
}
这更简单,如果你想知道最后一个字符是否是点
if([currentDisplay hasSuffix:@"."])
这更简单,如果你想知道最后一个字符是否是点
if([currentDisplay hasSuffix:@"."])
您正在执行子字符串,而不是删除字符。因此“最后一个删除的字符”没有意义。(并且您应该调用变量“firstChar”,而不是“lastChar”,因为您得到的是字符串中的第一个字符。)天哪,我指的是第一个字符。我不应该在周末工作。谢谢,删除发生在稍后的子字符串相同的函数中,而不是删除字符。因此,“删除的最后一个字符”是没有意义的。(你应该调用变量“firstChar”,而不是“lastChar”,因为你得到的是字符串中的第一个字符。)天哪,我指的是第一个字符。我不应该在周末工作。感谢稍后在同一个函数中发生的删除操作+1更好!(因为SubstringToIndex为您创建了一个新的字符串对象,而这并没有)+1更好!(因为SubstringToIndex为您创建了一个新的字符串对象,而这没有)