String class method
Let us look at which methods are available in String class
String concat(String s)
This method is used to combined two string. It returns combined string.
For example, String s3=s1.concat(s2);
here concat method joins two strings(s1 and s2) and returns a third string (s3) as a result.
This method returns the length or number of characters of a string.
For example, s1.length();
It gives the number of character in the string s1.
char charAt(int i)
This method returns the character at the specified location i.
For example, s1.chatAt(5);
It gives the 5th character in the string s1.
int compareTo(String s)
This method useful to compare two string and to know which string is bigger or smaller.
For example, s1.compareTo(s2);
here s1 and s2 are compared. If s1 and s2 are equal, then it returns 0. If s1 is greater than s2, then it returns positive number. If s1 is less than s2, then it returns negative number. This is case sensitive.
This is same as compareTo() method but this does not take the case of string into consideration. This means it is case insensitive.
boolean equals(String s)
This method returns true if two strings are same, otherwise false.
For example, s1.equals(s2);
boolean equalsIgnoreCase(String s)
This method is same as preceding, but it performs case insensitive comparison.
boolean startsWith(String s)
This method returns true if this string starts with given prefix else returns false.
For example, s1.startsWith(String s2);
here if s1 start with s2 then it returns true,otherwise false. This is case sensitive method.
boolean endsWith(String s)
It returns true if this string ends with given suffix else returns false.
For example, s1.endsWith(String s2);
here if s1 ends with s2 then it returns true,otherwise false. This is also case sensitive method.
int indexOf(String s)
This is method is called in the form s1.indexOf(s2), and it returns an integer value. If s1 contains s2 as a sub string, then the first occurrence of s2 in the string s1 will be returned by this method.
For example, s1=”This is book” and s2=”is”. To know the position of the s2 in s1 . We write,
int n = s1.indexOf(s2);
int lastIndexOf(String s)
This method is similar to the preceding method, but returns the last occurrence of the substring. If it is not found, it returns negative value.
String replace(char c1, char c2)
This method replacing all the old character to new character.
For example, s1=”hello”
here it returns the string “hexxo”.
String substring(int i)
This method is useful to extract substring from a main string.
For example, s1.substring(5) returns characters string from 5th character till the end of s1.
String substring(int i1, int i2)
This method returns a new string consisting of at character string from i1 till i2.
This method converts all characters of the string into lower case and returns that lower case string.
For example, s1=”BASICOFY”
here it returns “basicofy”.
This method converts all characters of the string into upper case and returns that upper case string.
For example, s1=”basicofy”
here it returns “BASICOFY”.
This mthod removes spaces from the beginning and ending of a string.
For example, ” Ravi Kiran ”
the spaces before Ravi and after Kiran are unnecessary and should be removed.
void getchar(int i1, int i2, char arr, int i3)
This method a copies characters from a string into a character array. The character starting from i1 to i2-1 in the string are copied into the array ‘arr’ to location starting from i3.
This method is useful to break a string into pieces at places represented by the delimiter.