Chaos Project

Game Development => Sea of Code => Topic started by: G_G on January 23, 2010, 12:45:05 am

Title: Getting Certain Index of String (resolved)
Post by: G_G on January 23, 2010, 12:45:05 am
I know how to do it in RGSS. I've done it before. And it'd really help out here for a cool idea I have. Anyways pretty much I'm trying to get a certain letter or index from a string.
Example:
"Apple" - I may just one to get the A, or the P, or LE. Is it possible to do this?
Title: Re: Getting Certain Index of String
Post by: winkio on January 23, 2010, 01:25:25 am
IndexOf will get you the index
with the index, the easiest way would be to use Substring to get the characters you need.
Title: Re: Getting Certain Index of String
Post by: G_G on January 23, 2010, 01:41:10 am
Okay that sorta helps. I was just about to edit my post to be more explanitory as well.

See in my program its not gonna be apple. Its going to be a number. For example. A text box will consist of this.
"1679"
Pretty much the program will be asking for a passcode using a simple algorithm.
*1st is random between 0-9
*2nd is 1st + 5. If over 10 choose 1's digit.
*3rd is 1st+2nd. If over 10 choose 1's digit.
*4th is 3rd + 2. If over 10 choose 1's digit.
So when the code is checked its going to get the string of 1st+5. If its over 10 I need to get the last number. Aka the ones digit. But its still all a string.

Example:
1st: 8
2nd: 8+5=13 ~ Instead of the 2nd number being 13 it'll be 3. Just getting the ones digit.
3rd: 8+3=11 ~ Instead of the 3rd number being 11 it'll be 1. Just getting the number from the ones digit.
And so on.

Really how would I just get the 1's digit?
Title: Re: Getting Certain Index of String
Post by: winkio on January 23, 2010, 01:51:20 am
strings have a handy property called Length.  if the ones digit is at the end of the string, just call a substring for the last character:

string s = "12341"
string ones = s.Substring(s.Length-1)

(don't need the second argument of substring because we are already at the end of the string)

Your other option, of course, would be to parse it to an integer, then perform % 10, then convert the result back to a string.
Title: Re: Getting Certain Index of String
Post by: G_G on January 23, 2010, 01:55:24 am
Oh wow thanks winkio that really worked! Phew, I've had a brain blockage here for awhile >__> thanks :)