【问题标题】:how to remove ( from string [duplicate]如何从字符串中删除([重复]
【发布时间】:2013-01-16 04:18:31
【问题描述】:

可能重复:
iphone sdk - Remove all characters except for numbers 0-9 from a string

我有一个带有电话号码的字符串

我的号码 = @"(334)687-6619";

我正在使用

NSString *phoneURLString = [NSString stringWithFormat:@"tel:%@", phoneNumber];
NSURL *phoneURL = [NSURL URLWithString:phoneURLString];
[[UIApplication sharedApplication] openURL:phoneURL];

拨打电话。 但是由于“(”,“)”,我的方法无法拨打电话。

请建议我做一个正确的字符串 像

“1-800-555-1212”或类似的东西。

谢谢

【问题讨论】:

  • @CarlVeazey 你确定这是重复的。其他线程也从字符串中删除了“-”。
  • 我得到了我所期望的结果。
  • 哎呀.. @Carl Veazey 你说得对..

标签: iphone ipad


【解决方案1】:

你可以使用

NSString *s = @"(334)687-6619";
NSCharacterSet *doNotWant = [NSCharacterSet characterSetWithCharactersInString:@"("];
s = [[s componentsSeparatedByCharactersInSet: doNotWant] componentsJoinedByString: @""];

s = [s stringByReplacingOccurrencesOfString:@")" withString:@"-"];
NSLog(@"%@", s);

所以,它将用“”替换“(”,用“-”替换“)”。

谢谢。

【讨论】:

  • 如果您分两步进行(这本身就是多余的),您甚至不必使用字符集和拆分并连接技巧,而是只需使用 @987654322 @.
  • 好的,我的朋友,它正在工作。问题是关于替换字符串。所以我放了。
  • s = [s stringByReplacingOccurrencesOfString:@")" withString:@"-"];也在工作。谢谢队友..:)
  • 我认为@iManan 是对的。它会工作
【解决方案2】:

使用此代码

NSString *str = @"(334)687-6619";
str = [str stringByReplacingCharactersInRange:NSMakeRange(0, 1) withString:@""];
str = [str stringByReplacingCharactersInRange:NSMakeRange(3, 1) withString:@"-"];    
NSLog(@"%@",str);

输出为: 334-687-6619

【讨论】:

  • 不错的答案@paras joshi....
【解决方案3】:

从字符串中删除字符:

NSCharacterSet *set = [NSCharacterSet characterSetWithCharactersInString:@"()"];
NSArray *comps = [string componentsSeparatedByCharactersInSet:set];
NSString *newString = [comps componentsJoinedByString:@""];

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多