String Class and Its Methods

Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

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.

There are two ways to create a String in Java

String literal : String str1 = "Welcome";

Using new keyword : String str1 = new String("Welcome");

Methods :

1) public int length(): Returns the length of this string.

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.

4) int compareTo(Object o): Compares this String to another Object.

5) int compareTo(String anotherString): Compares two strings lexicographically.

6) int compareToIgnoreCase(String str):Compares two strings lexicographically, ignoring case


differences.

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.

Example implementing some of the methods of String Class :

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 :

Here is how we can create vectors in Java.


Vector<Type> vector = new Vector<>();

Here, Type indicates the type of a linked list. For example,

// create Integer type linked list

Vector<Integer> vector= new Vector<>();

// create String type linked list

Vector<String> vector= new 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.

int capacity():Returns the current capacity of this vector.

void clear():Removes all of the elements from this vector.

boolean contains(Object elem):Tests if the specified object is a component in this vector.

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.

boolean isEmpty(): Tests if this vector has no components.

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.

Example implementing some of the methods of vector class :

You might also like