String in Java- Constructor methods and examples

Java String is defined in java.lang.String.

Java String is a sequence of characters quoted in double quote (” “).

Java String Constructors

Sr NoConstructor and description
1String()
Create a new empty string object
2String(byte[] bytes)
Construct a string object with byte array
3String(char[] value)
Construct a string object with character array
4String(String original)
Create a string with another string
5String(StringBuffer buffer)
Create a string with StringBuffer object
6String(StringBuilder builder)
Create a string with StringBuilder object

Java String Methods

There are many methods avaible in string class few common and widely used methods are

Java String Method charAt()

This method takes one integer argument and return a character specified at index position.

For any string the index value starts from 0 to string length -1 position.

If passed index value not in valid range then it throws IndexOutOfBoundsException.

For example string Hello Friends contains 13 characters starting from 0 to 12 H is first position with index value 0, F is in seventh position with index value 6 and s is in 13 position with index value 12.


Using number other then 0 to 12( 0 to length()-1) will result in IndexOutOfBountException

Signature:

public char charAt(int index)

Parameters:

index -Integer index value to get character at specified index position.

Return:

Character at passed index value.

Exception Throws:

IndexOutOfBoundsException If index value in not between 0 to String length()-1

Example

Result

Now Print all characters with its index value

Example

Result

Java String Method compareToIgnoreCase()

This method is used to compare two string with case ignorance and compare both string lexicographically. If both strings are same then it returns 0. If first string is greater then another then it returns positive integer, if second string is greater then first then it returns negative integer value.

Signature

public int compareToIgnoreCase(String str)

Parameters

str – String value that will compared with string.

Return

Integer value. if string is greater then passed str value then it returns positive integer if both strings are equal then it returns zero if passed str is greater then string then this method returns negative integer value.

Example1

Result

In above program comparing same string is returning 0. In second println() statement A is lexicographically smaller then b so it is returning -1. In third println() statement C is lexicographically greater then b so it is returning 1 and so on.

Example2

Result

Java String Method indexOf with int parameter

indexOf(int ch) method is used to get the first occurrence of a character in given string. This method takes a character input (in Unicode code units) and return the index value of character in string, if character not found in string then it returns -1.

Signature

public int indexOf(int ch)

Parameters

A charcter value in unicode code point.

Return

Returns the int value that represents the first occurance of the character in string, if character not fount in string then it returns -1.

Example

Java String Method indexOf()

indexOf method is used to get the first occurrence of a sub string in given string. This method takes a string input and return the index value of it in given string object, if sub string not found in string then it returns -1.

Signature

public int indexOf(String str)

Parameters

str is sub string value to find in specified string.

Return

Returns the int value that represents the first occurrence of the sub string in string, if sub string not fount in string then it returns -1.

Example

Result

Java String Method lastIndexOf()

This method take a string parameter as input and return the last index value of parameter string in specified string. If string not found then it returns -1.
The second overloaded method find the string from fromindex position.

Signature

public int lastIndexOf(String str)
public int lastIndexOf(String str, int fromIndex)

Parameters

str- to search in String
fromindex- to search str from specified index

Return

Return the last index value of parameter str in specified string. If str not found then it returns -1

Example

Result

Java String substring() method

This method takes a int argument to generate sub string of string. It returns sub string starting from beginIndex value to last of string.
The second overloaded method allows to specify the ending index value also.
beginIndex parameters is inclusive and endIndex is exclusive.

Signature

public String substring(int beginIndex)
public String substring(int beginIndex,int endIndex)

Parameters

beginIndex –generate substring from this index value(inclusive).
endIndex-generate substring up to endIndex-1 value(exclusive).

Return

A substring

Throw

IndexOutOfBoundsException: If beginIndex is negative or endIndex is greater then length of string or if beginIndex is greater then endIndex.

Example

Result

Java String concat() method

String concat() is used to concatenate two strings. The parameter str string is appended to specified string and new string object is returned by this method.
Example
“Hello ”.concat(“Friends”)
This will generate a new string object “Hello Friends”

Parameters

Str – is string parameter used to concatenate with specified string

Example

Here str is concatenated with ” ” then it is concatenated with str1 and result is assigned to str3 String object.

Read More