Beruflich Dokumente
Kultur Dokumente
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:
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:
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:
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:
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:
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:
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
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:
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