Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 02-27-2011, 12:04 AM   #1 (permalink)
Registered Member
 
Join Date: Feb 2011
Posts: 1
CodingNoob is on a distinguished road
Default Compare the last digit of a string with that at the same place of another string

Hi there. I'm new to this forum and to iPhone programming so I'd like your help. I need to compare 2 strings (kind of); "entered" and "stored". The stored string is a fixed long number and I want to check that the last digit of "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....

Here, Id 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 ways but if there's a more simple way, your help would be appreciated...

1) check the "entered" string length and compare the character at that position of "entered" and "stored".

2) Check if "entered" string is equal to the value of the corresponding length in "stored".

If you have any insight, code snippet that might help, please let me know.
CodingNoob is offline   Reply With Quote
Old 02-27-2011, 01:26 AM   #2 (permalink)
Registered Member
iPhone Dev SDK Supporter
 
smasher's Avatar
 
Join Date: Jul 2008
Location: San Mateo, CA (San Fran)
Posts: 3,858
smasher will become famous soon enough
Default

NSString has a "characterAtIndex:" method you can use to get one character of a string; then you can compare it to the same character of the other string.

Code:
NSUInteger lastIndex = [entered length] -1;
unichar lastChar = [entered characterAtIndex:lastIndex];
unichar storedChar = [stored characterAtIndex:lastIndex];

BOOL match = (lastChar==storedChar)
if(match)
     NSLog(@"It's a match!");
I didn't check that "stored" is longer or as long as "entered;" you really should though, to prevent a crash.
__________________

Free Games!
smasher is offline   Reply With Quote
Reply

Bookmarks

Tags
compare, length, string

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 370
18 members and 352 guests
Absentia, akphyo, apatsufas, BinHex, cpsclicker, dre, Error404, Gaz, gmarro, jeroenkeij, Kirkout, MarkC, mottdog, Music Man, PavelMik, teebee74, whitey99, Wikiboo
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,666
Threads: 94,120
Posts: 402,898
Top Poster: BrianSlick (7,990)
Welcome to our newest member, cpsclicker
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 03:16 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0