Compare Phone Numbers with different format - ios

Phone nubmber can have different Formats , like 0XXXXXXXXXX or +91XXXXXXXXXX or XXXXXXXXXX.
In all cases , phone number is similar.
How can i compare ?

Please do this for love of god: Handling all formats of phone numbers is as tricky and hairy as handling different data formats. It's always a sane option to use some existing "mature" library. Use PhoneNumberKit, its one of the mature and popular libraries out there.
But if you want to live on the edge, you can try something like this -
Reverse the string & compare only the first 10 chars.
Regular Expressions would be a better solution. Have as many regular expressions as there are formats & you are good to go.
Please open source this library for others to benefit :)
General reading on handling phone numbers

You could start comparing right to left using a custom character by character match code. It also works because the last few digits on phones vary more than the first few, so your code will fail fast in a unequal comparison which is what you want anyways. Keep a count of how many matches you have and if it passes a certain threshold say 10, then call the number equal. Without know more specifics this would be made to work reasonably well.

As Mentioned by srikar to reverse the string and compare 10 characters.Create the method and call it for all the three strings and after that compare them.
NSMutableString *reversedString = [NSMutableString string];
NSInteger charIndex = [myString length];
NSInteger count;
count=10;
while(count >= 0) {
count--;
charIndex--;
NSRange subStrRange = NSMakeRange(charIndex, 1);
[reversedString appendString:[myString substringWithRange:subStrRange]];
}

Related

Character Replacements

