String methods

The String class in Java provides several methods that allow you to manipulate and interact with strings. These methods are designed to help you perform common string operations, such as concatenation, search, and replacement. Here are some of the most commonly used String methods in Java:

Concat: This method is used to concatenate two strings, like this:

String s1 = "Hello";
String s2 = " World!";
String s3 = s1.concat(s2);

length: This method is used to find the length of a string, like this:

String s = "Hello World!";
int length = s.length();

Substring: This method is used to extract a portion of a string, like this:

String s = "Hello World!";
String sub = s.substring(0, 5);

charAt: This method is used to access individual characters in a string, like this:

String s = "Hello World!";
char c = s.charAt(0);

indexOf: This method is used to find the index of the first occurrence of a specified character or substring within a string, like this:

String s = "Hello World!";
int index = s.indexOf("l");

Replace: This method is used to replace all occurrences of a specified character or substring within a string, like this:

String s = "Hello World!";
String replaced = s.replace("l", "x");

toUpperCase and toLowerCase: These methods are used to convert a string to upper or lower case, respectively, like this:

String s = "Hello World!";
String upper = s.toUpperCase();
String lower = s.toLowerCase();

Trim: This method is used to remove leading and trailing white spaces from a string, like this:

String s = "   Hello World!   ";
String trimmed = s.trim();

In conclusion, the String class in Java provides a wealth of methods that allow you to manipulate and interact with strings. Whether you need to concatenate, search, replace, or simply access individual characters, the String class has you covered. With these methods at your fingertips, you’ll be able to perform a wide variety of string operations with ease!

Shubhajna Rai
Shubhajna Rai

A Civil Engineering Graduate interested to share valuable information with the aspirants.

Leave a Reply

Your email address will not be published. Required fields are marked *

Get the latest updates on your inbox

Be the first to receive the latest updates from Codesdoc by signing up to our email subscription.

    StudentProjects.in