Strings
Strings
Strings
IN
JAVA
STRINGS
Stringsrepresent a sequence of characters.
The easiest way to represent a sequence of characters in
JAVA is by using a character array.
char Array[ ] = new char [5]; Character arrays are
not good enough to
support the range of
operations we want to
perform on strings.
O
R
String name= new String("Craig");
STRING Arrays
• An array of strings can also be created
String cities [ ] = new String[5];
• Will create an array of CITIES of size 5
to hold string constants
String Methods
The String class contains many useful methods for string-
processing applications.
◦ A String method is called by writing a String object, a dot, the name of
the method, and a pair of parentheses to enclose any arguments
1-10
Some Methods in the Class String (Part
2 of 8)
Some Methods in the Class String (Part
of
3 8)
Some Methods in the Class String (Part
8)
4
Some Methods in the Class String (Part
8)
5
String
of 8)
Some Methods in the Class String (Part 7
o
STRING BUFFER
CLASS
STRINGBUFFER class creates strings flexible length that
can be modified in terms of both length and content.
STRINGBUFFER may have characters and substrings
inserted in the middle or appended to the end.
STRINGBUFFER automatically grows to make room for
such additions
number of characters.
void setLength(int len):- Sets the length of the string s1
to len.
If len<s1.length(), s1 is truncated.
If len>s1.length(), zeros are added to s1.
charAt(int where):- Extracts value of a single character.
setCharAt(int where, char ch):- Sets the value of character
at specified position.
STRING BUFFER FUNCTIONS
append(s2):- Appends string s2 to s1 at the end.
insert(n,s2 ):- Inserts the string s2 at the position n of the
string s1
reverse():- Returns the reversed object on when it is called.
delete(int n1,int n2):- Deletes a sequence of characters
from the invoking object.