11 private int cachedHashcode;
16 public String(char c) {
17 char[] str = new char[1];
22 public String(char str[]) {
23 char charstr[] = new char[str.length];
24 for (int i = 0; i < str.length; i++)
27 this.count = str.length;
31 public String(byte str[]) {
32 char charstr[] = new char[str.length];
33 for (int i = 0; i < str.length; i++)
34 charstr[i] = (char) str[i];
36 this.count = str.length;
40 public String(byte str[], int offset, int length) {
41 if (length > (str.length - offset))
42 length = str.length - offset;
43 char charstr[] = new char[length];
44 for (int i = 0; i < length; i++)
45 charstr[i] = (char) str[i + offset];
51 public String(byte str[], String encoding) {
52 int length = this.count;
53 if (length > (str.length))
55 char charstr[] = new char[length];
56 for (int i = 0; i < length; i++)
57 charstr[i] = (char) str[i];
63 public String(char str[], int offset, int length) {
64 if (length > (str.length - offset))
65 length = str.length - offset;
66 char charstr[] = new char[length];
67 for (int i = 0; i < length; i++)
68 charstr[i] = str[i + offset];
74 public String(String str) {
75 this.value = str.value;
76 this.count = str.count;
77 this.offset = str.offset;
80 public String(StringBuffer strbuf) {
81 value = new char[strbuf.length()];
82 count = strbuf.length();
84 for (int i = 0; i < count; i++)
85 value[i] = strbuf.value[i];
88 public boolean endsWith(String suffix) {
89 return regionMatches(count - suffix.count, suffix, 0, suffix.count);
92 public String substring(int beginIndex) {
93 return substring(beginIndex, this.count);
96 public String subString(int beginIndex, int endIndex) {
97 return substring(beginIndex, endIndex);
100 public String substring(int beginIndex, int endIndex) {
101 String str = new String();
102 if (beginIndex > this.count || endIndex > this.count || beginIndex > endIndex) {
104 System.printString("Index error: " + beginIndex + " " + endIndex + " " + count + "\n" + this);
106 str.value = this.value;
107 str.count = endIndex - beginIndex;
108 str.offset = this.offset + beginIndex;
112 public String subString(int beginIndex) {
113 return this.subString(beginIndex, this.count);
116 public int lastindexOf(int ch) {
117 return this.lastindexOf(ch, count - 1);
120 public int lastIndexOf(char ch) {
121 return this.lastindexOf((int) ch, count - 1);
124 public static String concat2(String s1, String s2) {
126 return "null".concat(s2);
128 return s1.concat(s2);
131 public String concat(String str) {
132 String newstr = new String();
133 newstr.count = this.count + str.count;
134 char charstr[] = new char[newstr.count];
136 for (int i = 0; i < count; i++) {
137 charstr[i] = value[i + offset];
139 for (int i = 0; i < str.count; i++) {
140 charstr[i + count] = str.value[i + str.offset];
142 newstr.value = charstr;
146 public int lastindexOf(int ch, int fromIndex) {
147 for (int i = fromIndex; i > 0; i--)
148 if (this.charAt(i) == ch)
153 public String replace(char oldch, char newch) {
154 char[] buffer = new char[count];
155 for (int i = 0; i < count; i++) {
161 return new String(buffer);
164 public String toUpperCase() {
165 char[] buffer = new char[count];
166 for (int i = 0; i < count; i++) {
168 if (x >= 'a' && x <= 'z') {
169 x = (char) ((x - 'a') + 'A');
173 return new String(buffer);
176 public String toLowerCase() {
177 char[] buffer = new char[count];
178 for (int i = 0; i < count; i++) {
180 if (x >= 'A' && x <= 'Z') {
181 x = (char) ((x - 'A') + 'a');
185 return new String(buffer);
188 public int indexOf(int ch) {
189 return this.indexOf(ch, 0);
192 public int indexOf(int ch, int fromIndex) {
193 for (int i = fromIndex; i < count; i++)
194 if (this.charAt(i) == ch)
199 public int indexOf(String str) {
200 return this.indexOf(str, 0);
203 public int indexOf(String str, int fromIndex) {
206 for (int i = fromIndex; i <= (count - str.count); i++)
207 if (regionMatches(i, str, 0, str.count))
212 public int indexOfIgnoreCase(String str, int fromIndex) {
217 public int lastIndexOf(String str, int fromIndex) {
218 int k = count - str.count;
221 for (; k >= 0; k--) {
222 if (regionMatches(k, str, 0, str.count))
228 public int lastIndexOf(String str) {
229 return lastIndexOf(str, count - str.count);
232 public boolean startsWith(String str) {
233 return regionMatches(0, str, 0, str.count);
236 public boolean startsWith(String str, int toffset) {
237 return regionMatches(toffset, str, 0, str.count);
240 public boolean regionMatches(int toffset, String other, int ooffset, int len) {
241 if (toffset < 0 || ooffset < 0 || (toffset + len) > count || (ooffset + len) > other.count)
243 for (int i = 0; i < len; i++)
244 if (other.value[i + other.offset + ooffset] != this.value[i + this.offset + toffset])
249 public char[] toCharArray() {
250 char str[] = new char[count];
251 for (int i = 0; i < count; i++)
252 str[i] = value[i + offset];
256 public byte[] getBytes() {
257 byte str[] = new byte[count];
258 for (int i = 0; i < count; i++)
259 str[i] = (byte) value[i + offset];
263 public void getChars(char dst[], int dstBegin) {
264 getChars(0, count, dst, dstBegin);
267 public void getChars(int srcBegin, int srcEnd, char dst[], int dstBegin) {
268 if ((srcBegin < 0) || (srcEnd > count) || (srcBegin > srcEnd)) {
270 System.printString("Index error: " + srcBegin + " " + srcEnd + " " + count + "\n" + this);
273 int len = srcEnd - srcBegin;
275 for (int i = srcBegin; i < srcEnd; i++)
276 dst[j++] = value[i + offset];
280 public int length() {
284 public char charAt(int i) {
285 return value[i + offset];
288 public String toString() {
292 public static String valueOf(Object o) {
299 public static String valueOf(boolean b) {
301 return new String("true");
303 return new String("false");
306 public static String valueOf(char c) {
307 char ar[] = new char[1];
309 return new String(ar);
312 public static String valueOf(int x) {
326 chararray = new char[length + 1];
328 chararray = new char[length];
338 chararray[--length + voffset] = (char) (x % 10 + '0');
340 } while (length != 0);
341 return new String(chararray);
344 public static String valueOf(double val) {
345 char[] chararray = new char[20];
346 String s = new String();
348 s.count = convertdoubletochar(val, chararray);
353 public static native int convertdoubletochar(double val, char[] chararray);
355 public static String valueOf(long x) {
370 chararray = new char[length + 1];
372 chararray = new char[length];
382 chararray[--length + voffset] = (char) (x % 10 + '0');
384 } while (length != 0);
385 return new String(chararray);
388 public int compareTo(String s) {
389 int smallerlength = count < s.count ? count : s.count;
391 for (int i = 0; i < smallerlength; i++) {
392 int valDiff = this.charAt(i) - s.charAt(i);
397 return count - s.count;
400 public int hashCode() {
401 if (cachedHashcode != 0)
402 return cachedHashcode;
404 for (int i = 0; i < count; i++)
405 hashcode = hashcode * 31 + value[i + offset];
406 cachedHashcode = hashcode;
410 public boolean equals(Object o) {
411 if (o.getType() != getType())
413 String s = (String) o;
414 if (s.count != count)
416 for (int i = 0; i < count; i++) {
417 if (s.value[i + s.offset] != value[i + offset])
423 public boolean equalsIgnoreCase(String s) {
424 if (s.count != count)
426 for (int i = 0; i < count; i++) {
427 char l = s.value[i + s.offset];
428 char r = value[i + offset];
429 if (l >= 'a' && l <= 'z')
430 l = (char) ((l - 'a') + 'A');
431 if (r >= 'a' && r <= 'z')
432 r = (char) ((r - 'a') + 'A');
439 public Vector split() {
440 Vector splitted = new Vector();
445 for (i = 0; i < count; i++) {
446 if (value[i + offset] != '\n' && value[i + offset] != '\t' && value[i + offset] != ' ')
453 if (value[i + offset] == '\n' || value[i + offset] == '\t' || value[i + offset] == ' ') {
454 String t = new String();
458 splitted.addElement(t);
461 while (i < count && (value[i + offset] == '\n' || value[i + offset] == '\t' || value[i + offset] == ' ')) {
471 String t = new String();
475 splitted.addElement(t);
481 public boolean contains(String str) {
483 char[] strChar = str.toCharArray();
486 for (i = 0; i < count; i++) {
487 if (value[i] == strChar[0]) {
489 for (j = 0; j < str.length() && i + j < count; j++) {
490 if (value[i + j] == strChar[j])
493 if (cnt == str.length())
502 public String trim() {
505 int off = offset; /* avoid getfield opcode */
506 char[] val = value; /* avoid getfield opcode */
508 while ((st < len) && (val[off + st] <= ' ')) {
511 while ((st < len) && (val[off + len - 1] <= ' ')) {
514 return ((st > 0) || (len < count)) ? substring(st, len) : this;
517 public boolean matches(String regex) {
518 System.println("String.matches() is not fully supported");
519 return this.equals(regex);