-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStringDataType.java
170 lines (143 loc) · 7.51 KB
/
StringDataType.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
public class StringDataType {
public static void main(String[] args) {
// Initialize String variables
String str1 = "Hello"; // A string literal
String str2 = "World"; // Another string literal
String str3 = "HELLO"; // A string with different case
String str4 = " Java Programming "; // String with leading/trailing spaces
// Display string properties and methods
displayStringProperties(str1, str2, str3, str4);
// Demonstrate string comparison
demonstrateStringComparison(str1, str3);
// Demonstrate string concatenation
demonstrateStringConcatenation(str1, str2);
// Demonstrate string trimming
demonstrateStringTrimming(str4);
// Demonstrate string conversion to upper and lower case
demonstrateStringCaseConversion(str1);
// Demonstrate string replacement
demonstrateStringReplacement(str1);
// Demonstrate string splitting
demonstrateStringSplitting(str4);
// Demonstrate string substring
demonstrateStringSubstring(str1);
// Demonstrate string contains
demonstrateStringContains(str2);
// Demonstrate static methods
demonstrateStaticMethods();
}
// Method to display string properties and various methods
private static void displayStringProperties(String str1, String str2, String str3, String str4) {
System.out.println("String Properties and Methods:");
System.out.println("Length of str1: " + str1.length());
System.out.println("Char at index 1 in str2: " + str2.charAt(1));
System.out.println("Does str1 contain 'el': " + str1.contains("el"));
System.out.println("Does str2 start with 'Wo': " + str2.startsWith("Wo"));
System.out.println("Does str2 end with 'ld': " + str2.endsWith("ld"));
System.out.println("Are str1 and str2 equal? " + str1.equals(str2));
System.out.println("Are str1 and str3 equal ignoring case? " + str1.equalsIgnoreCase(str3));
System.out.println(); // Empty line for separation
}
// Method to demonstrate string comparison
private static void demonstrateStringComparison(String str1, String str3) {
System.out.println("String Comparison:");
// Compare two strings lexicographically
int comparisonResult = str1.compareTo(str3);
if (comparisonResult < 0) {
System.out.println(str1 + " is lexicographically less than " + str3);
} else if (comparisonResult > 0) {
System.out.println(str1 + " is lexicographically greater than " + str3);
} else {
System.out.println(str1 + " is lexicographically equal to " + str3);
}
System.out.println(); // Empty line for separation
}
// Method to demonstrate string concatenation
private static void demonstrateStringConcatenation(String str1, String str2) {
System.out.println("String Concatenation:");
// Concatenate two strings using + operator
String concatenatedString = str1 + " " + str2;
System.out.println("Concatenated string: " + concatenatedString);
// Concatenate using StringBuilder for performance
StringBuilder sb = new StringBuilder();
sb.append(str1).append(" ").append(str2);
System.out.println("Concatenated string using StringBuilder: " + sb.toString());
System.out.println(); // Empty line for separation
}
// Method to demonstrate string trimming
private static void demonstrateStringTrimming(String str4) {
System.out.println("String Trimming:");
// Trim leading and trailing spaces
String trimmedString = str4.trim();
System.out.println("Original string: '" + str4 + "'");
System.out.println("Trimmed string: '" + trimmedString + "'");
System.out.println(); // Empty line for separation
}
// Method to demonstrate string conversion to upper and lower case
private static void demonstrateStringCaseConversion(String str1) {
System.out.println("String Case Conversion:");
// Convert string to uppercase
String upperCaseString = str1.toUpperCase();
System.out.println("Uppercase: " + upperCaseString);
// Convert string to lowercase
String lowerCaseString = str1.toLowerCase();
System.out.println("Lowercase: " + lowerCaseString);
System.out.println(); // Empty line for separation
}
// Method to demonstrate string replacement
private static void demonstrateStringReplacement(String str1) {
System.out.println("String Replacement:");
// Replace 'H' with 'h'
String replacedString = str1.replace('H', 'h');
System.out.println("Replaced string (H to h): " + replacedString);
System.out.println(); // Empty line for separation
}
// Method to demonstrate string splitting
private static void demonstrateStringSplitting(String str4) {
System.out.println("String Splitting:");
// Split string by spaces
String[] splitStrings = str4.split(" ");
System.out.println("Split string by space:");
for (String s : splitStrings) {
System.out.println("'" + s + "'");
}
System.out.println(); // Empty line for separation
}
// Method to demonstrate string substring
private static void demonstrateStringSubstring(String str1) {
System.out.println("String Substring:");
// Extract a substring from the string
String substring = str1.substring(1, 4); // Extract from index 1 to 4
System.out.println("Substring (1, 4): " + substring);
System.out.println(); // Empty line for separation
}
// Method to demonstrate string contains
private static void demonstrateStringContains(String str2) {
System.out.println("String Contains:");
// Check if the string contains a specific substring
boolean containsResult = str2.contains("orl");
System.out.println("Does str2 contain 'orl'? " + containsResult);
System.out.println(); // Empty line for separation
}
// Method to demonstrate static methods
private static void demonstrateStaticMethods() {
System.out.println("Static String Methods:");
// Demonstrate valueOf method
String strFromInt = String.valueOf(42); // Converts int to String
System.out.println("String.valueOf(42): " + strFromInt);
// Demonstrate format method
String formattedString = String.format("Hello, %s!", "World"); // Format with placeholders
System.out.println("String.format(\"Hello, %s!\", \"World\"): " + formattedString);
// Demonstrate join method
String joinedString = String.join("-", "2025", "01", "09"); // Join strings with a delimiter
System.out.println("String.join(\"-\", \"2025\", \"01\", \"09\"): " + joinedString);
// Demonstrate copyValueOf method
String copyString = String.copyValueOf(new char[] { 'H', 'i' }); // Copy characters to a string
System.out.println("String.copyValueOf(new char[] {'H', 'i'}): " + copyString);
// Demonstrate copyValueOf with offset and count
String copySubstring = String.copyValueOf(new char[] { 'H', 'e', 'l', 'l', 'o' }, 1, 3); // Copy a part of the
// array
System.out.println("String.copyValueOf(new char[] {'H', 'e', 'l', 'l', 'o'}, 1, 3): " + copySubstring);
System.out.println(); // Empty line for separation
}
}