String Class and Its Methods
String Class and Its Methods
String Class and Its Methods
Strings, which are widely used in Java programming, are a sequence of characters. In Java
programming language, strings are treated as objects. The Java platform provides the String class to
create and manipulate strings. The String class represents character strings. All string literals in Java
programs, such as "abc", are implemented as instances of this class. Strings are constant; their values
cannot be changed after they are created.
Methods :
2) public boolean isEmpty(): Returns true if, and only if, length() is 0.
3) public char charAt(int index): Returns the char value at the specified index. An index ranges from 0
to length() - 1. The first char value of the sequence is at index 0, the next at index 1, and so on, as for
array indexing.
7) String concat(String str):Concatenates the specified string to the end of this string.
8) boolean endsWith(String suffix): Tests if this string ends with the specified suffix
9) int indexOf(int ch): Returns the index within this string of the first occurrence of the specified
character.
10) int indexOf(int ch, int fromIndex): Returns the index within this string of the first occurrence of the
specified character, starting the search at the specified index.
11) int indexOf(String str): Returns the index within this string of the first occurrence of the specified
substring.
12) int indexOf(String str, int fromIndex): Returns the index within this string of the first occurrence of
the specified substring, starting at the specified index.
13) int lastIndexOf(int ch): Returns the index within this string of the last occurrence of the specified
character.
14) int lastIndexOf(int ch, int fromIndex): Returns the index within this string of the last occurrence of
the specified character, searching backward starting at the specified index.
15) int lastIndexOf(String str):Returns the index within this string of the rightmost occurrence of the
specified substring.
16) int lastIndexOf(String str, int fromIndex):Returns the index within this string of the last occurrence
of the specified substring, searching backward starting at the specified index.
17) String replace(char oldChar, char newChar): Returns a new string resulting from replacing all
occurrences of oldChar in this string with newChar.
18) String toLowerCase(): Converts all of the characters in this String to lower case using the rules of
the default locale.
19) String toUpperCase():Converts all of the characters in this String to upper case using the rules of
the default locale.
20) String trim():Returns a copy of the string, with leading and trailing whitespace omitted.
Vectors:
Java Vector class comes under the java.util package. The vector class implements a growable array of
objects. Like an array, it contains the component that can be accessed using an integer index.
Vector is very useful if we don't know the size of an array in advance or we need one that can change
the size over the lifetime of a program.
Vector implements a dynamic array that means it can grow or shrink as required.
Creating a Vector :
Methods :
void add(int index, Object element): Inserts the specified element at the specified position in this
Vector.
void addElement(Object obj):Adds the specified component to the end of this vector, increasing its
size by one.
Object firstElement():Returns the first component (the item at index 0) of this vector.
Object get(int index): Returns the element at the specified position in this vector.
int indexOf(Object elem): Searches for the first occurence of the given argument, testing for equality
using the equals method.
int indexOf(Object elem, int index): Searches for the first occurence of the given argument, beginning
the search at index, and testing for equality using the equals method.