Phone number formatting on iOS - objective-c

I have a text field where the user enters data. It's a phone number field. If the user enters 1234567890, I want it displayed as (123)-(456)-7890 as the user types. How is this possible?

This will help you
Format (xxx) xxx-xxxx
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
int length = (int)[self getLength:textField.text];
//NSLog(#"Length = %d ",length);
if(length == 10)
{
if(range.length == 0)
return NO;
}
if(length == 3)
{
NSString *num = [self formatNumber:textField.text];
textField.text = [NSString stringWithFormat:#"(%#) ",num];
if(range.length > 0)
textField.text = [NSString stringWithFormat:#"%#",[num substringToIndex:3]];
}
else if(length == 6)
{
NSString *num = [self formatNumber:textField.text];
//NSLog(#"%#",[num substringToIndex:3]);
//NSLog(#"%#",[num substringFromIndex:3]);
textField.text = [NSString stringWithFormat:#"(%#) %#-",[num substringToIndex:3],[num substringFromIndex:3]];
if(range.length > 0)
textField.text = [NSString stringWithFormat:#"(%#) %#",[num substringToIndex:3],[num substringFromIndex:3]];
}
return YES;
}
- (NSString *)formatNumber:(NSString *)mobileNumber
{
mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:#"(" withString:#""];
mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:#")" withString:#""];
mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:#" " withString:#""];
mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:#"-" withString:#""];
mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:#"+" withString:#""];
NSLog(#"%#", mobileNumber);
int length = (int)[mobileNumber length];
if(length > 10)
{
mobileNumber = [mobileNumber substringFromIndex: length-10];
NSLog(#"%#", mobileNumber);
}
return mobileNumber;
}
- (int)getLength:(NSString *)mobileNumber
{
mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:#"(" withString:#""];
mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:#")" withString:#""];
mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:#" " withString:#""];
mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:#"-" withString:#""];
mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:#"+" withString:#""];
int length = (int)[mobileNumber length];
return length;
}

This felt more clear and handles removing any unwanted characters much more nicely. Formats correctly for 1 (###) ###‑#### or (###) ###‑####
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
NSString *newString = [textField.text stringByReplacingCharactersInRange:range withString:string];
NSArray *components = [newString componentsSeparatedByCharactersInSet:[[NSCharacterSet decimalDigitCharacterSet] invertedSet]];
NSString *decimalString = [components componentsJoinedByString:#""];
NSUInteger length = decimalString.length;
BOOL hasLeadingOne = length > 0 && [decimalString characterAtIndex:0] == '1';
if (length == 0 || (length > 10 && !hasLeadingOne) || (length > 11)) {
textField.text = decimalString;
return NO;
}
NSUInteger index = 0;
NSMutableString *formattedString = [NSMutableString string];
if (hasLeadingOne) {
[formattedString appendString:#"1 "];
index += 1;
}
if (length - index > 3) {
NSString *areaCode = [decimalString substringWithRange:NSMakeRange(index, 3)];
[formattedString appendFormat:#"(%#) ",areaCode];
index += 3;
}
if (length - index > 3) {
NSString *prefix = [decimalString substringWithRange:NSMakeRange(index, 3)];
[formattedString appendFormat:#"%#-",prefix];
index += 3;
}
NSString *remainder = [decimalString substringFromIndex:index];
[formattedString appendString:remainder];
textField.text = formattedString;
return NO;
}

The code bellow is what I typically use. The format is different but you get the picture. This will handle input such as '123df#$#$gdfg45-+678dfg901' and output '1 (234) 567-8901'
#import "NSString+phoneNumber.h"
#implementation NSString (phoneNumber)
-(NSString*) phoneNumber{
static NSCharacterSet* set = nil;
if (set == nil){
set = [[NSCharacterSet decimalDigitCharacterSet] invertedSet];
}
NSString* phoneString = [[self componentsSeparatedByCharactersInSet:set] componentsJoinedByString:#""];
switch (phoneString.length) {
case 7: return [NSString stringWithFormat:#"%#-%#", [phoneString substringToIndex:3], [phoneString substringFromIndex:3]];
case 10: return [NSString stringWithFormat:#"(%#) %#-%#", [phoneString substringToIndex:3], [phoneString substringWithRange:NSMakeRange(3, 3)],[phoneString substringFromIndex:6]];
case 11: return [NSString stringWithFormat:#"%# (%#) %#-%#", [phoneString substringToIndex:1], [phoneString substringWithRange:NSMakeRange(1, 3)], [phoneString substringWithRange:NSMakeRange(4, 3)], [phoneString substringFromIndex:7]];
case 12: return [NSString stringWithFormat:#"+%# (%#) %#-%#", [phoneString substringToIndex:2], [phoneString substringWithRange:NSMakeRange(2, 3)], [phoneString substringWithRange:NSMakeRange(5, 3)], [phoneString substringFromIndex:8]];
default: return nil;
}
}
#end

We wrote a custom NSFormatter subclass for phone numbers here: https://github.com/edgecase/PhoneNumberFormatter
You can use it like any other NSFormatter subclass.

Thanks so much for the first answer but, i think, that method -(int)getLength:(NSString*)mobileNumber is useless. You can try something like below:
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
int length = [[self formatNumber:[textField text]] length];
if (length == 10) {
if(range.length == 0) {
return NO;
}
}
if (length == 3) {
NSString *num = [self formatNumber:[textField text]];
textField.text = [NSString stringWithFormat:#"(%#) ",num];
if (range.length > 0) {
[textField setText:[NSString stringWithFormat:#"%#",[num substringToIndex:3]]];
}
}
else if (length == 6) {
NSString *num = [self formatNumber:[textField text]];
[textField setText:[NSString stringWithFormat:#"(%#) %#-",[num substringToIndex:3],[num substringFromIndex:3]]];
if (range.length > 0) {
[textField setText:[NSString stringWithFormat:#"(%#) %#",[num substringToIndex:3],[num substringFromIndex:3]]];
}
}
return YES;
}
- (NSString*)formatNumber:(NSString*)mobileNumber {
mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:#"(" withString:#""];
mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:#")" withString:#""];
mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:#" " withString:#""];
mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:#"-" withString:#""];
mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:#"+" withString:#""];
int length = [mobileNumber length];
if (length > 10) {
mobileNumber = [mobileNumber substringFromIndex: length-10];
}
return mobileNumber;
}

For those of you who require international number formatting: https://code.google.com/p/libphonenumber/
Comes with C++, Java and JavaScript implementations. Should be easy to wrap the C++ implementations in a .mm file and write a small Objective-C wrapper around it.

A valid option is https://github.com/iziz/libPhoneNumber-iOS
All the other answer cover only a small part of the possibilities and combinations, this library actually parse and validate EVERY phone number, and identify:
Nationality
phone number type
National carrier

Relevant for U.S. Phone Numbers:
Adding to #wan's post, I added a conditional statement if the user starts with the country code (1). That way, it will format to be: 1 (XXX) XXX-XXXX instead of (1XX) XXX-XXXX.
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
textField = self.phoneNumberTextField;
NSInteger length = [self getLength:textField.text];
//NSLog(#"Length = %d ",length);
if ([textField.text hasPrefix:#"1"]) {
if(length == 11)
{
if(range.length == 0)
return NO;
}
if(length == 4)
{
NSString *num = [self formatNumber:textField.text];
textField.text = [NSString stringWithFormat:#"%# (%#) ",[num substringToIndex:1],[num substringFromIndex:1]];
if(range.length > 0)
textField.text = [NSString stringWithFormat:#"%#",[num substringToIndex:4]];
}
else if(length == 7)
{
NSString *num = [self formatNumber:textField.text];
NSRange numRange = NSMakeRange(1, 3);
textField.text = [NSString stringWithFormat:#"%# (%#) %#-",[num substringToIndex:1] ,[num substringWithRange:numRange],[num substringFromIndex:4]];
if(range.length > 0)
textField.text = [NSString stringWithFormat:#"(%#) %#",[num substringToIndex:3],[num substringFromIndex:3]];
}
} else {
if(length == 10)
{
if(range.length == 0)
return NO;
}
if(length == 3)
{
NSString *num = [self formatNumber:textField.text];
textField.text = [NSString stringWithFormat:#"(%#) ",num];
if(range.length > 0)
textField.text = [NSString stringWithFormat:#"%#",[num substringToIndex:3]];
}
else if(length == 6)
{
NSString *num = [self formatNumber:textField.text];
textField.text = [NSString stringWithFormat:#"(%#) %#-",[num substringToIndex:3],[num substringFromIndex:3]];
if(range.length > 0)
textField.text = [NSString stringWithFormat:#"(%#) %#",[num substringToIndex:3],[num substringFromIndex:3]];
}
}
return YES;
}
-(NSString*)formatNumber:(NSString*)mobileNumber
{
mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:#"(" withString:#""];
mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:#")" withString:#""];
mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:#" " withString:#""];
mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:#"-" withString:#""];
mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:#"+" withString:#""];
NSLog(#"%#", mobileNumber);
NSInteger length = [mobileNumber length];
if(length > 10)
{
mobileNumber = [mobileNumber substringFromIndex: length-10];
NSLog(#"%#", mobileNumber);
}
return mobileNumber;
}
-(NSInteger)getLength:(NSString*)mobileNumber
{
mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:#"(" withString:#""];
mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:#")" withString:#""];
mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:#" " withString:#""];
mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:#"-" withString:#""];
mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:#"+" withString:#""];
NSInteger length = [mobileNumber length];
return length;
}

Maybe you could use this simple method:
+ (NSString*) formatPhoneNumber:(NSString *)phoneNumber codeLength:(int) code segmentLength:(int) segment
{
NSString* result = #"";
int length = [phoneNumber length];
NSString* firstSegment = #"";
NSString* restSegment = #"";
for (int i=0; i<length; i++) {
char c = [phoneNumber characterAtIndex:i];
if(i < code)
firstSegment = [firstSegment stringByAppendingFormat:#"%c", c];
else
{
restSegment = [restSegment stringByAppendingFormat:#"%c", c];
int threshold = (i - code) + 1;
if((threshold % segment == 0) && (threshold > 0) && !(threshold > length))
restSegment = [restSegment stringByAppendingFormat:#"%c", '-'];
}
}
result = [result stringByAppendingFormat:#"%#-%#", firstSegment, restSegment];
return result;
}
Assuming above method is in Contact class, just use the method like this:
NSString* phoneNumber = #"085755023455";
NSString* formattedNumber = [Contact formatPhoneNumber:phoneNumber codeLength:3 segmentLength:4];
That would be resulting something like:
085-7550-2345-5

You can use AKNumericFormatterlibrary for it. It has formatter and convenient UITextField category, it's available as a cocoapod.

C# Xamarin.iOS version of most comprehensive answer on how to do phone formatting in iOS is below
public override void ViewDidLoad()
{
base.ViewDidLoad();
PhoneNumberTextField.ShouldChangeCharacters = ChangeCharacters;
}
private bool ChangeCharacters(UITextField textField, NSRange range, string replacementString)
{
var text = textField.Text;
var newString = text.Substring(0, range.Location) + replacementString + text.Substring(range.Location + range.Length);
var decimalString = Regex.Replace(newString, #"[^\d]", string.Empty);
var length = decimalString.Length;
var hasLeadingOne = length > 0 && decimalString[0] == '1';
if ((length == 0) || (length > 10 && !hasLeadingOne) || (length > 11))
{
textField.Text = decimalString;
return false;
}
var index = 0;
var formattedString = "";
if (hasLeadingOne)
{
formattedString += "1";
index += 1;
}
if (length - index > 3)
{
var areaCode = decimalString.Substring(index, 3);
formattedString += "(" + areaCode + ")";
index += 3;
}
if (length - index > 3)
{
var prefix = decimalString.Substring(index, 3);
formattedString += " " + prefix + "-";
index += 3;
}
var remainder = decimalString.Substring(index);
formattedString += remainder;
textField.Text = formattedString;
return false;
}

Also for format +x (xxx) xxx-xx-xx you can use something like this simple solution:
+ (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
NSString *newString = [textField.text stringByReplacingCharactersInRange:range withString:string];
NSArray *components = [newString componentsSeparatedByCharactersInSet:[[NSCharacterSet decimalDigitCharacterSet] invertedSet]];
NSString *decimalString = [components componentsJoinedByString:#""];
if (decimalString.length > 11) {
return NO;
}
NSMutableString *formattedString = [NSMutableString stringWithString:decimalString];
[formattedString insertString:#"+" atIndex:0];
if (formattedString.length > 2)
[formattedString insertString:#" (" atIndex:2];
if (formattedString.length > 7)
[formattedString insertString:#") " atIndex:7];
if (formattedString.length > 12)
[formattedString insertString:#"-" atIndex:12];
if (formattedString.length > 15)
[formattedString insertString:#"-" atIndex:15];
textField.text = formattedString;
return NO;}

SWIFT
func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
let length = self.getTextLength(textField.text)
if length == 10{
if range.length == 0{
return false
}
}
if length == 3{
var num : String = self.formatNumber(textField.text)
textField.text = num + "-"
if(range.length > 0){
textField.text = (num as NSString).substringToIndex(3)
}
}
else if length == 6{
var num : String = self.formatNumber(textField.text)
let prefix = (num as NSString).substringToIndex(3)
let postfix = (num as NSString).substringFromIndex(3)
textField.text = prefix + "-" + postfix + "-"
if range.length > 0{
textField.text = prefix + postfix
}
}
return true
}
func getTextLength(mobileNo: String) -> NSInteger{
var str : NSString = mobileNo as NSString
str = str.stringByReplacingOccurrencesOfString("(", withString: "")
str = str.stringByReplacingOccurrencesOfString(")", withString: "")
str = str.stringByReplacingOccurrencesOfString(" ", withString: "")
str = str.stringByReplacingOccurrencesOfString("-", withString: "")
str = str.stringByReplacingOccurrencesOfString("+", withString: "")
return str.length
}
func formatNumber(mobileNo: String) -> String{
var str : NSString = mobileNo as NSString
str = str.stringByReplacingOccurrencesOfString("(", withString: "")
str = str.stringByReplacingOccurrencesOfString(")", withString: "")
str = str.stringByReplacingOccurrencesOfString(" ", withString: "")
str = str.stringByReplacingOccurrencesOfString("-", withString: "")
str = str.stringByReplacingOccurrencesOfString("+", withString: "")
if str.length > 10{
str = str.substringFromIndex(str.length - 10)
}
return str as String
}

Swift Formatted Phone Number
Improves oppon #datinc's answer,
input such as 1123df#$#$gdfg45-+678dfg901 will be output as +11(234)567-8901
func formattedPhone(phone: String) -> String? {
let notPhoneNumbers = NSCharacterSet.decimalDigitCharacterSet().invertedSet
let str = phone.componentsSeparatedByCharactersInSet(notPhoneNumbers).joinWithSeparator("")
let startIdx = str.startIndex
let endIdx = str.endIndex
let count = str.characters.count
if count == 7 {
return "\(str[startIdx..<startIdx.advancedBy(3)])-\(str[startIdx.advancedBy(3)..<endIdx])"
}else if count == 10{
return "(\(str[startIdx..<startIdx.advancedBy(3)]))\(str[startIdx.advancedBy(3)..<startIdx.advancedBy(6)])-\(str[startIdx.advancedBy(6)..<endIdx])"
}
else if count > 10{
let extra = str.characters.count - 10
return "+\(str[startIdx..<startIdx.advancedBy(extra)])(\(str[endIdx.advancedBy(-10)..<endIdx.advancedBy(-7)]))\(str[endIdx.advancedBy(-7)..<endIdx.advancedBy(-4)])-\(str[endIdx.advancedBy(-4)..<endIdx])"
}
return nil
}

This will help you
Format (xxx) xxx-xxxx For SWIFT 3.0
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
let length = Int(getLength(mobileNumber: textField.text!))
if length == 15 {
if range.length == 0 {
return false
}
}
if length == 3 {
let num = self.formatNumber(mobileNumber: textField.text!)
textField.text = NSString(format:"(%#)",num) as String
if range.length > 0{
let index: String.Index = num.index(num.startIndex, offsetBy: 3)
textField.text = NSString(format:"%#",num.substring(to: index)) as String
}
}else if length == 6 {
let num = self.formatNumber(mobileNumber: textField.text!)
let index: String.Index = num.index(num.startIndex, offsetBy: 3)
textField.text = NSString(format:"(%#) %#-",num.substring(to: index), num.substring(from: index)) as String
if range.length > 0{
textField.text = NSString(format:"(%#) %#",num.substring(to: index), num.substring(from: index)) as String
}
}
return true
}
func formatNumber(mobileNumber: String) -> String {
var number = mobileNumber
number = number.replacingOccurrences(of: "(", with: "")
number = number.replacingOccurrences(of: ")", with: "")
number = number.replacingOccurrences(of: " ", with: "")
number = number.replacingOccurrences(of: "-", with: "")
number = number.replacingOccurrences(of: "+", with: "")
let length = Int(number.characters.count)
if length > 15 {
let index = number.index(number.startIndex, offsetBy: 15)
number = number.substring(to: index)
}
return number
}
func getLength(mobileNumber: String) -> Int {
var number = mobileNumber
number = number.replacingOccurrences(of: "(", with: "")
number = number.replacingOccurrences(of: ")", with: "")
number = number.replacingOccurrences(of: " ", with: "")
number = number.replacingOccurrences(of: "-", with: "")
number = number.replacingOccurrences(of: "+", with: "")
let length = Int(number.characters.count)
return length
}

NSString *str=#"[+]+91[0-9]{10}";
NSPredicate *no=[NSPredicate predicateWithFormat:#"SELF MATCHES %#",str];
if([no evaluateWithObject:txtMobileno.text]==NO
{
UIAlertView *alert=[[UIAlertView alloc]initWithTitle:#"Warning" message:#"Please Enter correct contact no." delegate:self cancelButtonTitle:#"ok" otherButtonTitles:nil];
[alert show];
[alert release];
}

So this method will format for (xxx) xxx - xxxx ....
it is a modification of the current top answer and handles backspaces
- (IBAction)autoFormat:(UITextField *)sender {
NSString *mobileNumber = [NSString stringWithFormat:#"%#",sender.text];
mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:#"(" withString:#""];
mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:#")" withString:#""];
mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:#" " withString:#""];
mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:#"-" withString:#""];
mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:#"+" withString:#""];
int length = [mobileNumber length];
if(length > 0 && [sender.text length] > self.oldLength){
if(length >= 7 && length <= 10){
sender.text = [NSString stringWithFormat:#"(%#) %# - %#",[mobileNumber substringToIndex:3], [mobileNumber substringWithRange:NSMakeRange(3,3)],[mobileNumber substringWithRange:NSMakeRange(6,[mobileNumber length]-6)]];
} else if(length >= 4 && length <= 6) {
sender.text = [NSString stringWithFormat:#"(%#) %#",[mobileNumber substringToIndex:3], [mobileNumber substringWithRange:NSMakeRange(3,[mobileNumber length]-3)]];
}
if(length >= 11 && length % 4 == 3){
NSString *lastChar = [sender.text substringFromIndex:[sender.text length] - 1];
sender.text = [NSString stringWithFormat:#"%# %#",[sender.text substringToIndex:[sender.text length] - 1],lastChar];
}
self.oldLength = [sender.text length];
} else if([sender.text length] < self.oldLength) {
NSLog(#"deleted - ");
self.oldLength = 0;
sender.text = #"";
for (int i = 0; i < [mobileNumber length]; i = i + 1) {
sender.text = [NSString stringWithFormat:#"%#%#",sender.text,[mobileNumber substringWithRange:NSMakeRange(i, 1)]];
[self autoFormat:sender];
}
}}
hope it helps

REFormattedNumberField is probably the best. Just supply a format you wish.

+(NSString *) phoneNumberFormatterTextField:(NSString *)number forRange:(NSRange)range
{
int length = (int)[[self getPhoneNumber:number] length];
if(length == 3)
{
NSString *num = [MPosBaseScreenController getPhoneNumber:number];
number = [num stringByReplacingOccurrencesOfString:#"(\\d{3})"
withString:#"($1) "
options:NSRegularExpressionSearch
range:NSMakeRange(0, num.length)];
}
else if(length == 6 || length > 6 )
{
NSString *num = [MPosBaseScreenController getPhoneNumber:number];
number = [num stringByReplacingOccurrencesOfString:#"(\\d{3})(\\d{3})"
withString:#"($1) $2 - "
options:NSRegularExpressionSearch
range:NSMakeRange(0, num.length)];
}
return number;
}

Here's a simple category which will mimic the format of the input
#interface NSString (formatDecimalsAs)
- (NSString *)formatDecimalsAs:(NSString *)formattedDecimals;
#end
#implementation NSString (formatDecimalsAs)
- (NSString *)formatDecimalsAs:(NSString *)formattedDecimals
{
// insert non-digit characters from source string
NSMutableString *formattedNumber = [self mutableCopy];
for (int i = 0; i < formattedDecimals.length; i++)
{
if (i > formattedNumber.length)
{
break;
}
unichar character = [formattedDecimals characterAtIndex:i];
if ([[NSCharacterSet decimalDigitCharacterSet].invertedSet characterIsMember:character])
{
[formattedNumber insertString:[NSString stringWithFormat:#"%c", character] atIndex:(NSUInteger) i];
}
}
return formattedNumber;
}
#end
example use
[#"87654321" formatDecimalsAs:#"1111 1111"] // returns #"8765 4321"

Removes all non-digit characters
If 7 digits remain, 123-4567
for 10 digits, (123) 456-7890
Otherwise, groups of three. To adjust the size of the groups, change the value assigned to substrsize
-(NSString*)formatPhone:(NSString*)phone {
NSString *formattedNumber = [[phone componentsSeparatedByCharactersInSet:
[[NSCharacterSet decimalDigitCharacterSet] invertedSet]]
componentsJoinedByString:#""];
int substrSize = 3;
NSMutableArray *t = [[NSMutableArray alloc] initWithCapacity:formattedNumber.length / substrSize + 1];
switch (formattedNumber.length) {
case 7:
formattedNumber = [NSString stringWithFormat:#"%#-%#",
[formattedNumber substringToIndex:3],
[formattedNumber substringFromIndex:3]];
break;
case 10:
formattedNumber = [NSString stringWithFormat:#"(%#) %#-%#",
[formattedNumber substringToIndex:3],
[formattedNumber substringWithRange:NSMakeRange(3, 3)],
[formattedNumber substringFromIndex:6]];
break;
default:
for (int i = 0; i < formattedNumber.length / substrSize; i++) {
[t addObject:[formattedNumber substringWithRange:NSMakeRange(i * substrSize, substrSize)]];
}
if (formattedNumber.length % substrSize) {
[t addObject:[formattedNumber substringFromIndex:(substrSize * t.count)]];
}
formattedNumber = [t componentsJoinedByString:#" "];
break;
}
return formattedNumber;
}

In SWIFT 3
func formattedPhone(phone: String) -> String? {
let notPhoneNumbers = CharacterSet.decimalDigits.inverted
let str = phone.components(separatedBy: notPhoneNumbers).joined(separator: "")
let startIdx = str.startIndex
let endIdx = str.endIndex
let count = str.characters.count
if count == 7 {
return "\(str[startIdx..<startIdx.advance(3, for: str)])-\(str[startIdx.advance(3, for: str)..<endIdx])"
}else if count == 10{
return "+1 (\(str[startIdx..<startIdx.advance(3, for: str)])) \(str[startIdx.advance(3, for: str)..<startIdx.advance(6, for: str)])-\(str[startIdx.advance(6, for: str)..<endIdx])"
}
else if count > 10{
let extra = str.characters.count - 10
return "+\(str[startIdx..<startIdx.advance(extra, for: str)]) (\(str[endIdx.advance(-10, for: str)..<endIdx.advance(-7, for: str)])) \(str[endIdx.advance(-7, for: str)..<endIdx.advance(-4, for: str)])-\(str[endIdx.advance(-4, for: str)..<endIdx])"
}
return nil
}
Swift 3 string.index.advancedBy(3) alternate:
extension String.Index{
func advance(_ offset:Int, `for` string:String)->String.Index{
return string.index(self, offsetBy: offset)
}
}

First, add UITextFieldDelegate to your .h file and Delegate your UITextField in the nib file.
Second, add this code to your .m file :
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
NSString *filter = #"(###)-(###)-####";
if(!filter) return YES;
NSString *changedString = [textField.text stringByReplacingCharactersInRange:range withString:string];
if(range.length == 1 &&
string.length < range.length &&
[[textField.text substringWithRange:range] rangeOfCharacterFromSet:[NSCharacterSet characterSetWithCharactersInString:#"0123456789"]].location == NSNotFound)
{
NSInteger location = changedString.length-1;
if(location > 0)
{
for(; location > 0; location--)
{
if(isdigit([changedString characterAtIndex:location]))
{
break;
}
}
changedString = [changedString substringToIndex:location];
}
}
textField.text = [self filteredPhoneStringFromStringWithFilter:changedString :filter];
return NO;
}
-(NSString*) filteredPhoneStringFromStringWithFilter:(NSString*)number : (NSString*)filter{
NSUInteger onOriginal = 0, onFilter = 0, onOutput = 0;
char outputString[([filter length])];
BOOL done = NO;
while(onFilter < [filter length] && !done)
{
char filterChar = [filter characterAtIndex:onFilter];
char originalChar = onOriginal >= number.length ? '\0' : [number characterAtIndex:onOriginal];
switch (filterChar) {
case '#':
if(originalChar=='\0')
{
// We have no more input numbers for the filter. We're done.
done = YES;
break;
}
if(isdigit(originalChar))
{
outputString[onOutput] = originalChar;
onOriginal++;
onFilter++;
onOutput++;
}
else
{
onOriginal++;
}
break;
default:
// Any other character will automatically be inserted for the user as they type (spaces, - etc..) or deleted as they delete if there are more numbers to come.
outputString[onOutput] = filterChar;
onOutput++;
onFilter++;
if(originalChar == filterChar)
onOriginal++;
break;
}
}
outputString[onOutput] = '\0'; // Cap the output string
return [NSString stringWithUTF8String:outputString];
}

Related

Validating a given string for a car plate number

I need to validate a Brazilian car plate number using Objective-C.
A Brazilian car plate number consists of three alphabetic characters + four numeric digits such as ABC1234.
How do I this in a simple way using Objective-C?
I am sure someone knows how to do it in 2 or 3 lines of code, but this is what I can work out so far:
+(BOOL)validatePlacaString:(NSString *)inputedString{
// remove "-" and spaces from the string:
inputedString = [inputedString stringByReplacingOccurrencesOfString:#" " withString:#""];
inputedString = [inputedString stringByReplacingOccurrencesOfString:#"-" withString:#""];
NSLog(#"you have inputed '%#' for validation...", inputedString);
// get the first 3 chars:
NSRange range03 = NSMakeRange(0, 3);
// get the next 4 chars:
NSRange range34 = NSMakeRange(3, 4);
if ([inputedString length] == 7) {
// convert to uppercase the given string:
NSString *first3Chars = [[inputedString uppercaseString] substringWithRange:range03];
NSString *last4Chars = [inputedString substringWithRange:range34];
NSLog(#"first 3 chars --> '%#' last 4 chars --> '%#' ", first3Chars, last4Chars);
// make sure you have only allowed chars in the first 3 chars:
NSArray *first3CharsArray = [self convertToArray:first3Chars];
BOOL valid = YES;
for (NSString *s in first3CharsArray)
{
if ([#"ABCDEFGHIJKLMNOPQRSTUVXWYZ" rangeOfString:s].location != NSNotFound) {
NSLog(#"VALID --> %#", s);
}else{
NSLog(#"NOT VALID --> %#", s);
valid = NO;
break;
}
}
NSLog(#"String is valid --> %d", valid);
if (valid) {
if ([[self getNumbersFromString:last4Chars] length] == 4) {
return YES;
}else{
return NO;
}
}else{
return NO;
}
}else{
// inputed string is not 7!!!
return NO;
}
return NO;
}
+(NSArray *)convertToArray:(NSString *)givenString{
NSMutableArray *arr = [[NSMutableArray alloc]init];
for (int i=0; i < givenString.length; i++) {
NSString *tmp_str = [givenString substringWithRange:NSMakeRange(i, 1)];
[arr addObject:[tmp_str stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
}
return arr;
}
+(NSString*)getNumbersFromString:(NSString*)String{
NSArray* Array = [String componentsSeparatedByCharactersInSet:
[[NSCharacterSet decimalDigitCharacterSet] invertedSet]];
NSString* returnString = [Array componentsJoinedByString:#""];
return (returnString);
}

Check if string is palindrome in objective c

I'm trying to check if a string is palindrome or not using objective c. I'm new to programming without any experience in other programming languages so bear with me please. I get stuck at my if condition I want it to say that if the first position in the string is equal to the last one the string is a palindrome.
What im a doing wrong?
int main (int argc, const char * argv[])
{
NSString *p = #"121" ;
BOOL palindrome = TRUE;
for (int i = 0 ; i<p.length/2+1 ; i++)
{
if (p[i] != p [p.Length - i - 1])
palindrome = false;
}
return (0);
}
You're trying to use an NSString as an NSArray (or probably, like a C string), which won't work. Instead, you need to use the NSString method characterAtIndex: to get the character to test.
Apart from the unbalanced braces, accessing a character from NSString is more complicated than using array notation. You need to use the method characterAtIndex: You can optimise your code, by breaking out of the loop if a palindrome is impossible and taking the length call outside of the for loop.
NSString *p = #"121";
NSInteger length = p.length;
NSInteger halfLength = (length / 2);
BOOL isPalindrome = YES;
for (int i = 0; i < halfLength; i++) {
if ([p characterAtIndex:i] != [p characterAtIndex:length - i - 1]) {
isPalindrome = NO;
break;
}
}
It may be desirable to check case insensitively. To do this, make the string be all lowercase before looping, using the lowercaseString method.
As pointed out by Nikolai in the comments, this would only work for strings containing 'normal' unicode characters, which is often not true — such as when using UTF8 for foreign languages. If this is a possibility, use the following code instead, which checks composed character sequences rather than individual characters.
NSString *p = #"121";
NSInteger length = p.length;
NSInteger halfLength = length / 2;
__block BOOL isPalindrome = YES;
[p enumerateSubstringsInRange:NSMakeRange(0, halfLength) options:NSStringEnumerationByComposedCharacterSequences usingBlock:^(NSString *substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop) {
NSRange otherRange = [p rangeOfComposedCharacterSequenceAtIndex:length - enclosingRange.location - 1];
if (![substring isEqualToString:[p substringWithRange:otherRange]]) {
isPalindrome = NO;
*stop = YES;
}
}];
var str: NSString = "123321"
var length = str.length
var isPalindrome = true
for index in 0...length/2{
if(str.characterAtIndex(index) != str.characterAtIndex(length-1 - index)){
print("\(index )not palindrome")
isPalindrome = false
break
}
}
print("is palindrome: \(isPalindrome)")
As it seems there's no answer yet that handles composed character sequences correctly I'm adding my two cents:
NSString *testString = #"\u00E0 a\u0300"; // "à à"
NSMutableArray *logicalCharacters = [NSMutableArray array];
[testString enumerateSubstringsInRange:(NSRange){0, [testString length]}
options:NSStringEnumerationByComposedCharacterSequences
usingBlock:^(NSString *substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop)
{
[logicalCharacters addObject:substring];
}];
NSUInteger count = [logicalCharacters count];
BOOL isPalindrome = YES;
for (NSUInteger idx = 0; idx < count / 2; ++idx) {
NSString *a = logicalCharacters[idx];
NSString *b = logicalCharacters[count - idx - 1];
if ([a localizedCaseInsensitiveCompare:b] != NSOrderedSame) {
isPalindrome = NO;
break;
}
}
NSLog(#"isPalindrome: %d", isPalindrome);
This splits the string into an array of logical characters (elements of a string that a normal user would call a "character").
#import Foundation;
BOOL isPalindrome(NSString * str)
{
if (!str || str.length == 0) return NO;
if (str.length == 1) return YES;
for(unsigned i = 0; i < str.length / 2; ++i)
if ([str characterAtIndex:i] != [str characterAtIndex:str.length - i - 1]) return NO;
return YES;
}
int main() {
#autoreleasepool {
NSLog(#"%s", isPalindrome(#"applelppa") ? "YES" : "NO");
} return 0;
}
Recursive
- (BOOL)isPaliRec:(NSString*)str :(int)start :(int)end{
if(start >= end)
return YES;
else if([str characterAtIndex:start] != [str characterAtIndex:end])
return NO;
else
return [self isPaliRec:str :++start :--end];
}
Non Recursive
- (BOOL)isPali:(NSString*)str{
for (int i=0; i<str.length/2; i++)
if([str characterAtIndex:i] != [str characterAtIndex:(str.length-i-1)])
return NO;
return YES;
}
you can call:
NSString *str = #"arara";
[self isPaliRec:str :0 :(int)str.length-1];
[self isPali:str];
Swift 3:
// Recursive
func isPaliRec(str: String, start: Int = 0, end: Int = str.characters.count-1) -> Bool {
if start >= end {
return true
} else if str[str.index(str.startIndex, offsetBy: start)] != str[str.index(str.startIndex, offsetBy: end)] {
return false
} else {
return isPaliRec(str: str, start: start+1, end: end-1)
}
}
// Non Recursive
func isPali(str: String) -> Bool {
for i in 0..<str.characters.count/2 {
let endIndex = str.characters.count-i-1
if str[str.index(str.startIndex, offsetBy: i)] != str[str.index(str.startIndex, offsetBy: endIndex)] {
return false
}
}
return true
}
// Using
let str = "arara"
isPaliRec(str: str)
isPali(str: str)
Also, you can use swift 3 methods like a string extension... It's more elegant. extension sample
NSString *str=self.txtFld.text;
int count=str.length-1;
for (int i=0; i<count; i++) {
char firstChar=[str characterAtIndex:i];
char lastChar=[str characterAtIndex:count-i];
NSLog(#"first=%c and last=%c",firstChar,lastChar);
if (firstChar !=lastChar) {
break;
}
else
NSLog(#"Pailndrome");
}
We can also do this using NSRange like this...
enter code NSString *fullname=#"123321";
NSRange rangeforFirst=NSMakeRange(0, 1);
NSRange rangeforlast=NSMakeRange(fullname.length-1, 1);
BOOL ispalindrome;
for (int i=0; i<fullname.length; i++) {
if (![[fullname substringWithRange:rangeforFirst] isEqualToString:[fullname substringWithRange:rangeforlast]]) {
NSLog(#"not match");
ispalindrome=NO;
return;
}
i++;
rangeforFirst=NSMakeRange(i, 1);
rangeforlast=NSMakeRange(fullname.length-i-1, 1);
}
NSLog(#"no is %#",(ispalindrome) ? #"matched" :#"not matched");
NSString *str1 = #"racecar";
NSMutableString *str2 = [[NSMutableString alloc] init];
NSInteger strLength = [str1 length]-1;
for (NSInteger i=strLength; i>=0; i--)
{
[str2 appendString:[NSString stringWithFormat:#"%C",[str1 characterAtIndex:i]]];
}
if ([str1 isEqual:str2])
{
NSLog(#"str %# is palindrome",str1);
}
-(BOOL)checkPalindromeNumber:(int)number{
int originalNumber,reversedNumber = 0,remainder;
originalNumber=number;
while (number!=0) {
remainder=number%10;
reversedNumber=(reversedNumber*10)+remainder;
number=number/10;
}
if (reversedNumber==originalNumber) {
NSLog(#"%d is Palindrome Number",originalNumber);
return YES;
}
else{
NSLog(#"%d is Not Palindrome Number",originalNumber);
return NO;
}
}

Split NSString with multiple delimiters?

For text bozo__foo!!bar.baz, how to split an NSString containing this into (bozo, foo, bar, baz)?
That is, separe it in components with strings (delimiters) __, !! and ..
You can split the strings using NSCharacterSet. Try this
NSString *test=#"bozo__foo!!bar.baz";
NSString *sep = #"_!.";
NSCharacterSet *set = [NSCharacterSet characterSetWithCharactersInString:sep];
NSArray *temp=[test componentsSeparatedByCharactersInSet:set];
NSLog(#"temp=%#",temp);
I'm aware that this question has already been answered but this is a way to separate strings using multiple strings. This is a category to NSString.
- (NSArray<NSString *> *)componentsSeparatedByStrings:(NSArray<NSString *> *)separators
{
NSMutableArray<NSString *> *components = [[NSMutableArray alloc] init];
unichar buffer[self.length + 1];
NSInteger currentOrigin = 0;
NSInteger currentLength = 0;
[self getCharacters:buffer];
for(NSInteger i = 0; i < self.length; i++)
{
unichar currentChar = buffer[i];
currentLength++;
for(NSInteger n = 0; n < separators.count; n++)
{
NSString *currentDivider = [separators objectAtIndex:n];
if(currentDivider.length == 0)
{
return #[self];
}
else if(currentDivider.length > 1)
{
BOOL goodMatch = NO;
for(NSInteger x = 0; x < currentDivider.length; x++)
{
unichar charInDivider = [currentDivider characterAtIndex:x];
if(charInDivider == currentChar)
{
goodMatch = YES;
}
else
{
goodMatch = NO;
break;
}
if(goodMatch == YES && ((x + 1) != currentDivider.length))
{
i++;
currentLength++;
currentChar = buffer[i];
}
}
if(goodMatch == YES)
{
NSRange newComponentRange = NSMakeRange(currentOrigin, (currentLength - currentDivider.length));
NSString *newComponent = [self substringWithRange:newComponentRange];
currentOrigin = (i + 1);
currentLength = 0;
[components addObject:newComponent];
NSLog(#"%#", components);
}
}
else // If current divider is only one character long.
{
if([currentDivider characterAtIndex:0] == currentChar)
{
NSRange newComponentRange = NSMakeRange(currentOrigin, (currentLength - 1));
NSString *newComponent = [self substringWithRange:newComponentRange];
currentOrigin = (i + 1);
currentLength = 0;
[components addObject:newComponent];
break;
}
}
}
// Handle the end of the string.
if((i + 1) == self.length)
{
NSRange newComponentRange = NSMakeRange(currentOrigin, currentLength);
NSString *newComponent = [self substringWithRange:newComponentRange];
currentOrigin = 0;
currentLength = 0;
[components addObject:newComponent];
}
}
return components;
}
Example: "ABCD__EFGHI__JKLMNOP-QRST.UV_WXYZ"
NSLog(#"%#", [test componentsSeparatedByStrings:#[#"__", #"-", #"."]]);
Log Result: "(ABCD, EFGHI, JKLMNOP, QRST, "UV_WXYZ")"
NSString *text = #"bozo__foo!!bar.baz";
NSArray *split1 = [text componentsSeparatedByString:#"__"];
NSArray *split2 = [[split1 lastObject] componentsSeparatedByString:#"!!"];
NSArray *split3 = [[split2 lastObject] componentsSeparatedByString:#"."];
NSLog(#"%#, %#, %#, %#", split1[0], split2[0], split3[0], split3[1]);
More functional solution is to apply -componentsSeparatedByString: recursively, for each component, which was derived during previous separator application:
NSString Category
- (NSMutableArray<NSString *> *)gvr_componentsSeparatedByStrings:(NSArray<NSString *> *)separators {
if (separators.count == 0) {
return [NSMutableArray arrayWithObject:self];
}
NSString *separator = [separators firstObject];
NSArray *reducedSeparators = [separators gvr_arrayByRemovingFirstObject];
NSArray *components = [self componentsSeparatedByString:separator];
NSMutableArray *result = [NSMutableArray new];
for (NSString *component in components) {
NSMutableArray *subResult = [component gvr_componentsSeparatedByStrings:reducedSeparators];
[result addObjectsFromArray:subResult];
}
return result;
}
NSArray Category
- (NSArray *)gvr_arrayByRemovingFirstObject {
NSMutableArray *result = [NSMutableArray new];
for (NSInteger i = 1; i < self.count; i++) {
[result addObject:self[i]];
}
return [result copy];
}
I solved it for my project by looking for the longest separator, replacing the others with this one, then do the separation on the only one left.
Try this:
NSString *test = #"bozo__foo!!bar.baz";
test = [test stringByReplacingOccurrencesOfString:#"!!" withString:#"__"];
test = [test stringByReplacingOccurrencesOfString:#"." withString:#"__"];
NSArray<NSString *> *parts = [test componentsSeparatedByString:#"__"];

Substring to nth character

I need to substring to the 2nd comma in an NSString.
Input:
NSString *input = #"title, price, Camry, $19798, active";
Desired Output:
NSString *output = #"title, price";
Thanks!
UPDATE:
I have the following but the problem is it needs to skip the last comma:
NSString *output = [input rangeOfString:#"," options:NSBackwardsSearch];
Try this:
- (NSString *)substringOfString:(NSString *)base untilNthOccurrence:(NSInteger)n ofString:(NSString *)delim
{
NSScanner *scanner = [NSScanner scannerWithString:base];
NSInteger i;
for (i = 0; i < n; i++)
{
[scanner scanUpToString:delim intoString:NULL];
[scanner scanString:delim intoString:NULL];
}
return [base substringToIndex:scanner.scanLocation - delim.length];
}
this code should do what you need:
NSString *input = #"title, price, Camry, $19798, active";
NSArray *array = [input componentsSeparatedByString:#","];
NSArray *subArray = [array subarrayWithRange:NSMakeRange(0, 2)];
NSString *output = [subArray componentsJoinedByString:#","];
NSLog(output);
You could split -> splice -> join that string like this in objc:
NSString *input = #"title, price, Camry, $19798, active";
// split by ", "
NSArray *elements = [input componentsSeparatedByString: #", "];
// grab the subarray
NSArray *subelements = [elements subarrayWithRange: NSMakeRange(0, 2)];
// concat by ", " again
NSString *output = [subelements componentsJoinedByString:#", "];
You can try something like this:
NSArray *items = [list componentsSeparatedByString:#", "];
NSString result = #"";
result = [result stringByAppendingString:[items objectAtIndex:0]];
result = [result stringByAppendingString:#", "];
result = [result stringByAppendingString:[items objectAtIndex:1]];
You have to check you have at least two items if you want avoid an exception.
There's really nothing wrong with simply writing the code to do what you want. Eg:
int commaCount = 0;
int i;
for (i = 0; i < input.count; i++) {
if ([input characterAtIndex:i] == (unichar) ',') {
commaCount++;
if (commaCount == 2) break;
}
}
NSString output = nil;
if (commaCount == 2) {
output = [input substringToIndex:i];
}
You could create an NSString category to handle finding nth occurrences of any string. This is example is for ARC.
//NSString+MyExtension.h
#interface NSString(MyExtension)
-(NSString*)substringToNthOccurrence:(NSUInteger)nth
ofString:(NSString*)string;
-(NSString*)substringToNthOccurrence:(NSUInteger)nth
ofString:(NSString*)string
options:(NSStringCompareOptions)options;
#end
#implementation NSString(MyExtension)
-(NSString*)substringToNthOccurrence:(NSUInteger)nth
ofString:(NSString*)string
{
return [self substringToNthOccurrence:nth ofString:string options:0];
}
-(NSString*)substringToNthOccurrence:(NSUInteger)nth
ofString:(NSString*)string
options:(NSStringCompareOptions)options
{
NSUInteger location = 0,
strlength = [string length],
mylength = [self length];
NSRange range = NSMakeRange(location, mylength);
while(nth--)
{
location = [self rangeOfString:string
options:options
range:range].location;
if(location == NSNotFound || (location + strlength) > mylength)
{
return [self copy]; //nth occurrence not found
}
if(nth == 0) strlength = 0; //This prevents the last occurence from being included
range = NSMakeRange(location + strlength, mylength - strlength - location);
}
return [self substringToIndex:location];
}
#end
//main.m
#import "NSString+MyExtension.h"
int main(int argc, char *argv[])
{
#autoreleasepool {
NSString *output = [#"title, price, Camry, $19798, active" substringToNthOccurrence:2 ofString:#","];
NSLog(#"%#", output);
}
}
*I'll leave it as an exercise for someone to implement the mutable versions.

method for replacing comma between quotes in NSString, please check my one

For parsing purposes it was necessary to replace commas inside quotas with space. I did not find and solution in NSString class and wrote this one.
It works, but I would like to know your opinion if there is more simple approach:
- (NSString *) replaceBetweenQuotesInString:(NSString*)line {
const char *buffer = [line UTF8String];
NSMutableIndexSet *evenIndexes = [NSMutableIndexSet indexSet];
NSMutableIndexSet *oddIndexes = [NSMutableIndexSet indexSet];
BOOL evenOdd = YES;
for (unsigned int i = 0; i < [line length]; i++) {
if (buffer[i] == '"'){
if (evenOdd)
[evenIndexes addIndex:i];
else
[oddIndexes addIndex:i];
evenOdd = !evenOdd;
}
}
if ([evenIndexes count] != [oddIndexes count] )
[evenIndexes removeIndex:[evenIndexes lastIndex]];
int totalRanges = (int) [evenIndexes count];
for (int i = 0; i < totalRanges; i++) {
NSRange range = NSMakeRange([evenIndexes firstIndex], [oddIndexes firstIndex] - [evenIndexes firstIndex]);
[evenIndexes removeIndex:[evenIndexes firstIndex]];
[oddIndexes removeIndex:[oddIndexes firstIndex]];
line = [line stringByReplacingOccurrencesOfString:#"," withString:#" " options:NSLiteralSearch range:range];
}
[evenIndexes removeAllIndexes];
[oddIndexes removeAllIndexes];
return line;
}
I have not tested this but since you do not seem to be concerned about escaped quotes:
NSMutableString* result = [[NSMutableString alloc] init];
NSArray* components = [line componentsSeparatedByString: #"\""];
for (int i = 0 ; i < [components count] ; ++i)
{
if (i % 2 == 0)
{
[result appendString: [components objectAtIndex: i]];
}
else
{
NSString* fixed = [[components objectAtIndex: i] stringByReplacingOccurrencesOfString: #","
withString: #" "];
[result appendFormat: #"\"%#\"", fixed];
}
}