How to get substring fom a string in iphone? - 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];

Related

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

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);
}

How to find matching words in two separate strings?

If there are two separate strings, we need to retrieve all words that are matching (in those strings), in an array. also this matching should be case insensitive. What will be the most efficient way to achieve this.
For Example:
NSString *s1 = #"Hello there, I want to match similar words.";
NSString *s2 = #"Do you really want to match word, hello?";
The resulting array should contain, "hello", "want", "to", "match".
I went through many questions and, responses over the net like Regular expression to match a line that doesn't contain a word?, but didn't, find solution needed.
try this:
NSString *s1 = #"Hello there, I want to match similar words.";
NSString *s2 = #"Do you really Want to match word, hello?";
NSCharacterSet *separatorSet = [NSCharacterSet characterSetWithCharactersInString:#" ?.,"];
NSArray *as1 = [[s1 lowercaseString] componentsSeparatedByCharactersInSet:separatorSet];
NSArray *as2 = [[s2 lowercaseString] componentsSeparatedByCharactersInSet:separatorSet];
NSMutableSet* set1 = [NSMutableSet setWithArray:as1];
NSMutableSet* set2 = [NSMutableSet setWithArray:as2];
[set1 intersectSet:set2];
[set1 removeObject:#""];
NSArray* result =[set1 allObjects];
NSLog(#"%#",result);
You can use NSMutableSet for this..
NSString *s1 = #"Hello there, I want to match similar words.";
NSString *s2 = #"Do you really want to match word, hello?";
NSArray *components = [s1 componentsSeparatedByString:#" "];
NSArray *components1 = [s2 componentsSeparatedByString:#" "];
NSLog(#"%#",components);
NSLog(#"%#",components1);
NSMutableSet *resultSet = [NSMutableSet setWithArray:components1];
NSSet *setA = [NSSet setWithArray:components];
[resultSet intersectSet:setA];
NSArray *result = [resultSet allObjects];
NSLog(#"%#",result);
This Solution will work for you, If you don't have any punctuations in the sentences.. If you want sentences with punctuations to work just remove all the punctuations in your sentences.
NSString *s1 = #"Hello there, I want to match similar words.";
NSString *s2 = #"Do you really want to match word, hello?";
NSCharacterSet *separatorSet = [NSCharacterSet characterSetWithCharactersInString:#" ?.,"];
NSArray *s1Array = [s1 componentsSeparatedByCharactersInSet:separatorSet];
NSArray *s2Array = [s2 componentsSeparatedByCharactersInSet:separatorSet];
NSMutableArray *resultArray = [NSMutableArray arrayWithCapacity:0];
for(int index = 0; index < [s1Array count]; index++){
NSString *compareString = [[s1Array objectAtIndex:index] lowercaseString];
NSUInteger findIndex = [s2Array indexOfObjectPassingTest:^BOOL(id obj, NSUInteger idx, BOOL *stop) {
NSString *currentString = (NSString *)obj;
if([[currentString lowercaseString] isEqualToString:compareString] &&
![currentString isEqualToString:#""]){
return YES;
*stop = YES;
}else{
return NO;
}
}
];
if(findIndex !=NSNotFound){
[resultArray addObject:compareString];
}
}
NSLog(#"Result:%#",[resultArray description]);
Try with below code
NSString *s1 = #"Hello there, I want to match similar words.";
NSString *s2 = #"Do you really want to match word, hello?";
NSCharacterSet *doNotWant = [[NSCharacterSet letterCharacterSet] invertedSet];
NSArray *array1 = [[s1 uppercaseString] componentsSeparatedByCharactersInSet:doNotWant];
NSArray *array2 = [[s2 uppercaseString] componentsSeparatedByCharactersInSet:doNotWant];
NSSet *set1 = [[NSSet alloc] initWithArray:array1];
NSMutableSet *set2 = [[NSMutableSet alloc] initWithArray:array2];
[set2 intersectSet:set1];
NSLog(#"set2:%#",set2);
NSLog(#"set1:%#",set1);
NSArray *aray_res = [set2 allObjects];
NSLog(#"aray_res:%#",aray_res);

Cant replace string in NSMutableString

This is my string:
2011-10-07T08:55:16-05:00
I am trying to remove the colon with this code:
NSRange range = NSMakeRange(dateString.length-3, 1);
NSString *temp = [dateString substringWithRange:range];
if ([temp isEqualToString:#":"])
[dateString replaceCharactersInRange:range withString:#""];
My code enters the if statement, so I know that it found the colon. But it crashes with no errors on the last line. What am I doing wrong?
try this :
NSMutableString *dateString = [NSMutableString stringWithString:#"2011-10-07T08:55:16-05:00"];
NSRange range = NSMakeRange(dateString.length-3, 1);
NSString *temp = [dateString substringWithRange:range];
if ([temp isEqualToString:#":"])
[dateString replaceCharactersInRange:range withString:#""];

how can I convert string to an array with separator?

I have a string in the following format
myString = "cat+dog+cow"
I need to store each string separated by + in to a array.
Eg:
myArray[0] = cat
myArray[1] = dog
myArray[2] = cow
Can anyone tell me the proper way to do this?
componentsSeparatedByString: splits the string and return the result in an array.
NSArray *myArray = [myString componentsSeparatedByString:#"+"];
[myArray objectAtIndex:0];//cat
[myArray objectAtIndex:1];//dog
[myArray objectAtIndex:2];//cow
Try this..
NSArray *arr = [myString componentsSeparatedByString:#"-"];
[arr objectAtIndex:0];//Hai
[arr objectAtIndex:1];//Welcome
It is vert simple..
NSString * test = #"Hello-hi-splitting-for-test";
NSArray * stringArray = [test componentsSeparatedByString:#"-"];
// Now stringArray will contain all splitted strings.. :)
Hope this helps...
I you don't want use array then iterate through each character...
NSMutableString * splittedString = nil;
for(int i=0;i<test.length;i++){
unichar character = [test characterAtIndex:0];
if (character=='-') {
if (splittedString!=nil) {
NSLog(#"String component %#",splittedString);
[splittedString release];
splittedString = nil;
}
} else {
if (splittedString==nil) {
splittedString = [[NSMutableString alloc] init];
}
[splittedString appendFormat:#"%C",character];
}
}
if (splittedString!=nil) {
NSLog(#"String last component %#",splittedString);
[splittedString release];
splittedString = nil;
}
Thats all...
NSArray *myWords = [myString componentsSeparatedByString:#"+"];
You can find this one very simple
NSString *str = #"cat+dog+cow";
NSArray *arr = [str componentsSeparatedByString:#"+"];
NSLog(#"Array items %#",arr);
OUTPUT:
Array items
(
Cat,
dog,
Cow
)
Use the componentsSeparatedByString: method of NSString.
NSString string = #"hai-welcome";
NSArray myArray = [string componentsSeparatedByString:#"-"];
NSString* haiString = [myArray objectAtIndex:0];
NSString* welcomeString = [myArray objectAtIndex:1];
NSArray *strArray = [myString componentsSeparatedByString:#"-"];
firstString = [strArray objectAtIndex:0];//Hai
secondString = [strArray objectAtIndex:1];//Welcome
This will be the solution if you are dealing with a string:
NSString *mySstring = #"hai-welcome";
NSMutableArray *anArray=[[NSMutableArray alloc] initWithArray:[componentsSeparatedByString: #"-"]];
And each word will be stored in respective position from 0-n.
Try This. :)
If you are averse to using arrays, you can consider this –
NSString *accessMode, *message;
NSScanner *scanner = [NSScanner scannerWithString:#"hai-welcome"];
NSCharacterSet *hyphenSet = [NSCharacterSet characterSetWithCharactersInString:#"-"];
[scanner scanUpToCharactersFromSet:hyphenSet intoString:&accessMode];
[scanner scanCharactersFromSet:hyphenSet intoString:nil];
[scanner scanUpToCharactersFromSet:[NSCharacterSet characterSetWithCharactersInString:#""] intoString:&message];
NSArray *lines = [string componentsSeparatedByString:#"-"];
The first value will be stored in 0th index of lines and second value will be stored in 1th index of lines..
Why not array?
Simple code :
NSString *myString = [[NSString alloc] initWithString:#"cat+dog+cow"];
NSArray *resultArray = [tempString componentsSeparatedByString:#"+"];
NSLog(#"1. %# 2. %# 3. %#",[resultArray objectAtIndex:0],[resultArray objectAtIndex:1],[resultArray objectAtIndex:2]);
Try This:
NSString *str = #"cat+dog+cow" ;
NSArray *array = [str componentsSeparatedByString:#"+"];
NSLog(#"%#",array) ;

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];