Java Design Pattern
Introduction to Java 10
Introduction to Java 11
Introduction to Java 12

Java String Class Method

The following methods are most commonly used methods of String class.

charAt() :

charAt() returns the character located at the specified index.

String str="javaRace";

System.out.println(str.charAt(4));

Output : R

Note: Index of a String starts from 0, hence str.charAt(4) means fifth character of the String str.


length() :

length() returns the length of a string. The length is the number of characters present in a String.

String str="javaRace";

System.out.println(str.length());

Output : 8


equalsIgnoreCase() :

equalsIgnoreCase() determines the equality of two Strings, ignoring thier case (upper or lower case doesn't matter ). If two string content is equal then case(I mean upper case and lower case) part is ignoring.

String str="java";

System.out.println(str.equalsIgnoreCase("JAVA"));

Output : true


indexOf() :

indexOf() returns the index of first occurrence of a substring or a character. indexOf() method has four forms:

  • int indexOf(String str) It returns the index within this string of the first occurrence of the specified substring.
  • int indexOf(int ch, int fromIndex) It returns the index within this string of the first occurrence of the specified character, starting the search at the specified index.
  • int indexOf(int ch) It returns the index within this string of the first occurrence of the specified character.
  • int indexOf(String str, int fromIndex) It returns the index within this string of the first occurrence of the specified substring, starting at the specified index.

Example:

public class IndexOfDemo{
	public static void main(String[]aregs){

String str="Silan Software";
System.out.println(str.indexOf('u'));   //3rd form
System.out.println(str.indexOf('t',3));   //2nd form

String subString="Silan";
System.out.println(str.indexOf(subString));   //1st form

System.out.println(str.indexOf(subString,7));   //4th form

Output: 2

Note: here  -1 represents that the substring/Character is not found in the given String.


replace() :

replace() method replaces occurrences of character with a specified new character.

String str=="JAXA";

System.out.println(str.replace('X','V'));

Output : JAVA


substring() :

substring() method returns a part of the string. substring() method has two forms,

public String substring(int begin);

public String substring(int begin, int end);

The first argument represents the starting point of the subtring. If the substring() method is called with only one argument, the subtring returned, will contain characters from specified starting point to the end of original string.
But, if the call to substring() method has two arguments, the second argument specify the end point of substring.

String str="javaRace";

System.out.println(str.substring(4));

Output : Race

System.out.println(str.substring(4,7));

Output : Rac

toLowerCase() :

toLowerCase() method returns string with all uppercase characters converted to lowercase.

String str="JAVARACE";

System.out.println(str.toLowerCase());

Output : javarace


toUpperCase() :

This method returns string with all lowercase character changed to uppercase.

String str="javarace";

System.out.println(str.toUpperCase());

Output : JAVARACE


valueOf() :

Overloaded version of valueOf() method is present in String class for all primitive data types and for type Object.

NOTE : valueOf() is used to convert primitive data types into Strings.

public class ValueOfDemo{
public static void main(String[]args){
	int n=65;
	String s1=String.valueOf(num);     //converting int to string

	System.out.println(s1+""+"Silan Software");
	}
]

Output: 65 Silan Software
But for objects, valueOf() method calls toString().

toString() :

toString() method returns the string representation of the object used to invoke this method. toString()is used to represent any Java Object into a meaningful string representation. It is declared in the Object class, hence can be override by any java class.

public class Test {
public static void main(String[] args)
	{
		Test t=new Test();
		System.out.println(t);
	}
	public String toString()
	{
		return"This is  Trilochan 4m Silan Software";
	}
}

Output : This is Trilochan 4m Silan Software

 

toString() can also be used with normal string objects.

String str="silantutorial.com";

System.out.println(str.toString());

Output : silantutorial.com

Note: If we don't override the toString() method and directly print the object, then it will display the object id.

Example:

public class Example {

public static void main(String[]args)

Output: java8s.Car@15db9742 (here java8s is a user-defined package).

toString() with Concatenation :

Whenever we concatenate any other primitive data type, or object of other classes with a String object,toString() or valueOf() is called automatically to change the other object or primitive type into string, for successful concatenation.

int age=50;

String str="He is"+age+"years old.";

trim() :

This method returns a string from which any leading and trailing whitespaces has been removed.

String str="silantutorial";

System.out.println(str.trim());

Output : silantutorial

Note: If the whitespaces are between the string ,for example: String s1 = "Silan Software"; then System.out.println(s1.trim()); will output "Silan Software".
trim() method removes only the leading and trailing whitespaces.

About the Author



Silan Software is one of the India's leading provider of offline & online training for Java, Python, AI (Machine Learning, Deep Learning), Data Science, Software Development & many more emerging Technologies.


We provide Academic Training || Industrial Training || Corporate Training || Internship || Java || Python || AI using Python || Data Science etc






 PreviousNext