Is it possible to make a function that searchs a string for an exact substring so that it will only 'return true' if the exact string if found, not as part of a larger word?
NSString* search = #"tele";
NSString* stringOne = #"telephone";
NSString* stringTwo = #"tele phone";
NSString* stringThree = #"phone tele";
What I mean is: Is it possible to search for a string in a way that the NSString 'search' would be found in strings Two and Three, but not One?
Try using the following function in the NSString class:
- (NSRange)rangeOfString:(NSString *)aString
http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/nsstring_Class/Reference/NSString.html
Simplest approach is to append blanks (or whatever your separators are) to front and rear of both strings, then do the search.
Related
I have a small doubt on splitting a string into substring.
I want to display State and Country name in one label. i have a string in a service coming from json file "FullAddress": "New Windsor,New York,USA". i want only New York,USA to display in a label.
Use this and get your desire String from Array element 1 and 2
NSArray *strings = [FullAddress componentsSeparatedByString:#","];
Please Use this code it will be help you out
NSString *fullAddress=#"New Windsor,New York,USA";
NSRange range=[fullAddress rangeOfString:#","];
if (range.location!=NSNotFound) {
NSString *string=[fullAddress substringFromIndex:range.location+range.length];//this is address you want
NSLog(#"%#",string);
}
I have a string representing fruits, separated by dot:
apple.orange.banana.watermelon
and each fruit may have a tag:
apple.orange.[old]banana.[juicy]watermelon
also there may be fruit names which are partially overlapping:
apple.orange.[old]banana.[juicy]strawberry.[fresh]berry
also the tags may be same as fruit names:
apple.orange.[old]banana.[berry]strawberry.[fresh]berry
I need to check if such a string contains a specified fruit, so given the above string and a fruit name, say "berry", I want to know the string contains "berry" and of course it should not tell me YES if "berry"'s not there but "strawberry" is.
A quick way came up in my mind is:
use componentsSeparatedByString to get an array of components (fruit names with tags)
go through each component, check if it has a tag (ie square brackets), remove it if YES
then check the remaining string is exactly the given fruit name
I cannot just use the whole string with rangeOfString because "berry" is a substring of "strawberry", I can't even first get components then check substring for each component because tags may be the same as fruit names.
I wonder is there any better way to do this? Better in terms of memory footprint and/or speed?
Thanks!
There are plenty of ways to do this.
You could use a regular expression.
You could split the string into parts.
But instead, let's notice that, when “berry” (the desired fruit) isn't the first or last fruit in the string, it's got to appear as either ]berry. or as .berry., and neither of those can match if “berry” is a substring.
So we can solve this problem quite simply putting a . on each end of the string before searching it for one of those patterns:
BOOL stringContainsFruit(NSString *string, NSString *fruit) {
string = [NSString stringWithFormat:#".%#.", string];
NSString *dotFruit = [NSString stringWithFormat:#".%#.", fruit];
if ([string rangeOfString:dotFruit].location != NSNotFound) {
return YES;
}
NSString *bracketFruit = [NSString stringWithFormat:#"]%#.", fruit];
if ([string rangeOfString:bracketFruit].location != NSNotFound) {
return YES;
}
return NO;
}
I am trying to find the Regular expression to extract a part of my string from the Entire sub string .Feel free to suggest me a best Way .
so here is the case , i can get any Eamil so lets say abd#gmail.com , or something similar ,
i want to remove just the Username that is "abd" and save into another string.
I don't think you'll need regular expressions for that. Just do this:
NSString* email = #"abd#gmail.com";
NSArray * components = [email componentsSeparatedByString:#"#"];
NSString* username = (NSString *)[components objectAtIndex:0];
you alsa can do:
NSString* email = #"abd#gmail.com";
NSString* username = [email substringToIndex:[email rangeOfString:#"#"].location];
I have following strings. But I need to separate them by this "jsonp1343930692" and assign them NSString again. How could I that? I could able to separate them to NSArray but I don't know how to separate to NSString.
jsonp1343930692("snapshot":[{"timestamp":1349143800,"data":[{"label_id":10,"lat":29.7161,"lng":-95.3906,"attr":{"ozone_level":37,"exp":"IN","gridpoint":"29.72:-95.39"}},{"label_id":10,"lat":30.168456,"lng":-95.50448}]}]})
jsonp1343930692("snapshot":[{"timestamp":1349144700,"data":[{"label_id":10,"lat":29.7161,"lng":-95.3906,"attr":{"ozone_level":37,"exp":"IN","gridpoint":"29.72:-95.39"}},{"label_id":10,"lat":30.168456,"lng":-95.50448,"attr":{"ozone_level":57,"exp":"IN","gridpoint":"30.17:-95.5"}},{"label_id":10,"lat":29.036944,"lng":-95.438333}]}]})
The jsonp1343930692 prefix in your string is odd: I don't know where you string come from, but it really seems to be some JSON string with this strange prefix that has no reason to be there. The best shot here is probably to check if it is normal to have this prefix, for example if you get this string from a WebService it is probably the WebService fault to return this odd prefix.
Anyway, if you want to remove the jsonp1343930692 prefix of your string, you have multiple options:
Check that the prefix is existant, and if so, remove the right number of characters from the original string:
NSString* str = ... // your string with the "jsonp1343930692" prefix
static NSString* kStringToRemove = #"jsonp1343930692";
if ([str hasPrefix:kStringToRemove])
{
// rebuilt a string by only using the substring after the prefix
str = [str substringFromIndex:kStringToRemove.length];
}
Split your string in multiple parts, using the jsonp1343930692 string as a separator
NSString* str = ... // your string with the "jsonp1343930692" prefix
static NSString* kStringToRemove = #"jsonp1343930692";
NSArray* parts = [str componentsSeparatedByString:kStringToRemove];
str = [parts componentsJoinedByString:#""];
Replace every occurrences of jsonp1343930692 by the empty string.
NSString* str = ... // your string with the "jsonp1343930692" prefix
static NSString* kStringToRemove = #"jsonp1343930692";
str = [str stringByReplacingOccurrencesOfString:kStringToRemove withString:#""];
So in short you have many possibilities depending on what exactly you want to do :)
Of course, once you have removed your strange jsonp1343930692 prefix, you can deserialize your JSON string to obtain a JSON object (either using some third-party lib like SBJSON or using NSJSONSerializer on iOS5 and later, etc)
Have a look at the NSJSONSerialization class to turn this into a Cocoa collection that you can deal with.
I have the following NSString:
productID = #"com.sortitapps.themes.pink.book";
At the end, "book" can be anything.... "music", "movies", "games", etc.
I need to find the third period after the word pink so I can replace that last "book" word with something else. How do I do this with NSRange? Basically I need this:
partialID = #"com.sortitapps.themes.pink.";
You can try a backward search for the dot and use the result to get the desired range:
NSString *str = #"com.sortitapps.themes.pink.book";
NSUInteger dot = [str rangeOfString:#"." options:NSBackwardsSearch].location;
NSString *newStr =
[str stringByReplacingCharactersInRange:NSMakeRange(dot+1, [str length]-dot-1)
withString:#"something_else"];
You can use -[NSString componentsSeparatedByString:#"."] to split into components, create a new array with your desired values, then use [NSArray componentsJoinedByString:#"."] to join your modified array into a string again.
Well, although this isn't a generic solution for finding characters, in your particular case you can "cheat" and save code by doing this:
[productID stringByDeletingPathExtension];
Essentially, I'm treating the name as a filename and removing the last (and only the last) extension using the NSString method for this purpose.