Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

need help with substring

 
shuhui hung
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 276
Chrome Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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..
 
Wouter Oet
Saloon Keeper
Posts: 2700
IntelliJ IDE Opera
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
or you can use string.charAt(0) and compare it with > and <
 
Rob Spoor
Sheriff
Pie
Posts: 20667
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But then you need to compare to 'A' and 'Z' (chars), not to "A" and "Z". But that's what I would do too.
 
Campbell Ritchie
Sheriff
Pie
Posts: 50245
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear friend:
thank for helping. i know how to do this.
i think i will use string.charAt(0) to compare
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic