Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Distinguishing characters in a String

 
Nick Collings
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, im need to find a way of distinguishing between individual characters in a String according to their position in the String. I need them to be then stored in another Stringso that i can then analyse and rearrange them.
A String contains to word "Bob"
i would then like to have three other Strings (first, second and third) that hold the first second and third characters in the word:
eg.
first = b
second = o
third = b
Thanks
 
Michael Morris
Ranch Hand
Posts: 3451
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This sounds like homework. I bet you can figure this out for yourself if you try. Take a look at the javadocs (API) for the String class and see what it can do. There are several ways you could do this.
 
Natraj Gudla
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One simple way would be, to use the toCharArray() method in the String class , to get the string content to be represented as a character array and then the rest is simple to access the array memebers.
The other way could be to use the substring() method to get single character at a time, serially.
Hope this helps you.
regards
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The documentation that Micheal referred to is available at http://java.sun.com/j2se/1.4.2/docs/api/
You'll perhaps want to make use of the indexOf, charAt, and substring methods of the String class.
If you've further questions, do post them here. Just try to be specific about what part of the program you're trying to figure out.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic