-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuiltInMethods.java
More file actions
32 lines (28 loc) · 1.84 KB
/
builtInMethods.java
File metadata and controls
32 lines (28 loc) · 1.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
//Question: ALL ABOUT BUILT IN METHODS OF STRING
package strings;
public class builtInMethods {
public static void main(String[] args) {
// length(); charAt();
String s = "Raj Sarkar";
System.out.println("Length= " + s.length());
System.out.println("Index 2 element= " + s.charAt(2));
// indexOf(); compareTo();
System.out.println("Index of 'a' from first= " + s.indexOf('a'));
System.out.println("Index of 'a' from last= " + s.lastIndexOf('a'));
String p = "Ram Sarkar";
System.out.println("Compare of 2 Strings= " + s.compareTo(p)); // compareTo() is used to comapre 2 Strings lexographically. Pahle dono Strings ka first element compare karega. Agar ascii value main difference hai to wo print kar dega. Aur fir next element check karega.
// contains(); startsWith();
System.out.println("Is sub String (Sar) present in s String? = " + s.contains("Sar")); // Boolean output
System.out.println("Is s String starts with (Ra)? = " + s.startsWith("Ra"));
System.out.println("Is s String ends with (mar)? = " + s.endsWith("mar"));
// toLowerCase(); concat();
System.out.println("Make all elements of s to lower case= " + s.toLowerCase());
System.out.println("Make all elements of s to upper case= " + s.toUpperCase());
System.out.println("Concat 2 Strings= " + s.concat(p));
// substring();
System.out.println("Sub String from index 4= " + s.substring(4)); // Print the 4th index and aage wale indices ke elements
System.out.println("Get the part of the string from 1 to 6-1 index= " + s.substring(1,6));
System.out.println(s.substring(2,2)); // Kuch bhi print nahi hoga
System.out.println(s.substring(2,3)); // 2nd index wala element print ho jayega
}
}