Sie sind auf Seite 1von 11

Piyush Chandekar

4. String Handling in JAVA


Methods of String class 1. length()
syntax:

Int length();
length() method of string class is used to count the number of characters of invoking String object. It returns the length of string.

class Example1 { Public static void main(String arg[ ]) { String s1=PC Solutions; int i = s1.length(); System.out.println(length = + i); } } o/p:length=12

Piyush Chandekar

2. charAt()
syntax:

char charAt(int index);

charAt() method of string class is used to find character at a given index number from the string of invoking String object.

class Example2 { Public static void main(String arg[ ]) { String s1=PC Solutions; char ch = s1.charAt(1); System.out.println(ch); System.out.println(s1.charAt(0)); } } o/p:C P

Piyush Chandekar

3. indexOf()
syntax:

int indexOf(char ch);


indexOff() method of string class is used to find position of given character, if given character is present in invoking string then it returns index number of the given character otherwise returns -1.

class Example3 { Public static void main(String arg[ ]) { String s1=PC Solutions; int n = s1.indexOf(P); System.out.println(n); System.out.println(s1.indexOf(u)); System.out.println(s1.indexOf(b)); System.out.println(s1.indexOf(k)); } } o/p:0 6 -1 -1
3

Piyush Chandekar

4. lastIndexOf()
syntax:

int lastIndexOf(char ch);


lastIndexOff() method of string class is used to find position of given character from the last index number of invoking object if the given character is present then it returns index number of the given character otherwise returns -1.

class Example4 { Public static void main(String arg[ ]) { String s1=PC Solutions; int n = s1.lastIndexOf(P); System.out.println(n); System.out.println(s1.indexOf(u)); System.out.println(s1.indexOf(o)); System.out.println(s1.indexOf(k)); } } o/p:0 6 9 -1
4

Piyush Chandekar

5. startsWith()
syntax:

boolean startsWith(String str) char ch);


startsWith() method of string class is used to compare the given string with the starting character of invoking object, if the given string is matched with starting character of invoking object then it returns true otherwise it will return false. It is case sensitive method.

class Example5 { Public static void main(String arg[ ]) { String s1=PC Solutions; boolean b = s1. startsWith(PC); System.out.println(b); System.out.println(s1. startsWith(tions)); System.out.println(s1. startsWith(solutions)); System.out.println(s1. startsWith(PC Solutions)); } } o/p:true false false true
5

Piyush Chandekar

6. endsWith()
syntax:

boolean endsWith(String str) char ch);


endsWith() method of string class is used to compare the given string with the last character of invoking object, if the given string is matched with last character of invoking object then it returns true otherwise it will return false. It is case sensitive method.

class Example6 { Public static void main(String arg[ ]) { String s1=PC Solutions; boolean b = s1. startsWith(PC); System.out.println(b); System.out.println(s1. endsWith(tions)); System.out.println(s1. endsWith(solutions)); System.out.println(s1. endsWith(PC Solutions)); } } o/p:false true true true

Piyush Chandekar

7. equals()
syntax:

boolean equals (String str) char ch);


equals() method of string class is used to compare the given string with the string of invoking object, if the both strings are equal then it returns true otherwise it will return false. It is case sensitive method.

class Example7 { Public static void main(String arg[ ]) { String s1=PC Solutions; boolean b = s1. equals(PC); System.out.println(b); System.out.println(s1. equals(tions)); System.out.println(s1. equals(solutions)); System.out.println(s1. equals(PC Solutions)); } } o/p:false false false true
7

Piyush Chandekar

8. equalsIgnoreCase()
syntax:

boolean equalsIgnoreCase (String str) char ch); equalsIgnoreCase() method of string class is used to compare the given string
with the string of invoking object by ignoring case. if the both strings are equal then it returns true otherwise it will return false.

class Example8 { Public static void main(String arg[ ]) { String s1=PC Solutions; boolean b = s1. equalsIgnoreCase (pc); System.out.println(b); System.out.println(s1. equalsIgnoreCase (tions)); System.out.println(s1. equalsIgnoreCase (solutions)); System.out.println(s1. equalsIgnoreCase (pc solutions)); } } o/p:false false false true
8

Piyush Chandekar

9. toUpperCase() and toLowerCase()


syntax:

String toUpperCase(); String toLowerCase();


toUpperCase() and toLowerCase() method of string class is used to convert the string of invoking object into upper case and lower case resp. & returns the resultant string.

class Example9 { Public static void main(String arg[ ]) { String s1=PC Solutions; String s2 = s1.toUpperCase(); System.out.println(s1); System.out.println(s2); System.out.println(s1. toLowerCase); } } o/p:PC Solutions PC SOLUTIONS Pc solutions

Piyush Chandekar

10.

trim()
syntax:

String trim();
trim() method of string class is used to the prefix and postfix spaces of invoking string & returns the resultant string.

class Example10 { Public static void main(String arg[ ]) { String s1= PC Solutions ; System.out.println(s1); String s2 = s1.trim(); System.out.println(s2); } } o/p:PC Solutions PC Solutions

10

Piyush Chandekar

11.

substring()
syntax:

String substring(int index); String substring(int startIndex, int endIndex);

substring() method of string class is an overloaded method, available in two versions. 1. substring() :- to retrieve part of string from given index number to last index number and returns the resultant string. 2. substring :- to retrieve a part of string from invoking object.
class Example11 { Public static void main(String arg[ ]) { String s1=PC Solutions; System.out.println(s1); String s2 = s1. substring(1); System.out.println(s2); String s3 = s1. Substring(1,4); System.out.println(s3); } } o/p:PC Solutions C Solutions CS
11

Das könnte Ihnen auch gefallen