Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Splitting a string (newbie question)  RSS feed

 
E Wong
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there,
i need some help with a verification system that i'm trying to do at the moment.

The scenario is as follows:

A user can login by entering a string that either consists of
a: 2 alphabets and 8 digits (AB12345678) (old format)
b: 10 digits (1234567891) (new format)

My problem is verifying the first situation. I'm not sure as to how to inform the system that the login is via situation A and not situation B.

What i have done so far is as follows...
If login method = situation1,
-get char at position, getNumericValue of char, total it up and see if it is in the required range. If yes, user is verified, if no, reject.

However, before i can add that part of coding, i need to see if the first 2 values of the string is a character or an integer.

Hopefully someone can shed some light on my problem.

Best regards,
E Wong
[ September 20, 2004: Message edited by: E Wong ]
 
Elouise Kivineva
Ranch Hand
Posts: 154
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would use a String method to create a new String from the first two chars in the password, then try to get an int from this String. If this attempt throws a NumberFormatException I can catch the exception and know I have two letters.

You could create a boolean "isNewStyle" = true.
If you enter the catch block it sets this boolean to false.

After the try/catch this boolean tells you whether the password was new syle (all digits) or not.
 
Joyce Lee
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How about using regular expression?

For example:


You can refer to Sun Tutorial on Regular Expression for more info.

Joyce
 
E Wong
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks for the quick response!

much appreciated, will try it out now.
 
Daniel Mayer
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Instead of [a-zA-Z] you can also write \p{Alpha}
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!