-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlab2.java
More file actions
117 lines (90 loc) · 4.16 KB
/
lab2.java
File metadata and controls
117 lines (90 loc) · 4.16 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
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
import java.util.Arrays;
public class lab2 {
public static void main(String[] args) {
String text = "We realizes that while our workers were thriving, the " +
"surrounding villages were still suffering. It became our " +
"goal to uplift their quality of life as well.";
// charAt()
char firstChar = text.charAt(0);
System.out.println("charAt(): " + firstChar);
// compareTo()
String anotherText = "Another text for comparison";
int compareResult = text.compareTo(anotherText);
System.out.println("compareTo(): " + compareResult);
// concat()
String combinedText = text.concat(" " + anotherText);
System.out.println("concat(): " + combinedText);
// contains()
boolean containsWord = text.contains("workers");
System.out.println("contains(): " + containsWord);
// endsWith()
boolean endsWithPeriod = text.endsWith(".");
System.out.println("endsWith(): " + endsWithPeriod);
// equals()
boolean isEqual = text.equals(anotherText);
System.out.println("equals(): " + isEqual);
// equalsIgnoreCase()
boolean isEqualIgnoreCase = text.equalsIgnoreCase(anotherText);
System.out.println("equalsIgnoreCase(): " + isEqualIgnoreCase);
// format()
String formattedText = String.format("Formatted text: %s", text);
System.out.println("format(): " + formattedText);
// getBytes()
byte[] textBytes = text.getBytes();
System.out.println("getBytes(): " + Arrays.toString(textBytes));
// getChars()
char[] charArray = new char[text.length()];
text.getChars(0, text.length(), charArray, 0);
System.out.println("getChars(): " + Arrays.toString(charArray));
// indexOf()
int indexOfThrive = text.indexOf("thriving");
System.out.println("indexOf(): " + indexOfThrive);
// intern()
String internedText = text.intern();
System.out.println("intern(): " + internedText);
// isEmpty()
boolean isEmpty = text.isEmpty();
System.out.println("isEmpty(): " + isEmpty);
// join()
String[] words = text.split(" ");
String joinedText = String.join("-", words);
System.out.println("join(): " + joinedText);
// lastIndexOf()
int lastIndexOfVillages = text.lastIndexOf("villages");
System.out.println("lastIndexOf(): " + lastIndexOfVillages);
// length()
int textLength = text.length();
System.out.println("length(): " + textLength);
// replace()
String replacedText = text.replace("workers", "employees");
System.out.println("replace(): " + replacedText);
// replaceAll()
String regexReplacedText = text.replaceAll("\\b(\\w+)\\b", "$1_replaced");
System.out.println("replaceAll(): " + regexReplacedText);
// split()
String[] splitText = text.split(" ");
System.out.println("split(): " + Arrays.toString(splitText));
// startsWith()
boolean startsWithWe = text.startsWith("We");
System.out.println("startsWith(): " + startsWithWe);
// substring()
String substringText = text.substring(10, 30);
System.out.println("substring(): " + substringText);
// toCharArray()
char[] charArrayFromText = text.toCharArray();
System.out.println("toCharArray(): " + Arrays.toString(charArrayFromText));
// toLowerCase()
String lowerCaseText = text.toLowerCase();
System.out.println("toLowerCase(): " + lowerCaseText);
// toUpperCase()
String upperCaseText = text.toUpperCase();
System.out.println("toUpperCase(): " + upperCaseText);
// trim()
String trimmedText = text.trim();
System.out.println("trim(): " + trimmedText);
// valueOf()
int intValue = 42;
String stringValue = String.valueOf(intValue);
System.out.println("valueOf(): " + stringValue);
}
}