我想在按钮点击时从文本字段中删除字符。当我从最后一个位置移除字符时,这对我有用,但无法从随机位置移除字符。 任何人都可以帮我解决这个问题
修改
目前我有:
- (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的数字
答案 0 :(得分: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];
}
}
答案 1 :(得分:1)
使用此:
NSString *stringWithoutSpaces = [textField.text stringByReplacingOccurrencesOfString:@"a" withString:@"b"]
答案 2 :(得分:1)
试试这个
NSString *string = [textField.text stringByReplacingCharactersInRange:NSMakeRange(3, 1) withString:@"A"];
答案 3 :(得分: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);
答案 4 :(得分: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];
答案 5 :(得分: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];
}
谢谢大家的帮助。