Java

String Handling Functions Using Java

In programming, there are multiple operations that can be performed on a string, including converting a string into uppercase, lowercase, finding the index of a specific element, concatenating the two strings into one string, finding substring and performing a comparison of multiple strings. These are all known as string-handling functions. Java String consists of an immutable sequence/order of characters in Unicode. It differs from a string in C or C++, where (in C or C++) a string is simply an array of chars.

This tutorial has stated the various string-handling functions using Java.

What are String Handling Functions in Java?

String handling techniques are utilized for checking and manipulating the data of string data. There are multiple string-handling functions that exist in Java. Some of them are listed below:

How to Use the “toUpperCase()” Method in Java?

The “toUpperCase()” method converts the string into uppercase.

Syntax

str.toUpperCase()

 
Here, the “toUpperCase()” method converts the “str” string value to uppercase.

Example

To convert the string intro the uppercase, first of all, create a string named “txt” that stores the value “linuxhint”:

String txt = "linuxhint";

 
Utilize the toUpperCase() method as the argument of the “println()” method to convert the string into uppercase and print the output on the console:

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

 
It can be observed that the string has been converted into uppercase successfully:

How to Use the “toLowerCase()” Method in Java?

The “toLowerCase()” function in Java is utilized for converting a string to lowercase letters. To do so, check out the stated example.

Syntax

str.toLowerCase()

 
Example

First, create a string with a name and pass the value to the string:

String txt = "MY NAME IS HAFSA";

 
Utilize the “println()” method and pass the string with “toLowerCase()” that will convert the string into small letters and print the output on the console:

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

 
Output

How to Use the “equals()” Method in Java?

The “equals()” method in Java is used for comparing two or multiple strings. It returns the resultant value as true if both strings are equal to each other or the same. Otherwise, it will return false.

Syntax

str1.equals(str2)

 
Here, the “equals()” method compares “str2” with “str1”.

Example

Create the first string named “str1” and utilize the “String()” method to pass the string value according to your preference:

String str1 = new String("Linuxhint");

 
Make another string named as “str2”:

String str2 = new String("linuxhint");

 
Utilize the “equals()” method to compare the first string with the second:

System.out.println("Result : "+(str1.equals(str2)));

 
As both strings are different (in terms of case), that is why the “equals()” method returns the false value:

How to Use the “concat()” Method in Java?

The “concat()” function in Java is utilized to combine the two strings. It appends the second string at the end of the first string without any space.

Syntax

str1.concat(str2)

 
Example

Create two strings with different name:

String str1 = "Linux";
String str2 = "hint";

 
Then, use the “concat()” method as the argument of the println() method to combine both strings:

System.out.println(str1.concat(str2));

 

How to Use the “length()” Method in Java?

You can utilize the built-in “length()” method of the String class for the purpose of calculating the length of a string in Java. Strings are objects created with the help of the string class, and the length() method is a public member method of the particular class. So, any variable of type string can access this method using the “.” (dot) operator.

Syntax

length()

 
Example

In this particular example, we will create a string with a name and set the value of the string:

String str = "This is Linuxhint tutorial website";

 
Declare the integer type variable and utilize the “length()” method:

int size= str.length();

 
Pass the variable as the argument of the stated method to get the length of the string:

System.out.println(size);

 
It can be noticed that the length of the defined string is printed on the console:

How to Use the “indexOf()” Method in Java?

The “indexOf()” method is utilized to return the position of the first occurrence of a specified character in a string.

Syntax

str.indexOf("value")

 
Example

Make a string with a name:

String str = "Linuxhint is one of the best tutorial websites";

 
Utilize the “indexOf()” method and pass the specific part of the string as the parameter to find its index:

System.out.println(str.indexOf("best"));

 

How to Use the “substring()” Method in Java?

The “substring()” method is utilized in Java to get the substring in Java. You can also use it to fetch the substring based on the specified single or multiple indexes.

Syntax

To use the “substring()” method, follow the below-given syntax:

String substring(int begIndex, int endIndex)

 
Here, the “begIndex” refers to the starting index, and “endIndex” indicates the ending index.

Example

Declare a string type variable and set its value:

String str = "Linuxhint is one of the best tutorial websites";

 
Invoke the “println()” method and utilize the “substring()” method and pass the starting and ending index values for the substring:

System.out.println(str.substring(12,28));

 
Output


That’s all about the string handling functions using Java.

Conclusion

String handling functions are utilized for checking and manipulating the string values. There are multiple string handling functions exist in Java, including “toUpperCase()”, “toLowerCase()”, “equals()”, “concat()”, “length()”, “indexof()”, and “substring()”. This tutorial has stated the string handling functions using Java.

About the author

Hafsa Javed