Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Split a String

 
Zahid, Butt
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ALL
Does anyone know how to split a String into its constituent characters ??
e.g if you have a string say "saddle", how do I get hold of the character at position 1 or 2 ??
do I use the method charAt() ??? How do you use this method ???

Thanks in Advance.
 
Art Metzer
Ranch Hand
Posts: 241
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yup.
If String s = "saddle", then s.charAt( 1 ) = 'a', s.charAt( 5 ) = 'e', and s.charAt( 10 ) gives a StringIndexOutOfBoundsException.
See also this link.
Art
 
Matt Senecal
Ranch Hand
Posts: 255
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
String.charAt(int) returns the char at the specified location, just like it's defined:
String myString = new String("Matt");
char myChar = myString.charAt(0); //returns "M"
You can use String.toCharArray() to break a String down into an array of chars (char[]).
Originally posted by Zahid, Butt:
Hi ALL
Does anyone know how to split a String into its constituent characters ??
e.g if you have a string say "saddle", how do I get hold of the character at position 1 or 2 ??
do I use the method charAt() ??? How do you use this method ???

Thanks in Advance.

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic