Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / June 2005

Tip: Looking for answers? Try searching our database.

How do I extract last character from string

Thread view: 
lightonlight - 17 Jun 2005 08:52 GMT
I have problem to solve
where the value is ending with c or f
lets say its 100c entered
then a I should extract the last character and then
apply a equation

if I enter 100f, then extract f and apply another equation

I know there is string.length();  method
then what

How to use it.

sorry this seems to be extremely BASIC question
but i need to do ASAP

thanks inadvance
Skip - 17 Jun 2005 10:08 GMT
> I have problem to solve
> where the value is ending with c or f
[quoted text clipped - 11 lines]
> sorry this seems to be extremely BASIC question
> but i need to do ASAP

String value = ......;
char last = value.chatAt(value.length()-1);
or
String last = value.substring(value.length()-1);

just check the Javadoc of java.lang.String for all other methods.
Aki Laukkanen - 17 Jun 2005 10:14 GMT
> I have problem to solve
> where the value is ending with c or f
[quoted text clipped - 8 lines]
>
> How to use it.

To start with:

char c = String.getCharAt(String.length-1);
if (c == 'c'){
  //equation 1
}
else{
  if(c == 'f'){
    //equation 2
  }
  else{
    //and now for something completely different
  }
}

Then you need to find a way to remove the letter and convert the numbet
to an int/long/float/double, whichever best suits your needs.

See StringBuffer.deleteCharAt(int index), String.substring(int begin,
int end), Integer.parseInt(String), Long.parseLong(String),
Double.parseDouble(String), for example.
THe definitions for these are easy to find at <http://java.sun.com> or
the documentation delivered with the JDK. (These will doubtlessly answer
many of your future questions as well.)

Signature

-Aki "Sus" Laukkanen

Thomas G. Marshall - 21 Jun 2005 00:48 GMT
Aki Laukkanen coughed up:

>> I have problem to solve
>> where the value is ending with c or f
[quoted text clipped - 12 lines]
>
> char c = String.getCharAt(String.length-1);

WHAT!?

> if (c == 'c'){
>   //equation 1
[quoted text clipped - 17 lines]
> the documentation delivered with the JDK. (These will doubtlessly
> answer many of your future questions as well.)

Signature

"Well, ain't this place a geographical oddity!
Two weeks from everywhere!"

redgambit - 21 Jun 2005 19:23 GMT
You could use the endsWith() method of a string in a if statment.

if (String.endsWith(f)) {
whatever
}
if (String.endsWith(c)){
whatever
}


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.