Sunday, November 4, 2018

JAVA Theory Part-9

String Functions




String – String is a series of character which includes spaces also. String is a class not a data type.

1.         length();
Syntax- int length();
Usage- This function returns the length of characters present in the string.

2.         charAt();
Syntax- char charAt(int n);
Usage- This function returns nth character of the string.

3.         toUpperCase();
Syntax- String toUpperCase();
Usage- This function/method is to convert all the character of the string into uppercase.

4.         toLowerCase();
Syntax- String toLowerCase();
Usage- This function/method is to convert all the     characters of the string into lowercase.

5.         replace();
Syntax- String replace(char ch1,char ch2);
Ch1- character to be replaced.
Ch2- character by which to be replaced.
Usage- This function replaces all the occurrences of the character ch1 with ch2 in the string.

6.         trim();
Syntax- String trim();
Usage- This function is used to remove all the white spaces at the beginning and end of the string.

7.         equals();
Syntax- boolean equals(String2);
Example-s1.equals(s2);
Usage- This function gives true if s1 equals to s2.

8.         concat():
Syntax- String concat(String2);
Example-s1.concat(String s2);
Usage- This function concatenates s1 equal to s2.

9.         substring();
Syntax- String substring(int n);
Usage-This function returns substring starting from the nth character of the string.

10.       substring();[another format]
Syntax- String substring(m,n);
Usage-This function returns substring starting from mth character upto the nth character without including the nth character.

11.       indexOf();
Syntax for string- int indexof(String str);
Syntax for character- int indexOf(char ch);
Usage-This returns the position of the first occurrence of the character or string in the string.

12.       indexOf()(another format)
Syntax- int indexOf(String str, int start index)
int indexOf(char ch , int start index)
Example- int indexOf(“the”,n);
int indexOf(‘y’,n);
This format gives the position of “the” and ‘y’ starting from the nth position in String.

13.       compareTo();
Syntax- int compareTo(string str);
Example-s.compareTo(s1);
Usage-This function returns negative if s is less than s1, positive if s is greater than s1 and zero if s is equal to s1.
This compares two string lexicographically.

14. boolean endsWith(String str)
Usage- Tests if the this string (current Stringobject) ends with the specified suffix (str).

15. int lastIndexOf(char ch)
Usage-returns the index within the this string of the last occurrence of the specified character.

16.       stringreplace(string s1, string s2)

Usage-It replace string s1 with string s2.

No comments:

Post a Comment

Sample Paper - Class 10th