stringByReplacingOccurrencesOfString 不适用于 space,即 objective c 中的“”
stringByReplacingOccurrencesOfString is not working for space i.e, " " in objective c
- (void)contactPicker:(CNContactPickerViewController *)picker didSelectContactProperty:(CNContactProperty *)contactProperty;
{
[self dismissViewControllerAnimated:YES completion: nil];
CNLabeledValue *phoneNumberValue = contactProperty.value;
NSString *contactString = [phoneNumberValue valueForKey:@"_stringValue"];
contactString = [contactString stringByReplacingOccurrencesOfString:@"-" withString:@""];
contactString = [contactString stringByReplacingOccurrencesOfString:@" " withString:@""]; // This line of code is not working properly
contactString = [contactString stringByReplacingOccurrencesOfString:@"(" withString:@""];
contactString = [contactString stringByReplacingOccurrencesOfString:@")" withString:@""];
txtRecipient.text = contactString;
}
这是我的代码。我正在使用 contactPicker 从电话簿中选择联系人。然后我将它存储到一个字符串变量中。之后,我使用 stringByReplacingOccurrencesOfString 从字符串值中删除破折号、括号和空格。除这一行外,一切正常:
contactString = [contactString stringByReplacingOccurrencesOfString:@" " withString:@""];
在这行代码之后,contactString 保持不变,即没有从字符串中删除空格。我也试过 componentsSeparatedByString
函数,但它只返回 1 个字符。即,
NSArray *components = [dateString componentsSeparatedByString:@" "];
components.length 返回 1。
希望你能理解我的问题。还有其他方法可以从字符串中删除空格吗?任何形式的帮助都将不胜感激。谢谢。
这看起来不标准space。
试试这个:
NSMutableCharacterSet* set = [NSMutableCharacterSet whitespaceCharacterSet];
[set addCharactersInString:@"()-"];
NSMutableString * contactString = [[phoneNumberValue valueForKey:@"_stringValue"] mutableCopy];
NSRange range;
while ((range = [contactString rangeOfCharacterFromSet:set]).location!=NSNotFound) {
[contactString deleteCharactersInRange:range];
}
- (void)contactPicker:(CNContactPickerViewController *)picker didSelectContactProperty:(CNContactProperty *)contactProperty;
{
[self dismissViewControllerAnimated:YES completion: nil];
CNLabeledValue *phoneNumberValue = contactProperty.value;
NSString *contactString = [phoneNumberValue valueForKey:@"_stringValue"];
contactString = [contactString stringByReplacingOccurrencesOfString:@"-" withString:@""];
contactString = [contactString stringByReplacingOccurrencesOfString:@" " withString:@""]; // This line of code is not working properly
contactString = [contactString stringByReplacingOccurrencesOfString:@"(" withString:@""];
contactString = [contactString stringByReplacingOccurrencesOfString:@")" withString:@""];
txtRecipient.text = contactString;
}
这是我的代码。我正在使用 contactPicker 从电话簿中选择联系人。然后我将它存储到一个字符串变量中。之后,我使用 stringByReplacingOccurrencesOfString 从字符串值中删除破折号、括号和空格。除这一行外,一切正常:
contactString = [contactString stringByReplacingOccurrencesOfString:@" " withString:@""];
在这行代码之后,contactString 保持不变,即没有从字符串中删除空格。我也试过 componentsSeparatedByString
函数,但它只返回 1 个字符。即,
NSArray *components = [dateString componentsSeparatedByString:@" "];
components.length 返回 1。
希望你能理解我的问题。还有其他方法可以从字符串中删除空格吗?任何形式的帮助都将不胜感激。谢谢。
这看起来不标准space。 试试这个:
NSMutableCharacterSet* set = [NSMutableCharacterSet whitespaceCharacterSet];
[set addCharactersInString:@"()-"];
NSMutableString * contactString = [[phoneNumberValue valueForKey:@"_stringValue"] mutableCopy];
NSRange range;
while ((range = [contactString rangeOfCharacterFromSet:set]).location!=NSNotFound) {
[contactString deleteCharactersInRange:range];
}