Documente Academic
Documente Profesional
Documente Cultură
chars
The char type can store one character value
'A', '\n', '\u00E9
char represented by a 16 bit unsigned integer
* A total of 216 different Unicode chars can be represented
int value;
char letter;
value = 5;
letter = 5;
value = letter; //stores a 53
letter = value; //error . Information can be lost
// when converting 32 bits to 16
int length()
length() method returns the number of chars in a
String object
System.out.println(size + hello.length()) ;
The length of a String is not always obvious
String in = JOptionPane.showInputDialog(Enter);
int size = in.length();
length()-1
char charAt(int)
charAt method returns character from a string
the String object is unchanged by this call
String in = JOptionPane.showInputDialog(Enter);
JOptionPane.showMessageDialog(null,first char is + in.charAt(0) );
JOptionPane.showMessageDialog(null,last char is +
in.charAt(in.length()-1 ) );
String substring(int,int)
This method returns a new String object, which contains a portion of
the characters of the invoking object
Parameters supply start and past the end position
String in, apiece;
in = JOptionPane.showInputDialog(Enter);
//user types hello world
apiece = in.substring(1,8);
// String in is unchanged
// String apiece contains characters ello wo
int indexOf(char)
indexOf method returns the position of the first
occurrence of parameter (-1 if parameter does not occur)
String in = JOptionPane.showInputDialog(Enter);
int blk = in.indexOf( ); //where is first blank?
If user had entered hello world, blk now contains 5
in = in.substring(blk+1, in.length() );
//in is now referencing an object without the first word
String substring(int)
Many String methods are OVERLOADED
This call has an implied 2nd parameter which is the length of the
String
String in = JOptionPane.showInputDialog(Enter);
int blk = in.indexOf( ); //where is first blank?
If user had entered hello world, blk now contains 5
in = in.substring(blk+1 );
//in is now referencing an object without the first word
Convert to string:
String str = Integer.toString(n);
Practice
Convert String word to pig latin (assume word stores 1 word)