String Methods in Java

string methods in java

Strings are among the most commonly used data types in programming, and almost every programming language has a function to manipulate strings.

Strings are also one of the basic data types that you can use while developing Java programs. The String class is used to represent strings in Java and is one of the standard classes provided by the Java API.

You can use this class to create and manipulate strings, as well as perform a wide variety of tasks related to strings. In this article, we will learn about some methods available for use when working with String objects in java.

What is a String in Java? – string methods in java

A String is a primitive data type in Java. It is a sequence of characters that can be created and stored in a variable of type String.

You can create strings in Java in two ways – creating an empty string and concatenating two or more strings. If you want to create an empty string, you can do so by simply not including any characters inside the quotes of the string, for example,

String myString = “” While concatenating strings, you can use the + operator to join two strings together. For example, String myString = “Hello ” + “world”; The + operator can be used to concatenate more than two strings. For example, String myString = “Hello ” + “world ” + “everyone”;

length() method of String – string methods in java

The length() method is used to find out the number of characters in the string. For example, String myString = “Hello world”; int len = myString.length(); System.out.println(len); When you run the program, the output will be 10, as the length of “Hello world” is 10 characters.

The length() method can be used to check if a string variable is null or empty. For example, String myString = “Hello world”; if(myString.length() == 0) System.out.println(“String is empty”); This if condition will be true, as myString is an empty string.

toLowerCase() method of String – string methods in java

The toLowerCase() method is used to convert the string to lowercase. For example, String myString = “Hello world”; myString = myString.toLowerCase(); When you run the program, the output will be hello world. Now, let’s see the same example using if condition.

String myString = “Hello world”; if(myString.toLowerCase() == “hello world”) System.out.println(“String is lowercase”);

toUpperCase() method of String – string methods in java

The toUpperCase() method is used to convert the string to uppercase. For example, String myString = “Hello world”; myString = myString.toUpperCase(); When you run the program, the output will be HELLO WORLD.

trim() method of String – string methods in java

The trim() method is used to remove the leading and trailing whitespaces from the string. For example, String myString = ” Hello world”; myString = myString.trim(); When you run the program, the output will be Hello world.

equals() and equalsIgnoreCase() methods of String

The equals() method is used to check if the two strings are equal. For example, String myString = “Hello world”; myString = myString.equals(“Hello world”); When you run the program, the output will be true.

Now, you can use the equalsIgnoreCase() method to check the equality of two strings even if the case is different. For example, String myString = “Hello world”; myString = myString.equalsIgnoreCase(“hello world”); When you run the program, the output will be true.

append() method of String  – string methods in java

The append() method is used to add a string to the end of an existing string. For example, String myString = “Hello world”; myString = myString.append(” Everyone!”); When you run the program, the output will be Hello world Everyone!

concat() method of String – string methods in java

The concat() method is used to add more than one string to the end of the existing string. For example, String myString = “Hello”; myString = myString.concat(” world”); When you run the program, the output will be Hello world.

This method can be used to add strings as well as numbers and other data types. You can also concatenate more than two strings using this method.

For example, String myString = “Hello”; myString = myString.concat(” world “); myString = myString.concat(” everyone!”); When you run the program, the output will be Hello world, everyone!.

Conclusion

The article discusses some of the most commonly used methods for working with String objects in java. These methods are helpful in creating, manipulating, and converting strings. We have also discussed some examples to illustrate their use.

Add a Comment

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