Sei sulla pagina 1di 5

visit @ http://www.infotechcomputer.

in

“Jh x.ks'kk; ue%“

‘Java String Functions’

Strings - Collection of characters is called a string. String is a reference data type in java.

Few of the methods that are in ur course

1. length () - this method of string class is used to find length of string.

for eg.

String k="infotech";

sout(k.length()); - it will print 8

2. charAt(int i) - this method of string class is used to extract a character from given index position of the

string.

for eg.

String k="infotech";

sout(k.charAt(2)) - ti will print f

INFOTECH Computer Education, Kishangarh (Raj)  9829171122 Page 1


visit @ http://www.infotechcomputer.in

NOTE - index value of characters starts at 0

3. concat(String m) - this method of string class is used to concat a string into another string.

for eg.

String k="infotech";

String m="computer";

sout(k.concat(m)); - it will print infotechcomputer

NOTE - we can also add two strings in java using + operator

for eg.

String k="infotech";

String m="computer";

sout(k+m); - will print infotechcomputer

4. equals(String str) - this method of string class is used to compare two strings for thier equality.

for eg.

String k="infotech";

String m="infotech";

sout(k.equals(m)); - it will print true bcoz both of the strings are same

INFOTECH Computer Education, Kishangarh (Raj)  9829171122 Page 2


visit @ http://www.infotechcomputer.in

5. indexOf(char ch) - this method of string class is used to find position of first occurence of the given

character in string.

for eg.

String k="infotech";

sout(k.indexOf('f')); - it will print 2

6. equalsIgnoreCase(String str) - this method of string class is same as equals but it compares two string for

thier equality and ignores the case.

for eg.

String k="infotech";

String m="INFOTECH";

sout(k.equals(m)); - will print false bcoz both strings are not same

sout(k.equalsIgnoreCase(m)); - will print true bcoz we have used equalsignorecase

7. lastIndexOf(char ch) - this function of string class is same as IndexOf function but it will find the position

of last occurence of the given character in string.

for eg.

String k="infotech computer";

sout(k.lastIndexOf('o')); - will print 10

INFOTECH Computer Education, Kishangarh (Raj)  9829171122 Page 3


visit @ http://www.infotechcomputer.in

8. startsWith(String str) - this function is used to find whether the string starts with the given string or not

for eg.

String k="infotech comptuer"

sout(k.startsWith("info")); - will print true

9. endsWith(String str) - just opposite of startsWith

10. toLowerCase() - this function coverts the string to lowercase letters

for eg.

String k="iNfOteCh";

sout(k.toLowerCase()); - will print "infotech"

11. to UpperCase() - just opposite of toLowerCase

12. trim() - will remove blank spaces from both sides of string.

for eg.

String k=" infotech ";

sout(k.trim()); - will print "infotech"

13. replace(char oldchar, char newchar) - this function will replace the given old character in string with

given new character

for eg.
INFOTECH Computer Education, Kishangarh (Raj)  9829171122 Page 4
visit @ http://www.infotechcomputer.in

String k="infotech";

sout(k.replace('t','s')); - will print infosech

14. substring(int begin,int end) - this funtion is used to extract a part of string from it.

for eg.

String k="infotech";

sout(k.substring(3,6)); - will print "ote"

NOTE - index starts with 0 and 6th character will not print

INFOTECH Computer Education, Kishangarh (Raj)  9829171122 Page 5

Potrebbero piacerti anche