File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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
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: 19844

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: 40418
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