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

Create Immutable class in java

Immutable Object: An object is known as immutable if its state can not be changed over time or we can say after object creation. Need for Immutable Classes In current days, most of the applications are running into multi-threading environments which results in concurrent modification problems. Popular Immutable classes in java All wrapper classes (java.lang.Integer, … Read more

Java Substring

A substring is a string that is part of a longer string. String class provides the following methods to get a substring from a string. 1. public String substring(int startIndex): Returns a new string that starts from a specified string and extends to the end of this string.  It will throw IndexOutOfBoundsException – if startIndex … Read more

Java String Concatenation

The general meaning of concatenation is a series of interconnected things. String concatenation refers to the combination of more than one string. Ways of String concatenation: By concatenation operator +. By concat() method. 1. By concatenation operator + String concatenation can be performed with the + operator. String concatenation is performed by the StringBuilder or … Read more