in IOS,how to remove +-#*() ,which get from address book - iphone

In IOS,how to remove +-#*() ,which get from address book.
for example:
NSString *txt = #"+1(510)1234567"
txt = [NSString stringWithFormat:#"tel://%#", txt];
[[UIApplication sharedApplication]canOpenURL:[NSURL URLWithString:txt]];
it's a invalid number to call.
[[UIApplication sharedApplication]openURL:[NSURL URLWithString:urlToCall]]; is useless for this type number.
could I have some better options except use
[txt stringByReplacingOccurrencesOfString:#"-" withString:#""]....
I just wanna make a call.
Thanks for your help.
In fact:we can make a call by openURL: the format of call is not "tel://",It is "tel:".
So,what I should do is that:
NSString *cleanedString = [[phoneNumber componentsSeparatedByCharactersInSet:
[[NSCharacterSet characterSetWithCharactersInString:#"0123456789-+()"]
invertedSet]] componentsJoinedByString:#""];
NSURL *telURL = [NSURL URLWithString:[NSString stringWithFormat:#"tel:%#", cleanedString]];
That's OK.
————————————————————————————————
#"tel://“ isn't the right url to make a call!
we must use #"tel:"
if not some number ,as +1 (510) 3436 which
BOOL bCanCall = [[UIApplication sharedApplication]canOpenURL:urlToCall]; will return False. you can't make a call.

Try this, it may work
-(NSString *) formatIdentificationNumber:(NSString *)string
{
NSCharacterSet * invalidNumberSet = [NSCharacterSet characterSetWithCharactersInString:#"\n_!##$%^&*()[]{}'\".,<>:;|\\/?+=\t~` "];
NSString * result = #"";
NSScanner * scanner = [NSScanner scannerWithString:string];
NSString * scannerResult;
[scanner setCharactersToBeSkipped:nil];
while (![scanner isAtEnd])
{
if([scanner scanUpToCharactersFromSet:invalidNumberSet intoString:&scannerResult])
{
result = [result stringByAppendingString:scannerResult];
}
else
{
if(![scanner isAtEnd])
{
[scanner setScanLocation:[scanner scanLocation]+1];
}
}
}
return result;
}
or see the link

You can remove like this..
NSString *s = #"+1(510)1234567";
NSCharacterSet *unwantedStr = [NSCharacterSet characterSetWithCharactersInString:#"+()"];
s = [[s componentsSeparatedByCharactersInSet: unwantedStr] componentsJoinedByString: #""];
NSLog(#"%#", s);

Alternativ, some hints here, but you can optimize the code.
- (void)testExample
{
NSMutableCharacterSet *mvalidCharSet = [[NSMutableCharacterSet alloc] init];
[mvalidCharSet formUnionWithCharacterSet:[NSCharacterSet decimalDigitCharacterSet] ];
[mvalidCharSet addCharactersInString: #"+"];
NSCharacterSet *validCharSet = [mvalidCharSet copy]; // not mutable -> more efficient
NSCharacterSet *invalidCharSet = [validCharSet invertedSet];
NSString *txt = #"+1(510)1234567";
NSArray * components = [txt componentsSeparatedByCharactersInSet:invalidCharSet];
NSString * rejoin = [components componentsJoinedByString:#""];
NSLog(#"%#", rejoin);
}

Related

How to get substring fom a string in iphone?

I've got some strings in the format:
="Netherlands Antillean Guilder (ANG)","Australian Dollar (AUD)"
I want to get the sub string between the parenthesis "()"
How do I do this?
Sloppy way to do it:
NSString *theString = #"Netherlands Antillean Guilder (ANG)";
NSArray *someArray = [theString componentsSeparatedByString:#"("];
theString = [someArray objectAtIndex:1];
someArray = [theString componentsSeparatedByString:#")"];
theString = [someArray objectAtIndex:0];
theString should be "ANG". Don't have SDK nearby, so there might be a mistake or two.
Hope it helps
NSString *startfrom = [[NSString alloc] initWithFormat:#"("];
NSString *Myword = nil;
NSScanner *scanner = [NSScanner scannerWithString:"Netherlands Antillean Guilder (ANG)","Australian Dollar (AUD)"];
[scanner scanUpToString:startfrom intoString:nil];
[scanner scanUpToString:#")" intoString:&Myword];
NSString *finalword = [Myword substringFromIndex:[startfrom length]];
NSLog(#" %#",finalword);
[startfrom release];

remove last digits from float value,iPhone

I have a float value say 28.980000.
I want something like that 28.98, only 2 values after decimal.
How can i do this ?
Regards
One of the easiest solutions would be to use stringWithFormat: of NSString:
NSString* result = [NSString stringWithFormat: #"%5.2f", floatVal];
Try this..
-(NSString*)floatToString:(float)val
{
NSString *result;
NSArray *numbers = [[NSArray alloc]initWithObjects:[NSString stringWithFormat:#"%f",val], nil];
NSLog(#"%#",numbers);
NSArray * floatingPoint = [[numbers objectAtIndex:0] componentsSeparatedByString:#"."];
NSString * lastObject = [[floatingPoint lastObject] stringByReplacingOccurrencesOfString:#"0" withString:#""];
if ([lastObject length] < 2) {
result = [NSString stringWithFormat:#"$ %.2f",[[numbers objectAtIndex:0] floatValue]];
} else {
result = [NSString stringWithFormat:#"$ %g",[[numbers objectAtIndex:0] floatValue]];
}
return result;
}

iPhone:Convert NSString 123-123-1234 into (123) 123-1234

I have string like 123-123-1234 so I want to convert string into this format
(123) 123-1234 so any idea to develop this functionality.
Thanks in advance.
I think something like this:
NSString *list = #"123-123-1234";
NSArray *listItems = [list componentsSeparatedByString:#"-"];
NSString *result = [NSString stringWithFormat:#"(%#) %#-%#", [listItem objectAtIndex:0], [listItem objectAtIndex:1], [listItem objectAtIndex:2]];
NSString * original = #"123-123-1234";
NSArray * components = [original componentsSeparatedByString:#"-"];
NSString * first = [NSString stringWithFormat:#"(%#)",[components objectAtIndex:0] ];
NSString * second = [NSString stringWithFormat:#"%#-%#",[components objectAtIndex:1],[components objectAtIndex:2]];
NSString * finalString = [NSString stringWithFormat:#"%#%#",first,second];
NSLog(#"Final Result = %#",finalString);
NSString *str = #"123-123-1234";
NSArray *arrForDate = [str componentsSeparatedByString: #"-"];
NSString *str1 = [NSString stringWithFormat:#"(%#) %#-%#",[arrForDate objectAtIndex:0],[arrForDate
objectAtIndex:1],[arrForDate objectAtIndex:2]];
NSLog(#"str1 %#",str1);
TESTED CODE: 100% WORKS
NSString *inputString=#"123-123-1234";
NSArray *TotalString=[inputString componentsSeparatedByString:#"-"];
NSString *outputString = [NSString stringWithFormat:#"(%#) %#-%#",[TotalString objectAtIndex:0],[TotalString objectAtIndex:1],[TotalString objectAtIndex:2]];
NSLog(#"outputString is : %# \n\n",outputString);
OUTPUT:
outputString is : (123) 123-1234
For Dynamic Sollution:
NOTE: if u have more dashes with ur string then it is really hard code everything with objectAtIndex with number 0,1,2,....
moreover it will crash if it has unexpected length
so here is the solution for this
NSString *inputString=#"123-123-1234";
NSArray *TotalString=[inputString componentsSeparatedByString:#"-"];
NSMutableString *outputString=[NSMutableString string];
for (NSMutableString *obj in TotalString) {
if ([TotalString objectAtIndex:0] == obj && [outputString length]<=0) {
outputString=[[outputString stringByAppendingFormat:#"(%#)",obj] mutableCopy];
}
else if ([TotalString objectAtIndex:1] == obj) {
outputString=[[outputString stringByAppendingFormat:#"%#%#",#" ",obj] mutableCopy];
}
else {
outputString=[[outputString stringByAppendingFormat:#"-%#",obj] mutableCopy];
}
}
OUTPUT:
outputString is : (123) 123-1234
NSString *list = #"123-123-1234";
NSArray *listItems = [list componentsSeparatedByString:#"-"];
NSString *result = [NSString stringWithFormat:#"(%#) %#-%#", [listItem objectAtIndex:0], [listItem objectAtIndex:1], [listItem objectAtIndex:2]];

Extract an NSString using NSScanner

I'm fetching data from AllContacts; in that data I'm getting contact details such as (998) 989-8989. Using this number, I'm not able to make a call. Can any one help out with this? Thanks in advance.
HI All
At last i have used this following code to resolve this issue
NSString *originalString = #"(998) 989-8989";
NSMutableString *strippedString = [NSMutableString
stringWithCapacity:originalString.length];
NSScanner *scanner = [NSScanner scannerWithString:originalString];
NSCharacterSet *numbers = [NSCharacterSet
characterSetWithCharactersInString:#"0123456789"];
while ([scanner isAtEnd] == NO) {
NSString *buffer;
if ([scanner scanCharactersFromSet:numbers intoString:&buffer]) {
[strippedString appendString:buffer];
} else {
[scanner setScanLocation:([scanner scanLocation] + 1)];
}
}
NSLog(#"%#", strippedString);
Thanks All
Sounds like you can just remove spaces, brackets and hypens.
NSString *phoneNumber = #"(998) 989-8989";
phoneNumber = [phoneNumber stringByReplacingOccurrencesOfString:#"-" withString:#""];
phoneNumber = [phoneNumber stringByReplacingOccurrencesOfString:#"(" withString:#""];
phoneNumber = [phoneNumber stringByReplacingOccurrencesOfString:#")" withString:#""];
phoneNumber = [phoneNumber stringByReplacingOccurrencesOfString:#" " withString:#""];
phoneNumber = [#"tel://" stringByAppendingString:phoneNumber];
[[UIApplication sharedApplication] openURL:phoneNumber];

iphone sdk - Remove all numbers except for characters a-z from a string

In my app I want to remove numbers except characters a-z from string. How can I get only characters?
This is the short answer which doesnt need any lengthy coding
NSString *newString = [[tempstr componentsSeparatedByCharactersInSet:
[[NSCharacterSet letterCharacterSet] invertedSet]] componentsJoinedByString:#""];`
swift 3:
(tempstr.components(separatedBy:NSCharacterSet.letters.inverted)).joined(separator: "")
eg:
("abc123".components(separatedBy:NSCharacterSet.letters.inverted)).joined(separator: "")
NSString *stringToFilter = #"filter-me";
NSMutableString *targetString = [NSMutableString string];
//set of characters which are required in the string......
NSCharacterSet *okCharacterSet = [NSCharacterSet characterSetWithCharactersInString:#"abcdefghijklmnopqrstuvwxyz"];
for(int i = 0; i < [stringToFilter length]; i++)
{
unichar currentChar = [stringToFilter characterAtIndex:i];
if([okCharacterSet characterIsMember:currentChar])
{
[targetString appendFormat:#"%C", currentChar];
}
}
NSLog(targetString);
[super viewDidLoad];
}
this was an answer given to me and works fine
I found an answer:
from remove-all-but-numbers-from-nsstring
NSString *originalString = #"(123) 123123 abc";
NSLog(#"%#", originalString);
NSMutableString *strippedString = [NSMutableString
stringWithCapacity:originalString.length];
NSScanner *scanner = [NSScanner scannerWithString:originalString];
NSCharacterSet *numbers = [NSCharacterSet
characterSetWithCharactersInString:#"abcdefghijklmnopqrstuvwxyz"];
while ([scanner isAtEnd] == NO) {
NSString *buffer;
if ([scanner scanCharactersFromSet:numbers intoString:&buffer]) {
[strippedString appendString:buffer];
} else {
[scanner setScanLocation:([scanner scanLocation] + 1)];
}
}
NSLog(#"%#", strippedString);