This week's book giveaway is in the Android forum.
We're giving away four copies of Head First Android and have Dawn & David Griffiths on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes need help with substring Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Head First Android this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "need help with substring " Watch "need help with substring " New topic

need help with substring

shuhui hung

Joined: Mar 16, 2011
Posts: 3
Dear Friend:
this is a javascrip code,how can i transfer to java? i try but not work. thank for helping
String id="AB09876541"

/* java scrip code */
if ( id.substr(0,1)<"A" ||id.substr(0,1)>"Z" ) return false;

/* java * this is not work in java ,how to modify ?/

if ( id.substring(0,1)<"A" ||id.substring(0,1)>"Z" ) return false;
Vinoth Kumar Kannan
Ranch Hand

Joined: Aug 19, 2009
Posts: 276

Welcome to JavaRanch

In Java, you cannot compare 2 Objects(in this case - Strings) with '>' or '<'.
You can use the compareTo() method of the String class.

Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

or you can use string.charAt(0) and compare it with > and <

"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
Rob Spoor

Joined: Oct 27, 2005
Posts: 20041

But then you need to compare to 'A' and 'Z' (chars), not to "A" and "Z". But that's what I would do too.

How To Ask Questions How To Answer Questions
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 43282
Why are you using compareTo() in the first place? There are lots of methods in the Character class which allow you to tell whether a char is a letter, a number, etc., etc. I think one of those methods would provide a more elegant solution to your problem.
shuhui hung

Joined: Mar 16, 2011
Posts: 3
Dear friend:
thank for helping. i know how to do this.
i think i will use string.charAt(0) to compare
I agree. Here's the link:
subject: need help with substring
jQuery in Action, 3rd edition