-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStrings.java
86 lines (70 loc) · 2.55 KB
/
Strings.java
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
public class Strings {
/*
? System.out
* System.out.print() - No new line
* System.out.println() - New line
* System.out.printf() - Format specifier // Good if prior knowledge of formatting (in C) is there
%d - Integer
%f - Float
%s - String
%c - Character
*/
public static void main(String[] args) {
String a = "Hardvan";
String b = new String("Hardvan");
/*
* Difference between a & b:
* a -> reference variable
* b -> object variable
*/
// ? String Methods
// Strings are immutable
String s = "Hardvan";
System.out.println("String: " + s);
// * .length()
int length = s.length();
System.out.printf("Length: %d%n", length); // %n is a new line
// * .toLowerCase()
String lower = s.toLowerCase();
System.out.printf("Lowercase: %s%n", lower);
// * .toUpperCase()
String upper = s.toUpperCase();
System.out.printf("Uppercase: %s%n", upper);
// * .trim() (Removes spaces from both sides)
String trim = " Hardvan ".trim();
System.out.printf("Trim: %s%n", trim);
// * .substring()
// Returns a substring from [start, end)
String sub1 = s.substring(2);
String sub2 = s.substring(2, 4);
System.out.printf("Substring: %s%n", sub1);
System.out.printf("Substring: %s%n", sub2);
// * .replace()
String rep = s.replace('a', 'e');
System.out.printf("Replace: %s%n", rep);
String rep2 = s.replace("van", "ik");
System.out.printf("Replace: %s%n", rep2);
// * .startsWith()
boolean starts = s.startsWith("Ha");
System.out.printf("Starts with: %b%n", starts);
// * .endsWith()
boolean ends = s.endsWith("van");
System.out.printf("Ends with: %b%n", ends);
// * .charAt()
char ch = s.charAt(2);
System.out.printf("Char at: %c%n", ch);
// * .indexOf()
int index = s.indexOf("van");
System.out.printf("Index of: %d%n", index);
// * .lastIndexOf()
// Returns the last index of the given character/string
int lastIndex = s.lastIndexOf("a");
System.out.printf("Last index of: %d%n", lastIndex);
// * .equals()
boolean eq = s.equals("Hardvan");
System.out.printf("Equals: %b%n", eq);
// * .equalsIgnoreCase()
boolean eqI = s.equalsIgnoreCase("hardvan");
System.out.printf("Equals ignore case: %b%n", eqI);
}
}