substring( )

The java string substring() method returns a part of the string.

We pass begin index and end index number position in the java substring method where start index is inclusive and end index is exclusive. In other words, start index starts from 0 whereas end index starts from 1.

There are two types of substring methods in java string.
              substring(int startIndex)
                        and
              substring(int startIndex, int endIndex)

Examples

public class SubstringExample{
public static void main(String args[]){
String s1="javatpoint";
System.out.println(s1.substring(2,4));//returns va
System.out.println(s1.substring(2));//returns vatpoint
}

}

Output

va
vatpoint 

public class SubstringExample2 {
public static void main(String[] args) {
String s1="Javatpoint";
String substr = s1.substring(0); // Starts with 0 and goes to end
System.out.println(substr);
String substr2 = s1.substring(5,10); // Starts from 5 and goes to 10
System.out.println(substr2);
String substr3 = s1.substring(5,15); // Returns Exception
}

Output

Javatpoint
point
Exception in thread "main"
java.lang.StringIndexOutOfBoundsException: begin 5, end 15, length
10 

concat( )

You can concatenate two strings using concat( ), shown here:

                       String concat(String str)

This method creates a new object that contains the invoking string with the contents of str appended to the end. concat( ) performs the same function as +. For example,

                          String s1 = "one";
                          String s2 = s1.concat("two");

puts the string “onetwo” into s2. It generates the same result as the following sequence:

                           String s1 = "one";
                           String s2 = s1 + "two"; 

replace( )

The java string replace() method returns a string replacing all the old char or CharSequence to new char or CharSequence.

There are two type of replace methods in java string.

  1. replace(char oldChar, char newChar)
  2. replace(CharSequence target, CharSequence replacement)

Java String replace(char old, char new) method example

public class ReplaceExample1{
public static void main(String args[]){
String s1="javatpoint is a very good website";
String replaceString=s1.replace('a','e');//replaces all occurrences of 'a' to 'e'
System.out.println(replaceString);
}

}

Output

jevetpoint is e very good website 

Java String replace(CharSequence target, CharSequence replacement) method example

public class ReplaceExample2{
public static void main(String args[]){
String s1="my name is khan my name is java";
String replaceString=s1.replace("is","was");//replaces all occurrences of "is" to "
was"
System.out.println(replaceString);
}

}

Output

my name was khan my name was java

trim()

The java string trim() method eliminates leading and trailing spaces. The unicode value of space character is '\u0020'. The trim() method in java string checks this unicode value before and after the string, if it exists then removes the spaces and returns the omitted string.

public class StringTrimExample {
public static void main(String[] args) {
String s1 =" hello java string ";
System.out.println(s1.length());
System.out.println(s1); //Without trim()
String tr = s1.trim();
System.out.println(tr.length());
System.out.println(tr); //With trim()
}
}

Output

22
hello java string
17
hello java string