-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathStringClass.java
More file actions
59 lines (46 loc) · 1.43 KB
/
Copy pathStringClass.java
File metadata and controls
59 lines (46 loc) · 1.43 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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
//String class in java
public class StringClass
{
public static void main(String []args)
{
String s1 = new String("hello World");
String s2,s3;
System.out.println(s1);
s2 = s1.toUpperCase(); //converting to uppercase
System.out.println(s2);
s3 = s1.toLowerCase(); //converting to lowercase
System.out.println(s3);
System.out.println(s1); //s1 is not changed
//indexOf method
int i = s1.indexOf("o");
System.out.println("\no found at indexOf " +i);
//checking string index
System.out.println("\nString found at "+s1.indexOf("Wo"));
//checking at given index
System.out.println("\nlastIndexOf " +s1.lastIndexOf("l"));
//camparing two string
String str1 = "Hello World";
String str2 = "Hello programmer";
if(str1.equals(str2))
System.out.println("Strings are same\n ");
else
System.out.println("Strings are not same\n");
if(str1.equalsIgnoreCase(str2))
System.out.println("Strings are same\n");
else
System.out.println("String are not same\n");
//compareTo method
int y = str1.compareTo(str2);
if(y==0)
System.out.println("Strings are same\n");
else if(y<0)
System.out.println("Dictionary order ");
else
System.out.println("opposite of dictionary order");
//substring find at index slicing
String a = str1.substring(4);
System.out.println("\nsubstring found "+a);
a = str1.substring(4,7);
System.out.println("\nsubstring found "+a);
}
}