• Post Reply Bookmark Topic Watch Topic
  • New Topic

grabbing first character of a string  RSS feed

 
David LaVergne
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I need to know a way to grab the first character of a string and save it as its own variable. For example, if you have the word 'house' or the number '16578', I want to be able to have the program pick out the first letter 'h' or the first number '8'. I realize that there may be many diffents ways to do this so please give any advice you can on how to accomplish any part of this.
Thank you.
 
Elouise Kivineva
Ranch Hand
Posts: 154
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can grab the first character using a String class method that returns a char, and ask it to give you the char at index 0.
You COULD also get the ones digit of any whole number using this same method. Find a String method that tells you how long the String is (which will tell you at what index the last char is located. NOTE it does not give you the index, but it tells you what you need to know to find it). Then use the same method you used above but ask it for the char from the last index, not the first.
Look at the String class and its methods in the Java API http://java.sun.com/j2se/1.4.2/docs/api/
The "proper" way to do the second part would be be to use the % operator. But anything that works, esp. if it's more simple to understand, is proper enough in my book.
I'm not trying to be cute with mysterious hints. We're not supposed to do your homework for you.
[ March 31, 2004: Message edited by: Elouise Kivineva ]
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JavaRanch, David!
Luckily what you're wanting to do isn't too tough.
Instead of a Rancher just giving you the answer, let me instead tell you how and where to find the answer.
Take a look at the J2SE API documentation for String class documentation. It should give you an idea of what methods are available to do what you're wanting to do.
Were you able to understand the documentation and find the method?
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you should take some time to familiarize yourself with the Java API documentation. (I've given the link to the docs for the Java 1.4.2 API here, but you can go here to find the docs for previous versions if you are using one of those.) In searching the docs, you will find that the String class has a charAt() method that returns the character at a given index. You can use this to solve the problem at hand.
Again, I strongly encourage you to learn how to navigate the API docs. They are a great tool to help you find the class and/or method to use for a particular situation.
HTH
Layne
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Woohoo! Massive simul-post ;-)
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!