在我的应用中,我有一个字符串:
“3022513240”
我想将其转换为:
(302)-251-3240
我该如何解决这个问题?
答案 0 :(得分:11)
我会这样做:
示例:
NSMutableString *stringts = [NSMutableString stringWithString:self.ts.text];
[stringts insertString:@"(" atIndex:0];
[stringts insertString:@")" atIndex:4];
[stringts insertString:@"-" atIndex:5];
[stringts insertString:@"-" atIndex:9];
self.ts.text = stringts;
希望这会有所帮助......
答案 1 :(得分:10)
这是一个Swift扩展,可将字符串格式化为10位数字的电话号码。
extension String {
public func toPhoneNumber() -> String {
return stringByReplacingOccurrencesOfString("(\\d{3})(\\d{3})(\\d+)", withString: "($1) $2-$3", options: .RegularExpressionSearch, range: nil)
}
}
例如:
let number = "1234567890"
let phone = number.toPhoneNumber()
print(phone)
// (123) 456-7890
已更新至Swift 3.0 :
extension String {
public func toPhoneNumber() -> String {
return self.replacingOccurrences(of: "(\\d{3})(\\d{3})(\\d+)", with: "($1) $2-$3", options: .regularExpression, range: nil)
}
}
答案 2 :(得分:2)
您可以使用this方式
NSError *aError = nil;
NBPhoneNumber *myNumber1 = [phoneUtil parse:@"6766077303" defaultRegion:@"AT" error:&aError];
if (aError == nil)
{
NSLog(@"isValidPhoneNumber ? [%@]", [phoneUtil isValidNumber:myNumber1] ? @"YES":@"NO");
NSLog(@"E164 : %@", [phoneUtil format:myNumber1 numberFormat:NBEPhoneNumberFormatE164]);
NSLog(@"INTERNATIONAL : %@", [phoneUtil format:myNumber1 numberFormat:NBEPhoneNumberFormatINTERNATIONAL]);
NSLog(@"NATIONAL : %@", [phoneUtil format:myNumber1 numberFormat:NBEPhoneNumberFormatNATIONAL]);
NSLog(@"RFC3966 : %@", [phoneUtil format:myNumber1 numberFormat:NBEPhoneNumberFormatRFC3966]);
}
else
{
NSLog(@"Error : %@", [aError localizedDescription]);
}
答案 3 :(得分:1)
您可以使用(\d{3})(\d{3})(\d{4})
这样的正则表达式,并将符合该模式的输入替换为($1)-$2-$3
答案 4 :(得分:0)
您可以使用RMPhoneFormat库格式化电话号码。 格式化应该复制您在“联系人”应用中看到的相同电话号码的内容。
答案 5 :(得分:0)
这是简单易行的
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
if (string.length) {
if (textField.text.length<=13) {
if (textField.text.length==3) {
NSString *tempStr=[NSString stringWithFormat:@"(%@)- ",textField.text];
textField.text=tempStr;
} else if (textField.text.length==8) {
NSString *tempStr=[NSString stringWithFormat:@"%@-",textField.text];
textField.text=tempStr;
}
} else {
return NO;
}
}
return YES;
}
答案 6 :(得分:0)
查找RMPhoneFormat,我用它很棒。
答案 7 :(得分:0)
我使用SHSPhoneNumberFormatter
执行此操作,因此我不必更改UITextField
进行设置:
let phoneNumberFormatter = SHSPhoneNumberFormatter()
phoneNumberFormatter.setDefaultOutputPattern("(###) ###-####")
收听UITextFieldTextDidChangeNotification
并运行以下内容:
let phoneDict = phoneNumberFormatter.valuesForString(phoneTextField.text)
phoneTextField.text = phoneDict["text"] as? String
答案 8 :(得分:0)
以下是将字符串转换为电话号码格式的代码
Swift 3
func PhoneNumberFormate( str : NSMutableString)->String{
str.insert("(", at: 0)
str.insert(")", at: 4)
str.insert("-", at: 8)
return str as String
}
使用
let nsMutableString = NSMutableString(string: "3022513240")
let strModify = self.PhoneNumberFormate(str: nsMutableString)
答案 9 :(得分:0)
Swift 4.2
extension String {
public func toPhoneNumber() -> String {
return self.replacingOccurrences(of: "(\\d{3})(\\d{3})(\\d+)", with: "$1-$2-$3", options: .regularExpression, range: nil)
}}
答案 10 :(得分:0)
Swift 4.2
处理字符串中可能包含或未包含格式或非数字字符的10和11位数字电话号码。
将处理:
结果:
代码:
extension String {
/// Handles 10 or 11 digit phone numbers
///
/// - Returns: formatted phone number or original value
public func toPhoneNumber() -> String {
let digits = self.digitsOnly
if digits.count == 10 {
return digits.replacingOccurrences(of: "(\\d{3})(\\d{3})(\\d+)", with: "($1)-$2-$3", options: .regularExpression, range: nil)
}
else if digits.count == 11 {
return digits.replacingOccurrences(of: "(\\d{1})(\\d{3})(\\d{3})(\\d+)", with: "$1($2)-$3-$4", options: .regularExpression, range: nil)
}
else {
return self
}
}
}
extension StringProtocol {
/// Returns the string with only [0-9], all other characters are filtered out
var digitsOnly: String {
return String(filter(("0"..."9").contains))
}
}
示例:
let num = "1234567890"
let formatted = num.toPhoneNumber()
// Formatted is "(123)-456-7890"