Class 10 ICSE - Java
Strings Methods
In java there are lot of string methods that allow us to perform operation on a string or we can say
sequence of char values.For example indexOf(int),it returns the index of first occurance of
specified character in the string.
So we will discuss strings methods which are performed on strings.
Length()
As the name suggest from length,means it is used to calculate the length of string,return type of
this
function is integer
We will understand this in program.
![](class10subchap/stringsmethodimg/img1.jpg)
![](class10subchap/stringsmethodimg/img2.jpg)
IndexOf(String)
As the name suggest index,means it will return index value of first character of the substring from
the
string, which is given to function as a parameter.If the character is not present in the string it will
return -1
Example:
charAt(int)
In this function we pass index value as a parameter and it will return the character at the given
index.Here the index value start from 0 and it will goes to n-1,where n is the length of string.The
starting index will be from 0.
Example:
![](class10subchap/stringsmethodimg/img5.jpg)
![](class10subchap/stringsmethodimg/img6.jpg)
replace(String,String)
This function takes two strings or characters as a parameters,and it will replace the old string with a new
string.
Here the
first parameter tells the old string or character which should be replaced, and second
parameter tells
by which string or character it should be replaced.
![](class10subchap/stringsmethodimg/img7.jpg)
![](class10subchap/stringsmethodimg/img8.jpg)
toLowerCase()
This function converts all the characters in a string in a
lowercase.
Example:
![](class10subchap/stringsmethodimg/img9.jpg)
![](class10subchap/stringsmethodimg/img10.jpg)
toUpperCase()
This function converts all the characters in a string in a
lowercase,and then returns the converted string.
Example:
![](class10subchap/stringsmethodimg/img11.jpg)
![](class10subchap/stringsmethodimg/img12.jpg)
compareTo()
This function is basically used to compare two strings.It works on the the basis of unicode of each
chaeracter in a string
This function returns on the basis of following conditions:-
-
It returns 0,if two strings are equal.
-
It returns positive(+),if firstmismatch character of first string is greater
than firstmismatch character of second String
-
It returns negative(-),if firstmismatch character of first string is lesser
than firstmismatch character of second String
Example:
![](class10subchap/stringsmethodimg/img13.jpg)
![](class10subchap/stringsmethodimg/img14.jpg)
In the above example, we can observe that:-
-
when String hello is compared to String "hello" , it will result zero,because these
String are equal
hello.compareTo("hello")
-
When String "hello" is compared to String "Hello", it will result to positive value
32,as
here we are taking difference of unicode(h) and unicode(H) which will output to
positive
value 32.
-
When String "hello" is compared to String "world", it will result to negative value
-15,as
here we are taking difference of unicode(h) and unicode(w) which will output to
negative
value .
hello.compareTo("hello")
![](class10subchap/stringsmethodimg/img15.jpg)
![](class10subchap/stringsmethodimg/img16.jpg)
trim()
This function is used to remove all the white space from after and before the string.It will output to the copy of original string,without after and before whitespace.
![](class10subchap/stringsmethodimg/img17.jpg)
![](class10subchap/stringsmethodimg/img18.jpg)
equals(String str)
This function is basically used for comparision,it will compare two string and return true,if both
strings are equal,and return false,if both strings are not equal.
Example:-
![](class10subchap/stringsmethodimg/img21.jpg)
![](class10subchap/stringsmethodimg/img22.jpg)
equalsIgnoreCase(String str)
This function works similar to equals() function,it check whether two strings are equal or
not,and
return false if they are not equal and true if both strings are equal.
The only major difference between these two functions is equalsIgnoreCase() does not check the case
sensitivity,i.e uppercase and lowercase.That is it will return true if one string is in uppercase
and
other string is in lowercase.
Example:-
![](class10subchap/stringsmethodimg/img19.jpg)
![](class10subchap/stringsmethodimg/img20.jpg)