This commit is contained in:
Zack Buhman 2025-01-12 18:19:35 -06:00
parent f0880a5e63
commit 110f0e7de9
2 changed files with 50 additions and 12 deletions

View File

@ -5,8 +5,6 @@ class Boolean {
public static final Boolean TRUE = new Boolean(true); public static final Boolean TRUE = new Boolean(true);
public static final Class<Boolean> TYPE = (Class<Boolean>)Class.getPrimitiveClass("boolean");
private final boolean value; private final boolean value;
public Boolean(boolean value) { public Boolean(boolean value) {

View File

@ -1,6 +1,9 @@
package java.lang; package java.lang;
public class String {
public final class String
implements Comparable<String>, CharSequence {
private final char[] value; private final char[] value;
public String() { public String() {
@ -26,14 +29,14 @@ public class String {
this(value, 0, value.length); this(value, 0, value.length);
} }
public String(char[] value, int offset, int length) { public String(char[] value, int offset, int count) {
this.value = new char[length]; this.value = new char[count];
int i = 0; int i = 0;
while (length > 0) { while (count > 0) {
this.value[i] = value[offset]; this.value[i] = value[offset];
i += 1; i += 1;
offset += 1; offset += 1;
length -= 1; count -= 1;
} }
} }
@ -47,17 +50,54 @@ public class String {
} }
public int compareTo(String anotherString) { public int compareTo(String anotherString) {
int length = this.length() < anotherString.length() int length = this.value.length < anotherString.value.length
? this.length() ? this.value.length
: anotherString.length(); : anotherString.value.length;
for (knt k = 0; k < length; k++) { for (knt k = 0; k < length; k++) {
int difference = this.charAt(k) - anotherString.charAt(k); int difference = this.value[k] - anotherString.value[k];
if (difference != 0) if (difference != 0)
return difference; return difference;
} }
return this.length() - anotherString.length(); return this.value.length - anotherString.value.length;
}
public int compareToIgnoreCase(String str) {
int length = this.value.length < anotherString.value.length
? this.value.length
: anotherString.value.length;
for (knt k = 0; k < length; k++) {
char a = Character.toLowerCase(Character.toUpperCase(this.value[k]));
char b = Character.toLowerCase(Character.toUpperCase(anotherString.value[k]));
int difference = a - b;
if (difference != 0)
return difference;
}
return this.value.length - anotherString.value.length;
}
public String concat(String str) {
if (str.value.length == 0)
return this;
if (value.length == 0)
return str;
int length_a = this.value.length;
int length_b = str.value.length;
char[] new_value = new char[length_a + length_b];
for (int i = 0; i < length_a; i++) {
new_value[i] = value[i];
}
for (int i = 0; i < length_b; i++) {
new_value[length_a + i] = str.value[i];
}
return new String(new_value);
}
public boolean contains(CharSequence s) {
} }
public String toString() { public String toString() {