Java StringBuffer delete() Method

delete(int startIndex, int endIndex) delete the substring of the string buffer from startIndex to endIndex-1. Syntax: public synchronized StringBuffer delete(int startIndex, int endIndex)   Note: startIndex should be between 0 and to length of the string or less than endIndex, if it is not StringIndexOutOfBoundsException will be thrown.   Example: class TestStringBuffer{ StringBuffer sb = … Read more

Java StringBuffer replace() Method

replace(int startIndex, int endIndex, String str) The replace the substring of the string buffer from startIndex to endIndex-1 with the specified string. Syntax: public synchronized  StringBuffer replace(int startIndex, int endIndex, String str)   Note: startIndex should be between 0 and to length of the string or less than endIndex, if it is not StringIndexOutOfBoundsException will … Read more

Java StringBuffer insert() Method

insert(int offset, String str) insert a specified string at the offset indicated position.   Syntax: public synchronized StringBuffer insert(int offset, String str)   Note: If the specified string is null then it inserts a “null” string at the offset indicated position. If the insert has an object, int, double, etc instead of a string then … Read more

Java StringBuffer append() Method

append(String str) append the specified string at the end of this string. Syntax: public synchronized StringBuffer append(String str)   Note: if the specified string is null then it appends the “null” string at the end. If the append has an object, int, double, etc as an argument then the argument value is first converted into … Read more

Java StringBuffer

StringBuffer is a class in Java whose object represents the mutable string. It is just like a string class except that its object can be modified. StringBuffer is synchronized, hence it is thread-safe. i.e. StringBuffer class objects are thread-safe, mutable sequences of characters. For these reasons, Java would handle an expression like String newString = … Read more

Java String trim() Method

trim(): Returns a copy of the string, with leading and trailing white space omitted. Syntax: public String trim()   Example class TestString{ String str = ” www.w3schools.blog “; public void trimString(){ //will remove all leading and trailing whitespace. System.out.println(str.trim()); } } public class StringTrimExample { public static void main(String args[]){ //creating TestString object TestString obj … Read more

Java String length() Method

length(): Returns the length of this string. Syntax: public int length()   Example class TestString{ String str = “www.w3schools.blog”; public void stringLengthTest(){ System.out.println(str.length()); } } public class StringLengthExample { public static void main(String args[]){ //creating TestString object TestString obj = new TestString(); //method call obj.stringLengthTest(); } } Output 20  

Java String intern() Method

A string object in the string constant pool is called a String Intern. We can create an exact copy of the heap memory string object in a string constant pool.   intern(): Returns a canonical representation for the string object. Syntax: public String intern()   Note: It returns a string that has the same contents as this … Read more

Java String toLowerCase | Java String toUpperCase

toLowerCase() Converts all of the characters in this String to lowercase. Syntax: public String toLowerCase()   toUpperCase(): Converts all of the characters in this String to upper case. Syntax: public String toUpperCase()   Example: class TestString{ String str = “Roy”; public void upperCase(){ //will convert all characters in upper case. System.out.println(str.toUpperCase()); } public void lowerCase(){ //will convert … Read more

Java String indexOf() | Java String lastIndexOf()

indexOf(String str) Returns the index of the first occurrence of the specified substring within this string. Syntax: public int indexOf(String str)   Note: If no such occurrence of a substring within this string then it returns -1. lastIndexOf(String str) Returns the index of the last occurrence of the specified substring within this string. Syntax: public int lastIndexOf(String str)   Note: If … Read more