*
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
Author

need help with substring

shuhui hung
Greenhorn

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.
Like..


OCPJP 6
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
Sheriff

Joined: Oct 27, 2005
Posts: 19651
    
  18

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


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37991
    
  22
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
Greenhorn

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
 
Don't get me started about those stupid light bulbs.
 
subject: need help with substring
 
Similar Threads
MAX ID
How does this code works ?
Javascript expand won't work in Netscape
Regular Expression issue
Regular expression help