String handling in java:-
A string is a data type used to represent text. It is a set of characters enclosed within double quotes. In java String is a class. There are many functions in this class to provide features that helps in string handling.
Here is the list of all functions with their return type, arguments and the functionalities.
<tbody>
Function name
Arguments
Return type
Functionality
Example
trim()
-
String
It removes the leading and trailing spaces , if any, from the string.
It doesn’t remove the spaces present
in between the string.
String str=” Program ”;
String res=str.trim();
System.out.println(res);
o/p
Program
toLowerCase()
-
String
It converts each character in a string to lowercase.
String str=”ComPuteR”;
String res=str.toLowerCase();
System.out.println(res);
o/p
computer
toUpperCase()
-
String
It converts each character in a string to Uppercase.
String str=”ComPuteR”;
String res=str.toUpperCase();
System.out.println(res);
o/p
COMPUTER
length()
-
int
It returns the length of the string (The no. of characters in the
string)
String str=”Computer”;
System.out.println(str.length());
o/p
8
charAt()
int
char
It accepts an integer(index) and returns the character at that particular index .
The index of the string rages from 0 to length()-1
String str=”Computer”;
System.out.println(str.charAt(0));
o/p
C
indexOf()
char
int
It takes a character and returns the index of the first occurrence of
the specified character in a string. If the character is not present in the
string it will return -1
String str=”Computer”;
System.out.println
(str.indexOf(‘C’));
o/p
0
indexOf()
char
int
It takes a character and returns the index of the last occurrence of
the specified character in a string. If the character is not present in the
string it will return -1
String str=”Pineapple”;
System.out.println
(str.lastindexOf(‘p’));
o/p
6
concat()
String
String
It joins or concatenates two strings.
String str1=”Computer ”;
String str2=”Program ”;
String str3=str1.concat(str2);
System.out.println(str3);
o/p
Computer Program
equals()
String
boolean
It checks whether two strings are equal or not. It returns a boolean,
if the strings are same true otherwise false.
String str1=”Computer ”;
String str2=”Program ”;
System.out.println(str1.equals(str2));
o/p
false
equalsIgnoreCase()
String
boolean
It checks whether two strings are equal or not after ignoring their
case. It returns a boolean, if the strings are same true otherwise false.
String str1=”Computer ”;
String str2=”computer ”;
System.out.println(str1.equalsIgnoreCase(str2));
o/p
true
CompareTo()
String
int
This method compares two strings. It not only checks the equality of
the strings but also checks whether a string is bigger or smaller than the
other string. It returns an integer,
if str1==str2 it returns zero.
if str1>str2 it returns positive integer
if str1==str2 it returns negative integer.
String str1=”CAT ”;
String str2=”FOX ”;
System.out.println(str1.comparerTo(str2));
o/p
-3
* When we compare “CAT” and “FOX” , the first
occurrence where these two string differ is ‘C’ and ‘F’. The result is
-3(67-70), which is the difference of their equivalent ACCII value
CompareToIgnore()
String
int
This method compares two strings ignoring their case . It not only checks
the equality of the strings but also checks whether a string is bigger or smaller
than the other string. It returns an integer,
if str1==str2 it returns zero.
if str1>str2 it returns positive integer
if str1==str2 it returns negative integer.
String str1=”CAT ”;
String str2=”cat ”;
System.out.println(str1.comparerToIgnore(str2));
o/p
0
replace()
Two char
String
It replaces all occurrences of a character in a string with another
character.
String str1=”jack and jill ”;
System.out.println(str1.replace(‘j’,’z’));
o/p
zack and zill
subString()
int
String
It returns a new string that is a substring of the given string
String str1=”jack and jill ”;
System.out.println(str1.subString(9));
o/p
zill
*It extracts the string form index 9 to the end of the string ____________________
String str1=”jack and jill ”;
System.out.println(str1.subString(5,8));
o/p
and
*It extracts the string form index 5 to 7
startsWith()
String
Boolean
It checks whether a string starts with a particular string.
String str1=”jack and jill ”;
String str2=”jack”;
System.out.println(str1.startsWith(str2));
o/p
true
endsWith()
String
Boolean
It checks whether a string ends with a particular string.
String str1=”jack and jill ”;
String str2=”jack”;
System.out.println(str1.endssWith(str2));
o/p
false
valueOf()
Any primitive datatype
String
It returns the string representation of the argument.
int num1=123;
System.out.println(String.valueOf(num1));
o/p
123 *In string datatype
</tbody>