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
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”:
Utilize the toUpperCase() method as the argument of the “println()” method to convert the string into uppercase and print the output on the console:
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
Example
First, create a string with a name and pass the value to the string:
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:
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
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:
Make another string named as “str2”:
Utilize the “equals()” method to compare the first string with the second:
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
Example
Create two strings with different name:
String str2 = "hint";
Then, use the “concat()” method as the argument of the println() method to combine both strings:
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
Example
In this particular example, we will create a string with a name and set the value of the string:
Declare the integer type variable and utilize the “length()” method:
Pass the variable as the argument of the stated method to get the length of the string:
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
Example
Make a string with a name:
Utilize the “indexOf()” method and pass the specific part of the string as the parameter to find its index:
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:
Here, the “begIndex” refers to the starting index, and “endIndex” indicates the ending index.
Example
Declare a string type variable and set its value:
Invoke the “println()” method and utilize the “substring()” method and pass the starting and ending index values for the substring:
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.