Checking if a nsstring matches another string - iphone

Suppose I have a string "167282". How can I check if the string contains "128"? Is there any provision to know the percentage of the 2nd string that matches the first string? (If all the characters of the 2nd string are present in the first string or not.) Please help and thanx in advance.

Use the following:
NSString *compareString = #"128";
NSCharacterSet *compareCharSet = [NSCharacterSet characterSetWithCharactersInString:#"167282"];
NSUInteger strLen = [compareString length];
NSUInteger matchingCount = 0;
for (NSUInteger i = 0; i < strLen; ++i) {
if ([compareCharSet characterIsMember:[compareString characterAtIndex:i]])
++matchingCount;
}
float percentMatching = matchingCount/(float)strLen;
Where matchingCount will be the number of characters in compareString that match a character in #"167282" and percentMatching will be the percent of total characters in compareString that match. This is, as best as I can tell, what you intended with your question - the concept of a percent match wouldn't make any sense otherwise.

You can use NSString's rangeOfString method to find out whether a string contains another string, as suggested by the answers to this question on the Apple Mailing Lists.
if ([#"167282" rangeOfString:#"128"].location != NSNotFound) {
NSLog(#"String contains '128'.");
}
else {
NSLog(#"String doesn't contain '128'.");
}

You could always use the NSString method rangeOfString: to check whether or not your original string contains your desired substring. This method will return an NSRange with the location of your substring within the original string:
[#"12345" rangeOfString:#"123"]; // Gives range {0, 3}
[#"12345" rangeOfString:#"345"]; // Gives range {2, 3}
[#"12345" rangeOfString:#"678"]; // Gives range {NSNotFound, 0}
This only works if the substring is entirely contained within the original string.
As for the "amount correct," you can use the length property of the returned range, along with the length of the original string, to see what part of the original string is comprised by the substring. For example:
NSRange r = [#"1234" rangeOfString:#"1"]; // Range {0, 1}
float portion = (float)r.length / (float)[#"1234" length]; // Gives 0.25f
Keep in mind this will fall apart if the substring is repeated - checking for #"1" as the substring of #"111" will give only the first occurrence, meaning it'll claim the string is "33% correct" by this method. Depending on your intended use, this may or may not be what you want.

Related

Counting same letters in a word

I need to count same letters in a word. For example: apple is my word and first I found whether 'a' exists in this letter or not. After that I want to count the number of 'a' in that word but I couldn't do that. This is my code which finds the specific letter;
if([originalString rangeOfString:compareString].location==NSNotFound)
{
NSLog(#"Substring Not Found");
}
else
{
NSLog(#"Substring Found Successfully");
}
originalString is a word which I took from my database randomly. So, how to count?
Thanks for your help.
i have different idea let's try...
NSString *string = #"appple";
int times = [[string componentsSeparatedByString:#"p"] count]-1;
NSLog(#"Counted times: %i", times);
NSString *strComplete = #"Appleeeeeeeeeeeeeee Appleeeeeeeee Aplleeeeeeeeee";
NSString *stringToFind = #"e";
NSArray *arySearch = [strComplete componentsSeparatedByString:stringToFind];
int countTheOccurrences = [arySearch count] - 1;
Output :
countTheOccurrences --- 34
You could just loop over the string once, adding each letter to an NSMutableDictionary (as the key) and keeping a tally of how many times the letter occurs (as the value).
The resulting NSMutableDictionary would hold the number of occurences for each unique letter, whereby you can extract what you like.

Check if textfield text is almost equal to string

I have a UITextField called textfield. And I have this code to check if the text in the textfield is equal to "exampletext"
if ([textfield.text isEqualToString:#"exampletext"]) {
NSLog(#"Correct");
} else {
NSLog(#"Wrong");
}
But I also want to check if the text in the textfield is almost equal to "exampletext", if the text is almost the same as "exampletext". Like if the text was "eampletex" I want to NSLog(#"Close")
Are there any ways to check if the textfield text is like 50% equal to "exampletext"?
Or any ways to check if the textfield text has 50% the same characters as "exampletext"?
Or something else like that?
What you are looking for is an implementation of the levenshtein distance, levenshtein("hello", "hallo") => 1, levenshtein("hello", "ellos") => 2. You can check this library.
Once you have the distance between the two strings, you could get it as a percentage calculating: percentage = 100 * levenshtein(original,other) / length(original)
Here's my go at it. Create a custom character set from the string you want to match. Check each character in the texfield.text against that character set, and if the number of matches is close to the number of letters in the string, do something..
NSString *testString = #"wordToCompare";
NSString *textFromTextfield = textfield.text;
//create a custom character set from the word you want to compare to...
NSCharacterSet *characterSetForString = [NSCharacterSet characterSetWithCharactersInString:testString];
//keep track of how many matches...
int numberOfCharsThatMatchSet = 0;
for (int x = 0; x < [textFromTextField length]; x++) {
unichar charToCheck = [textFromTextField characterAtIndex:x];
if ([characterSetForString characterIsMember:charToCheck] == YES) {
numberOfCharsThatMatchSet++;
}
NSLog(#"%d", numberOfCharsThatMatchSet);
}
// if the number of matches is the same as the length of the word + or - 2...
if ((numberOfCharsThatMatchSet > [testString length] - 2 ) && (numberOfCharsThatMatchSet < [testString length] + 2 )) {
NSLog(#"close match...");
}
Not sure if this is 100% what you're looking for, but maybe it will help anyway...
I'm sure there might be some open source out there somewhere that would do this for you..however, one approach I can think of that will give you a bit of a lead...
Sort out the characters of both your strings into arrays. Determine which string you want to be the master string and grab the string length of it.
Now compare each character. Ex: Word 1: hello, Word 2: ello.
Each time a letter is found add one to a count. If by the end of your looping your count is 80% of the original length you grabbed from the master string or greater then you most likely have a partial match.
So for our example Word 1 will be our master string and its length is 5. "ello" contains 4/5 characters and therefore is matches 80% of the original string.
I don't think there is an easy way (with several lines of code) of solving this. There are several algorithms you might consider and pick the one which suits your needs most.
You should look at this question. Although it has been designed and answered for another language, you asked for a way or method so you have your solution there.

Why is my NSRange always 0?

I want to implement a delete key for my calculator app. My pseudocode for this was:
foo = length of current number
bar = length of current number-1
current number = current number with character at index (foo-bar) removed
To do this in objective-c I have tried to implement this using the NSRange function as well as the StringbyappendingString method:
- (IBAction)DelPressed {
if ([self.display.text length] >=2)
{
int len = [self.display.text length];//Length of entire display
//Add to the brainlong printing out the length of the entire display
self.brainlog.text = [NSString stringWithFormat:#"Len is %g",len];
int le = ([self.display.text length]-1);//Length of entire display - 1
NSString *lestring = [NSString stringWithFormat:#"LE is %g",le];
//Add to the brainlog printing out the length of the display -1
self.brainlog.text = [self.brainlog.text stringByAppendingString:lestring];
NSRange range = NSMakeRange(le, len);//range only includes the last character
self.display.text = [self.display.text stringByReplacingCharactersInRange:range withString:#" "];//Replace the last character with whitespace
}
The output to brainlog (ie the length of both le and len) is:
Len is -1.99164 Le is -1.99164
Hard to see, but here's an image of the number I input to the calculator using the iOS simulator:
I have tried to change the value of le (ie making it the length of the display -3 or -5 etc) and both le and len are still the same.
I have also tried to make le in reference to len:
int len = [self.display.text length];//Length of entire display
//Add to the brainlong printing out the length of the entire display
self.brainlog.text = [NSString stringWithFormat:#"Len is %g",len];
int le = len-1;
But the values of the two are still the same. How can I use NSRange to delete the last character of the display?
Edit: Using Dustin's fix, I now have reduced a rather lengthy function into 6 lines of code:
-(IBAction)DelPressed{
if ([self.display.text length] >=2)
{
self.display.text = [self.display.text substringToIndes:[self.display.text length] -1];
}
}
Use substring instead; it's much better if all you want to do is remove the last character.
More specifically, substringToIndex(/*length-1*/)
Check this reference out if you need to do other things with strings

iPhone iOS how to count number of case-insensitive occurrences of a word within a string?

I'm looking for a way to search an arbitrary long string (10000 characters) and find the number of times a specific keyword is repeated in the string. How can this be done?
I have this method, that pretty much counts the number of fragments left after the string is split around keywords, but it is not case insensitive.
-(void)countKeywords
{
NSArray* components = [self.salesCopy componentsSeparatedByString:#"search term"];
NSLog(#"search term number found: %i",components.count);
}
What's a better way to count the number of keywords within a string?
Splitting the string, counting parts, and throwing them away is not efficient. Searching for substring repeatedly without creating new objects would definitely be more efficient. Since the string is relatively long, you may benefit from implementing an advanced string search algorithm, for example Knuth-Morris-Pratt, to significantly decrease your search time.
Here is an implementation that should be faster than your splitting code:
NSString *str = #"Hello sun, hello bird, hello my lady! Hello breakfast, May I buy you again tomorrow?";
NSRange r = NSMakeRange(0, str.length);
int count = 0;
for (;;) {
r = [str rangeOfString:#"hello" options:NSCaseInsensitiveSearch range:r];
if (r.location == NSNotFound) {
break;
}
count++;
r.location++;
r.length = str.length - r.location;
}
NSLog(#"%d", count);
Just create copies of both self.salesCopy and the searchTerm, set the copies to lower case via [NSString lowercaseString], then perform your code, and you'll have the count
-(void)countKeywords
{
NSString *lowerCaseSalesCopy = [self.salesCopy lowercaseString];
NSString *lowerCaseSearchTerm = [searchTerm lowercaseString];
NSArray* components = [lowerCaseSalesCopy componentsSeparatedByString:lowerCaseSearchTerm];
NSLog(#"search term number found: %i",components.count);
}
I am not 100% sure that could help you, but may do some of the job you need (if not all):
NSRange ran = [yourString rangeOfString:wordToLookFor options:NSCaseInsensitiveSearch];
And look at
ran.length
ran.location
ran.location will provide you the location within the string of the first occurrence. You could then cut the string after this occurrence, and run this again until the end of the string.

Compare, Getting string from a label

I'm making a word game and I've finally come up to one of the most important parts of my game, the compare part.
I got this label which will be invisible when it launches with the word that has to be guessed displaying in it with a random word generator. For example the word is: GARAGE
Now, for my game I have to compare the word with the input now I've already done this with the entire word with NSString but I want it to compare every letter. I want to be able to show that if the input has G as the first letter aswell, like garage. I want it to do something.
I want to know if this is possible and which methods you would use. I was thinking about making 6 strings since all my random words have 6 letters, and then break the word to 6 strings and the input aswell and then compare strings?
Hope someone has some usefull tips or example code thanks
So, assuming your string to be guessed...
NSString *stringToGuess = #"GARAGE";
and you were checking to see if it started with "GA"
NSString *myString = #"GA";
you would check it with hasPrefix:
if ([stringToGuess hasPrefix:myString]) {
// <stringToGuess> starts with <myString>
}
The documentation for NSString describes lots of neat methods for just about anything string related.
hasPrefix will let you tell if one string begins with another string. There's also characterAtIndex. You could use that to get one character from each string and compare it to the other.
You could write a method that would take an integer index and compare the two strings at that index:
- (BOOL) compareStringOne: (NSString *) stringOne
toStringTwo: (NSString *) stringTwo
atIndex: (NSUInteger) index;
{
if ([stringOne length] < index+1 || [stringTwo length] < index+1)
return FALSE;
return [stringOne characterAtIndex: index] == [stringTwo characterAtIndex: index];
}