将一个字符串的最后一位数字与另一个字符串相同位置的最后一位数字进行比较
我需要在 iOS 中比较 2 个数字字符串。第一个字符串stored
是一个固定的长数字,我想检查第二个字符串entered
的最后一位数字是否与上的相应数字相同code>stored
字符串(不一定是最后一位数字)。
例如...
entered = 123456789
stored = 12345678902345678
在此示例中,我想检查 entered
上的 9
是否与 stored
上相同位置的数字相同代码>.
我认为有两种可能的方法可以实现此目的,但如果有一种更简单的方法那就太好了...
- 检查
输入
字符串长度
并比较字符输入
和存储
的位置。 - 检查
输入
字符串是否等于存储
中对应length
的值。
有人可以在这方面提供一些建议吗?
I need to compare 2 numeric strings 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
分解字符集中的“即将存储”字符串,并将第 9 个元素(或与您想要比较的元素的任何位置)与现有存储字符串的第 9 个元素进行比较。
看看这个: 将 NSString 'word' 分解为字符..
将 'about-to-get-stored'
NSString
分解为字符后,取出第 9 个元素,然后获取其intValue
(如[lastElement intValue];
),另一边检索“已存储”的NSString
,按字符分解,取出第 9 个元素并然后获取其intValue
(如上所示),现在您有两个int
值,您可以轻松比较两个整数..希望有一点帮助。 :)
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 itsintValue
(like[lastElement intValue];
) and on the other side retrieve your 'already-stored'NSString
, break up in characters, take the 9th element and then get itsintValue
(as shown above) and now you have twoint
values, you can easily compare two integers..Hope that helps a little. :)