1

我想在单击按钮时从文本字段中删除一个字符。当我从最后一个位置删除字符时,这对我有用,但无法从随机位置删除字符。谁能帮我解决这个问题

编辑

目前我有:

- (IBAction)clearTextKeyPressed:(id)sender {
  NSString *enteredText = [NSString stringWithFormat:@"%@", self.targetTextField.text];
  if ( [enteredText length] > 0) {
      enteredText = [enteredText substringToIndex:[enteredText length] - 1];
      self.targetTextField.text = [NSString stringWithFormat:@"%@", enteredText];
      [self.numericGridKeypadDelegate textFieldDidChange:self.targetTextField];        
  }
}

使用这个我可以删除最后一个位置的字符,我也想从任何位置删除字符

注意 字符仅是 0-9 的数字

4

6 回答 6

2

干得好。不过,我会将其归为 NSString 的一个类别。

- (NSString*)deleteCharacterAtPosition:(int)position inString:(NSString*)myString {
    NSMutableString *mutableString = [myString mutableCopy];
    [mutableString deleteCharactersInRange:NSMakeRange(position, 1)];
    return mutableString;
}

如果您不想删除任何数字。然后,您应该在调用此函数之前检查该位置是否有非数字字符。

或者你可以这样做:

- (NSString*)deleteCharacterAtPosition:(int)position inString:(NSString*)myString {
    NSRange range;
    range.location = position;
    range.length = 1;
    return [test stringByReplacingCharactersInRange:range withString:@""];
}

这是检查位置是否为数字的方法:

- (bool)isCharacterAtPositionADigit:(int)position inString:(NSString*)myString {
    unichar c = [myString characterAtIndex:position];
    NSCharacterSet *numericSet = [NSCharacterSet decimalDigitCharacterSet];
    if ([numericSet characterIsMember:c]) {
        return true;
    }
    return false;
}

现在这就是你想要的:

- (IBAction)clearTextKeyPressed:(id)sender {
  NSString *enteredText = [NSString stringWithFormat:@"%@", self.targetTextField.text];
  int positionToDelete = 3;
  if ( [enteredText length] > 0 && [enteredText length] >= positionToDelete) {
      if (![self isCharacterAtPositionADigit:positionToDelete inString:enteredText])
          return;
      NSString* finalText = [self deleteCharacterAtPosition:positionToDelete inString:enteredText];
      self.targetTextField.text = finalText;
      [self.numericGridKeypadDelegate textFieldDidChange:self.targetTextField];        
  }
}
于 2013-09-30T13:27:21.160 回答
1

用这个:

NSString *stringWithoutSpaces = [textField.text stringByReplacingOccurrencesOfString:@"a" withString:@"b"]
于 2013-09-30T11:59:43.447 回答
1

像这样试试

NSString *string = [textField.text stringByReplacingCharactersInRange:NSMakeRange(3, 1) withString:@"A"];
于 2013-09-30T12:02:12.613 回答
1

您需要用空白搅拌代替。像这样..

NSString *string = [textField.text stringByReplacingCharactersInRange:NSMakeRange(1, 1) withString:@""];

例如看这个例子

 NSString *str = @"Hello";

    NSRange range = [str rangeOfString:@"e"];

    str = [str stringByReplacingCharactersInRange:range withString:@""];

    NSLog(@"String after replacement %@",str);
于 2013-09-30T12:06:25.300 回答
0

NSCharacterSet 很酷

 NSString* testString = @"This is buggy, buggy says hi, this is buggy when a car comes by.";
 NSString* firstCharacter = [testString substringToIndex:1];
 NSString* lastCharacter = [testString substringWithRange:NSRangeMake(testString.length - 1, 1)];
 NSCharacterSet* customCharacterSet = [NSCharacterSet characterSetWithCharactersInString:[NSString stringWithFormat:@"%@%@", firstCharacter, lastCharacter]];
 testString = [testString stringByTrimmingCharactersInSet:customCharacterSet];
于 2013-09-30T12:49:39.633 回答
0

使用获取我要删除的数字的偏移量来解决此问题:

        UITextRange *selRange = self.targetTextField.selectedTextRange;
        UITextPosition *selStartPos = selRange.start;
        NSInteger idx = [self.targetTextField offsetFromPosition:self.targetTextField.beginningOfDocument toPosition:selStartPos];
        if (idx != 0) {
          enteredText =  [enteredText stringByReplacingCharactersInRange:NSMakeRange(idx-1, 1) withString:@""];
          self.targetTextField.text = [NSString stringWithFormat:@"%@", enteredText];
        }

谢谢大家的帮助。

于 2013-09-30T13:31:23.573 回答