Compare the last digit of a string with that at the same place of another string
I need to compare 2 numeric stri开发者_C百科ngs in iOS. The first string, stored
, is a fixed long number, and I want to check that the last digit of the second string entered
is the same as the corresponding digit on the stored
string (which isn't necessarily the last digit).
For example...
entered = 123456789
stored = 12345678902345678
In this example, I'd like to check that 9
on entered
is the same as the digit in the same position on stored
.
I'm thinking there's 2 possible ways of achieving this, but if there's a simpler way that'd be great...
- Check the
entered
stringlength
and compare the character at that position ofentered
andstored
. - Check if the
entered
string is equal to the value of the correspondinglength
instored
.
Could someone please offer some advice in this area.
Breaking up the 'about to get stored' string in character set and comparing the 9th element (or with whatever position of element you want to compare) with the 9th element of the existing stored string.
Have a look at this: Breaking NSString 'word' in characters..
After breaking up 'about-to-get-stored' NSString
in characters, take the 9th element and then get its intValue
(like [lastElement intValue];
) and on the other side retrieve your 'already-stored' NSString
, break up in characters, take the 9th element and then get its intValue
(as shown above) and now you have two int
values, you can easily compare two integers..
Hope that helps a little. :)
精彩评论