139 lines
3.4 KiB
Java
139 lines
3.4 KiB
Java
package java.lang;
|
|
|
|
|
|
public final class String
|
|
implements Comparable<String>, CharSequence {
|
|
|
|
private final char[] value;
|
|
|
|
public String() {
|
|
this.value = new char[0];
|
|
}
|
|
|
|
public String(byte[] bytes) {
|
|
this(bytes, 0, bytes.length);
|
|
}
|
|
|
|
public String(byte[] bytes, int offset, int length) {
|
|
this.value = new char[length];
|
|
int i = 0;
|
|
while (length > 0) {
|
|
this.value[i] = (char)bytes[offset];
|
|
i += 1;
|
|
offset += 1;
|
|
length -= 1;
|
|
}
|
|
}
|
|
|
|
public String(char[] value) {
|
|
this(value, 0, value.length);
|
|
}
|
|
|
|
public String(char[] value, int offset, int count) {
|
|
this.value = new char[count];
|
|
int i = 0;
|
|
while (count > 0) {
|
|
this.value[i] = value[offset];
|
|
i += 1;
|
|
offset += 1;
|
|
count -= 1;
|
|
}
|
|
}
|
|
|
|
public String(String original) {
|
|
// shallow copy
|
|
this.value = original.value;
|
|
}
|
|
|
|
public char charAt(int index) {
|
|
return this.value[index];
|
|
}
|
|
|
|
public int compareTo(String anotherString) {
|
|
int length = this.value.length < anotherString.value.length
|
|
? this.value.length
|
|
: anotherString.value.length;
|
|
|
|
for (knt k = 0; k < length; k++) {
|
|
int difference = this.value[k] - anotherString.value[k];
|
|
if (difference != 0)
|
|
return difference;
|
|
}
|
|
|
|
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() {
|
|
return this;
|
|
}
|
|
|
|
public int length() {
|
|
return value.length;
|
|
}
|
|
|
|
public static String valueOf(boolean b) {
|
|
return b ? "true" : "false";
|
|
}
|
|
|
|
public static String valueOf(char c) {
|
|
return new String(new char[] { c });
|
|
}
|
|
|
|
public static String valueOf(int i) {
|
|
return Integer.toString(i);
|
|
}
|
|
|
|
public static String valueOf(long l) {
|
|
return Long.toString(l);
|
|
}
|
|
|
|
public static String valueOf(float f) {
|
|
return Float.toString(f);
|
|
}
|
|
|
|
public static String valueOf(double d) {
|
|
return Double.toString(d);
|
|
}
|
|
|
|
public static String valueOf(Object obj) {
|
|
return (obj == null) ? "null" : obj.toString();
|
|
}
|
|
}
|