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

Java String startsWith() | Java String endsWith()

startsWith(String prefix): Test if this string starts with the specified prefix. Syntax: public boolean startsWith(String prefix)   Note: It returns true if the character sequence represented by the argument is a prefix of the character sequence represented by this string otherwise returns false. It also returns true if the argument string is empty or is equal to this String … Read more

Java String charAt() Method

charAt(int index): Returns the char value at the specified index. Syntax: public char charAt(int index)   Note: An index ranges from 0 to length() – 1. If the index is negative or greater than length() – 1, it will throw IndexOutOfBoundsException.    Java String charAt() Example class TestString{ String str = “www.w3schools.blog”; public void charAtTest(){ //Returns the character value … Read more

Java string class methods

S.No. Method Description  1. public boolean equals(Object anObject) Compares this string to the specified object.  2. public boolean equalsIgnoreCase(String anotherString) Compares this String to another String, ignoring case.  3. public String concat(String str) Concatenates the argument string to the end of this string.  4. public int compareTo(String str) Compares two strings lexicographically.  5. public int … Read more

Java toString() method

toString() method of the Object class is used to provide a string representation of an object. When an object is passed in the print() method as an argument then the compiler internally calls the toString() method on the object. It returns object representation as classname@hexadecimal representation of the hash code of the object. class Student{ … Read more