Now Reading: What is String class method?

Loading

What is String class method?

svgMarch 17, 2023Javascriptleetcode

What is String Class Method?
String class methods are the built-in methods of the java.lang.String class that are used to manipulate strings in Java. These methods are useful for performing operations on strings such as trimming, concatenating, converting, comparing, and replacing strings. In this article, we will discuss the various String class methods and how they can be used to manipulate strings in Java.

Understanding the String Class
The String class is a class in the Java language that is used to represent a sequence of characters. A String object is immutable, meaning that once it is created, it cannot be changed. Strings are commonly used to store text data, such as words, sentences, and paragraphs. Strings are also used to store numerical data, such as integers and floating-point numbers.

String Class Methods
The String class provides a variety of methods that can be used to manipulate strings. These methods can be used to perform operations such as trimming, concatenating, converting, comparing, and replacing strings. Let’s take a look at some of the most commonly used String class methods.

Concatenating Strings
The concat() method is used to concatenate two strings. This method takes two strings as parameters and returns a new string that is the result of the concatenation of the two strings. For example, the following code concatenates the strings “Hello” and “World” and prints the result:

String str1 = “Hello”;
String str2 = “World”;
String str3 = str1.concat(str2);
System.out.println(str3); // prints “HelloWorld”

Comparing Strings
The compareTo() method is used to compare two strings. This method takes two strings as parameters and returns an integer value that indicates whether the two strings are equal, or if one string is greater than or less than the other. For example, the following code compares the strings “Hello” and “World” and prints the result:

String str1 = “Hello”;
String str2 = “World”;
int result = str1.compareTo(str2);
System.out.println(result); // prints -1

Replacing Strings
The replace() method is used to replace all occurrences of a specified character or substring in a string with another character or substring. This method takes three parameters: the character or substring to be replaced, the character or substring to replace it with, and the string in which the replacement should take place. For example, the following code replaces all occurrences of the character ‘a’ with the character ‘b’ in the string “Hello World”:

String str = “Hello World”;
String newStr = str.replace(‘a’, ‘b’);
System.out.println(newStr); // prints “Hello World”

Trimming Strings
The trim() method is used to remove leading and trailing whitespace from a string. This method takes no parameters and returns a new string with the whitespace removed. For example, the following code trims the string ” Hello World ” and prints the result:

String str = ” Hello World “;
String newStr = str.trim();
System.out.println(newStr); // prints “Hello World”

Converting Strings
The toUpperCase() and toLowerCase() methods are used to convert a string to upper or lower case, respectively. These methods take no parameters and return a new string with the characters converted to upper or lower case. For example, the following code converts the string “Hello World” to upper case and prints the result:

String str = “Hello World”;
String newStr = str.toUpperCase();
System.out.println(newStr); // prints “HELLO WORLD”

Conclusion
The String class in Java provides a variety of methods that can be used to manipulate strings. These methods can be used to perform operations such as trimming, concatenating, converting, comparing, and replacing strings. Understanding and using these methods can help developers write more efficient and effective code.

svg

What do you think?

Show comments / Leave a comment

Leave a reply

Loading
svg
Quick Navigation
  • 01

    What is String class method?