String class method

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.

int length()
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.

int compareToIgnoreCase()
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.

String toLowerCase()
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”.

String toUpperCase()
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”.

String trim()
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.

String split(delimiter)

This method is useful to break a string into pieces at places represented by the delimiter.

Program of string class method
class test
public static void main(String args[])
String s1=””;
String s2=”I like it”;
char arr[]={‘p’,’r’,’o’,’s’,’o’,’f’,’t’,’e’,’k’};
String s3=new String(arr);
//to display all string
//to find length of first string
System.out.println(“length of s1 = “+s1.length());
//concatinate two string
System.out.println(“s1 and s2 joined=”+s1.concat(s2));
//test string s1 is start with A or not
boolean x = s1.startsWith(“A”);
System.out.println(“s1 starts with A”);
System.out.println(“s1 is not starts with A”);
//extract substring from s3
String q=s3.substring(0,4);
//convert s1 to uppercase
System.out.println(“Upper s1=”+s1.toUpperCase());

I like it
length of s1 = 12
s1 and s2 joined=basicofy.comI like it
s1 is not starts with A

Leave a Reply

Your email address will not be published. Required fields are marked *