I have a UniCode string UniStr.
I also have a MAP of { UniCodeChar : otherMappedStrs }
I need the 'otherMappedStrs' version of UniStr.
Eg: UniStr = 'ABC', MAP = { 'A':'233','B':'#$','C':'9ij' }, Result = '233#$9ij'
I have come up with the formula below which works;
=ArrayFormula(JOIN("",VLOOKUP(REGEXEXTRACT(A1,REPT("(.)",LEN(A1))),MapRange,2,FALSE)))
The MAP being a whole character set (40 chars) is quite large.
I need to use this function in multiple spreadsheets. How can I subsume the MAP into the formula for portability ?
Is there a better way to iterate a string other than the REGEXEXTRACT method in formula ? This method has limitation for long strings.
I also tested the below formula. Problem here is it gives 2 results (or the size of the array within SUBSTITUTE replacement). If 3 substitutions made, then it gives three results. Can this be resolved ?
=ArrayFormula(SUBSTITUTE(A1,{"s","i"},{"#","#"}))
EDIT;
#Tom 's first solution appears best for my case (1) REGEX has an upper limit on search criteria which does not hinder in your solution (2) Feels fast (did not do empirical testing) (3) This is a better way to iterate string characters, I believe (you answered my Q2 - thanks)
I digress here. I wish google would introduce Named-Formulas or Formula-Aliases. In this case, hypothetically below. I have sent feed back along those lines many times. Nothing :(
MyFormula($str) == ArrayFormula(join(,vlookup(mid($str,row(indirect("1:"&len($str))),1), { "A","233";"B","#$";"C","9ij" },2,false)))
Not sure how long you want your strings to be, but the more traditional
=ArrayFormula(join(,vlookup(mid(A1,row(indirect("1:"&len(A1))),1), { "A","233";"B","#$";"C","9ij" },2,false)))
seems a bit more robust for long strings.
For a more radical idea, supposing the maximum length of your otherMappedStrings is 3 characters, then you could try:
=ArrayFormula(join(,trim(mid("233 #$9ij",find(mid(A1,row(indirect("1:"&len(A1))),1), "ABC")*3-2,3))))
where I have put a space in before #$ to pad it out to 3 characters.
Incidentally the original VLOOKUP is not case sensitive. If you want this behaviour, use SEARCH instead of FIND.
You seem to have several different Qs, but considering only portability, perhaps something like the following would help:
=join(,switch(arrayformula(regexextract(A1&"",rept("(.)",len(A1)))),"A",233,"B","#$","C","9ij"))
extended with 37 more pairs.

How to code for a password needing a number and capital letter in objective c

Looking for coding or ideas to help me get a good start on ways for a program to check, after tapping a button, the a UITextField for at least one capital letter, at least one number and a length >= 6. Looking towards if statements so the else code could send out an alert telling its missing something.
am pretty much new to coding and was directed by my teacher to ask on here, thank you
I would recommend checking each one individually and then only logging on (progressing) if they all come back ok
Checking the length
// Check if the text is a certain length
if (textField.text.length >= 6) {
}
Checking that is contains a number
if ([textField.text rangeOfCharacterFromSet:[NSCharacterSet decimalDigitCharacterSet]].location != NSNotFound) {
}
Checking that it contains an uppercase character
NSString * string = textField.text;
int count = 0;
for (i = 0; i < [s length]; i++) {
BOOL isUppercase = [[NSCharacterSet uppercaseLetterCharacterSet] characterIsMember:[s characterAtIndex:i]];
if (isUppercase == YES)
count++;
}
This code is from here and loops through the string checking if there are upper case characters contained in it. You can then check the value of count to see if there is at least one it in. If you want a stronger password you can increase this check too.
In future though this is all quite basic stuff which just requires time to find out instead of knowledge of C. I didn't know how to do any of this before having a look to write this answer.
Hopefully this helps your understanding and, going forward, try spending 30 minutes searching with google before posting a duplicate question
Some more good questions on this can be seen below.
This answer here: is almost the same as the one you have asked
This answer also covers special characters: here
As Hot Licks says, this is not a "write my code for me" site. This is a site to get help with specific problems.
Take a look at the NSString class reference in Xcode. You should read the whole thing. It lists lots and lots of very useful methods. You won't understand it all at first, but note methods that sound like the would be useful.
Big hint: You'll also want to take a look at the NSCharacterSet class, and NSString methods that use NSCharacterSet to search in a string for characters that belong to specific set of characters.
Note that you could probably also use regular expressions. Cocoa includes the NSRegularExpression class, which lets you apply regular expressions to strings.

How should i interact with a base of words in iPhone app?

I'm making a word game for iPhone. User can construct words from separate letters. And i need to check is this a real word, which user made, or some kind of pointless stuff like "sfugh". How i should better implement this? Should i have kind dictionary in program(I think it's a bad idea, because with a large amount of words it would work very slow) or maybe there is kind of online dictionaries, where i can check the word from my code, or should i make a trie? Or maybe where is other solutions?
Use a pre-populated sqlite database. That way, you can use string indexes to make the searches in the db faster.
You can use Apple's native UITextChecker to determine whether or not a word is misspelled/valid. Here's a sample project on GitHub. This way, you don't have to worry about assembling your own database full of words.
For easy digestion, here's the code that does all the work
- (IBAction)textFieldTextDidChange:(id)sender {
NSRange rangeOfMisspelling = [self.textChecker rangeOfMisspelledWordInString:self.textField.text
range:NSMakeRange(0, self.textField.text.length)
startingAt:0
wrap:NO
language:#"en_US"];
// no misspellings
if (rangeOfMisspelling.location == NSNotFound) {
self.textLabel.text = #"Valid word!";
}
// misspellings
else {
self.textLabel.text = #"Not a word.";
}
}
One thing to note is UITextChecker seems to count individual letters as valid words.

How to show five digit unicode in iphone

i am trying to show \u1F318 in my application. but iphone app just use first 4 digit and and create the image. Can any one guide me what i am doing wrong to show image of unicode \u1F318 in iPhone.
[(OneLabelTableViewCell *)cell textView].text = #"\u1F318";
out in application is
Note: this answer is based on my experience of Java and C#. If it turns out not to be useful, I'll delete it. I figured it was worth the OP's time to try the options presented here...
The \u escape sequence always expects four hex digits - as such, it can only represent characters in the Basic Multilingual Plane.
If this is Objective-C, I believe that supports \U followed by eight hex digits, e.g. \U0001F318. If so, that's the simplest approach:
[(OneLabelTableViewCell *)cell textView].text = #"\U0001F318";
If that doesn't work, it's possible that you need to specify the character as a surrogate pair of UTF-16 code points. In this case, U+1F318 is represented by U+D83C U+DF18, so you'd write:
[(OneLabelTableViewCell *)cell textView].text = #"\uD83c\uDF18";
Of course, this is assuming that it's UTF-16-based...
Even if that's the correct way of representing the character you want, it's entirely feasible that the font you're using doesn't support it. In that case, I'd expect you to see a single character (a question mark, a box, or something similar to represent an error).
(Side-note: I don't know what # is used for in Objective-C. In C# that would stop the \u from being an escape sequence in the first place, but presumably Objective-C is slightly different, given the code in your question and the output.)

How to get a % difference of two NSStrings

I'm thinking this may be impossible to do resonably, but I figured I would take a shot at it. So lets say I have two NSStrings. One is #"Singin' In The Rain" and the other is #"Singing In The Rain". These strings are very similar, but have a small difference. I'm trying to find a way where I could write something like the following:
NSString *stringOne = #"Singin' In The Rain";
NSString *stringTwo = #"Singing In The Rain";
float dif = [stringOne differenceFrom:stringTwo];
//dif = .9634 or something like that
One project that I did find similar to this was taken from the previous similar question on Stack Overflow: Check if two NSStrings are similar. However, this simply returns a BOOL which isn't as accurate as I need it to be. I also tried looking into the compare: documentation for NSString but it all looked too basic. Another similar thing I found is at https://gist.github.com/iloveitaly/1515464. However, this gives varying results, even saying two of the same string are different occasionally. Any advice would be much appreciated.
The question is a little vague, but I would assume that the most satisfactory results will come from using NSLinguisticTagger. If you parse each for tags with the NSLinguisticTagSchemeLexicalClass scheme then your string will be broken down into verbs, nouns, adjectives, etc. In your example, even if you weren't spotting that singin' and singing are the same, you'd spot the other three words are the same and that the thing at the end is a noun, so they're both about doing something in the same thing.
It'd probably be wise to use something like a BK-Tree to compare individual words where you suspect there may be a match (a noun obviously doesn't match an adverb but two nouns may match even if spellings differ).
Another off the wall suggestion:
The source, and hence the algorithm, for diff and similar programs is easily available. These compare input on a line-by-line basis and detect insertions, deletions and changes.
When comparing text strings for "closeness" then the insertion, deletion or changing of words seems as good a measure as any.
So:
Break each string into "words" (white space separated should be sufficient).
Compare the two lists using the diff algorithm, treating each "word" as a "line", use a re-sync length of 1 (the number of "lines" that need to be the same to treat the two inputs as back in sync)
Calculate the "closeness" as the number of insertions/deletions/changes compared to the total word count.
For the two example strings this would give 1:4 changes or 75% similar.
If you want greater granularity for each change split the two words into characters and repeat the algorithm giving you a fraction the word is similar by (as opposed to the whole word).
For the two example strings this would give 3 6/7 words out of 4, or 96% similar.
I'd recommend dynamic time warping for such comparisons:
http://en.wikipedia.org/wiki/Dynamic_time_warping
This will however return distance between two strings (so you'll get 0 for identical), but this the best starting point I can think of.

Resources