More String Methods in Java


String capacity() Method

The String capacity() method is used to find the total allocated capacity of string.


Syntax:

public int capacity()  

Example:

 public class Capacity
{   
	public static void main(String args[])
    {  
    	StringBuffer s1=new StringBuffer("onlinetpoint");    
		System.out.println(s1.capacity());
    }
}

String append() Method

The String append() method is used to concatenates str2 at the end of string str1.


Syntax:

public int append()

Example:

 public class Append
{   
	public static void main(String args[])
	{  
		StringBuffer s1=new StringBuffer("onlinetpoint"); 
		s1=s1.append(".com");
		System.out.println(s1);
	}
}

String insert() Method

The String insert() method is used to insert the string into another string.


Syntax:

StringBuffer insert(int index, String str) 

Example:

public class Insert
{   
	public static void main(String args[])
    {  
    	StringBuffer s1=new StringBuffer("onlinetpoint"); 
    	s1=s1.insert(12,".com");
		System.out.println(s1);
    }
} 

String delete() Method

The String delete() method is used to delete the characters within a StringBuffer Object.


Syntax:

StringBuffer delete(int startindex, int endindex)

Example:

 public class Delete
{   
	public static void main(String args[])
    {  
    	StringBuffer s1=new StringBuffer("onlinetutorialpoint.com"); 
    	s1=s1.delete(7,14);
		System.out.println(s1);
    }
}

String reverse() Method

The String reverse() method is used to reverse the characters within a StringBuffer Object.


Syntax:

StringBuffer reverse()

Example:

public class Reverse
{   
	public static void main(String args[])
    {  
    	StringBuffer s1=new StringBuffer("tnioptenilno"); 
    	s1.reverse();
		System.out.println(s1);
    }
}

String replace() Method

The String replace() method is used to replaces one set of characters into another set of characters within a StringBuffer Object.


Syntax:

 StringBuffer replace(int startIndex, int endIndex, String str)

Example:

public class Replace
{   
	public static void main(String args[])
    {  
    	StringBuffer s1=new StringBuffer("onlinetreplace"); 
    	s1.replace(7,14,"point.com");
		System.out.println(s1);
    }
}

String startsWith() & endsWith() Method

The String startsWith() method is used to find whether a given string starts with specified string. The String endsWith() method is used to find whether a given string ends with specified string.


boolean startsWith(String str)
boolean endsWith(String str) 

Example:

public class Startendwidth
{   
	public static void main(String args[])
    {  
    	String s1="onlinetpoint"; 
		System.out.println(s1.startsWith("point"));
        System.out.println(s1.endsWith("point"));
    }
} 

String join() & trim() Method

The String join() method is used to returns a string joined with given delimiter. The String trim() method is used to remove leading and trailing whitespace.


Syntax:

public String join(CharSequence delimiter, CharSequence... elements) 
String trim() 

Example:

public class Jointrim
{   
	public static void main(String args[])
    {  
    	String s1=" onlinetpoint "; 
		System.out.println(s1.trim());// remove the whitespace
		String s2=String.join(" ","welcome","to","onlinetpoint"); 
		System.out.println(s2);
    }
} 

String toLowerCase() & trim() Method

The String toLowerCase() method is used to convert the string into the lowercase letter. The String toUpperCase() method is used to convert the string into the uppercase letter.


Syntax:

 public String toLowerCase()   
public String toUpperCase()

Example:

public class Toloweruppercase
{   
	public static void main(String args[])
    {  
    	String s1="onlineTpoint";
		System.out.println(s1.toLowerCase());
		System.out.println(s1.toUpperCase());
    }
} 

String substring() & isEmpty() Method

The String substring() method is used to It returns a portion of the String. The String isEmpty() method is used to checks if the given string is empty or not.


Syntax:

 public boolean isEmpty()   
public String substring(int startIndex, int endIndex)

Example:

public class Substringisempty
{   
	public static void main(String args[])
    {  
    	String s1="onlinetpoint";
		System.out.println(s1.isEmpty());
		System.out.println(s1.substring(7,11));
    }
} 



Onlinetpoint is optimized for basic learning, practice and more. Examples are well checked and working examples available on this website but we can't give assurity for 100% correctness of all the content. This site under copyright content belongs to Onlinetpoint. You agree to have read and accepted our terms of use, cookie and privacy policy.