001 
002 /*
003  *  JScripter Emulation 1.0 - To Script Java
004  *  Copyright (C) 2008-2011  J.J.Liu<jianjunliu@126.com> <http://www.jscripter.org>
005  *  
006  *  This program is free software: you can redistribute it and/or modify
007  *  it under the terms of the GNU Affero General Public License as published by
008  *  the Free Software Foundation, either version 3 of the License, or
009  *  (at your option) any later version.
010  *  
011  *  This program is distributed in the hope that it will be useful,
012  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
013  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
014  *  GNU Affero General Public License for more details.
015  *  
016  *  You should have received a copy of the GNU Affero General Public License
017  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
018  */
019 
020 package org.jscripter.emu.java.lang;
021 
022 import org.jscripter.emu.java.io.Serializable;
023 
024 import js.*;
025 import js.core.*;
026 import jsx.core.StringLikes;
027 
028 /**
029  * <p><b>Internally</b> represents character strings, emulating a standard <tt>java.lang</tt> interface or 
030  * class with the same simple name as this one.</p>
031  * <p>This interface or class is only used internally by JS re-compiler implementations.</p>
032  * <p>Please refer to <a href="http://java.sun.com/docs/">the Java API Standards</a> for detail description of the original class or interface.</p>
033  * 
034  * @author <a href="mailto:jianjunliu@126.com">J.J.Liu (Jianjun Liu)</a> at <a href="http://www.jscripter.org" target="_blank">http://www.jscripter.org</a>
035  * 
036  * @javascript Re-compilers must redirect the resolution of the emulated original Java class or interface to this one.
037  */
038 public final class String implements Comparable<String>, java.lang.CharSequence, Serializable
039 {
040     /**
041      * <p>Allocates a string representing an empty character sequence.</p>
042      * @since 1.0
043      * @javascript Re-compilers must report error on end-users directly using this constructor.
044      * A re-compiler simply replaces an invocation of this empty constructor with its argument.
045      */
046     public String() {
047     }
048 
049     /**
050      * <p>Allocates a new string so that it represents the sequence of characters currently contained 
051      * in the character array argument.</p>
052      * @param value An array that is the source of characters.
053      * @since 1.0
054      * @javascript Re-compilers must report error on end-users directly using this constructor.
055      * A re-compiler simply replaces an invocation of this constructor with an invocation of 
056      * {@link #valueOf(char[])} passing the argument.
057      */
058     public String(char value[]) {
059     }
060 
061     /**
062      * <p>Allocates a {@link String} representing an empty character sequence.</p>
063      * @param value An array that is the source of characters.
064      * @param offset The initial offset.
065      * @param count The length.
066      * @since 1.0
067      * @javascript Re-compilers must report error on end-users directly using this constructor.
068      * A re-compiler simply replaces an invocation of this constructor with an invocation of 
069      * {@link #valueOf(char[], int, int)} passing the arguments.
070      */
071     public String(char value[], int offset, int count) {
072     }
073 
074     /**
075      * <p>Initializes a newly created string so that it represents the same sequence of characters as 
076      * the argument.</p>
077      * @param original A string.
078      * @since 1.0
079      * @javascript Re-compilers must report error on end-users directly using this constructor.
080      * A re-compiler simply replaces an invocation of this constructor with an invocation of 
081      * {@link #valueOf(Object)} passing the argument.
082      */
083     public String(String original) {
084     }
085 
086     /**
087      * <p>Returns the string representation of the primitive argument.</p>
088      * @param x A primitive value.
089      * @return "true" if the argument is <tt>true</tt>; "false" otherwise.
090      * @since 1.0
091      * @javascript Re-compilers must report error on end-users directly using this method.
092      * As a "synchronized" static emulating method, its invocation should be re-compiled into in-lined code for efficiency.
093      */
094     public static synchronized java.lang.String valueOf(boolean x) {
095         return Boolean.toString(x);
096     }
097 
098     /**
099      * <p>Returns the string representation of the primitive argument.</p>
100      * @param x A primitive value.
101      * @return A string of length 1 containing the argument as its single character.
102      * @since 1.0
103      * @javascript Re-compilers must report error on end-users directly using this method.
104      * As a "synchronized" static emulating method, its invocation should be re-compiled into in-lined code for efficiency.
105      */
106     public static synchronized java.lang.String valueOf(char x) {
107         return JsGlobal.String.with().fromCharCode(x);
108     }
109 
110     static void checkBounds(int start, int end, int count) {
111         if (start < 0) {
112             throw new java.lang.StringIndexOutOfBoundsException(start);
113         }
114         if (end < start) {
115             throw new java.lang.StringIndexOutOfBoundsException(end - start);
116         }
117         if (end > count) {
118             throw new java.lang.StringIndexOutOfBoundsException(end);
119         }
120     }
121 
122     /**
123      * <p>Returns the string representation of a specific subarray of the char array argument.</p>
124      * @param x A char array.
125      * @param offset The initial offset into the string.
126      * @param count The length of the string.
127      * @return A string representing the sequence of characters contained in the subarray of the 
128      * character array argument.
129      * @since 1.0
130      * @javascript Re-compilers must report error on end-users directly using this method.
131      * As a "synchronized" static emulating method, its invocation should be re-compiled into in-lined code for efficiency.
132      */
133     public static java.lang.String valueOf(char x[], int offset, int count) {
134         int end = offset + count;
135         checkBounds(offset, end, x.length);
136         ArrayLike<?> a = Js.array(x).slice(offset, end);
137         return JsGlobal.String.fromCharCode.with().apply(null, a);
138     }
139 
140     /**
141      * <p>Returns the string representation of the char array argument.</p>
142      * @param x A char array.
143      * @return A newly allocated string representing the same sequence of characters contained in the 
144      * character array argument.
145      * @since 1.0
146      * @javascript Re-compilers must report error on end-users directly using this method.
147      * As a "synchronized" static emulating method, its invocation should be re-compiled into in-lined code for efficiency.
148      */
149     public static synchronized java.lang.String valueOf(char[] x) {
150         return JsGlobal.String.fromCharCode.with().apply(null, x);
151     }
152 
153     /**
154      * <p>Returns the string representation of the primitive argument.</p>
155      * @param x A primitive value.
156      * @return A string representation of the primitive argument.
157      * @since 1.0
158      * @javascript Re-compilers must report error on end-users directly using this method.
159      * As a "synchronized" static emulating method, its invocation should be re-compiled into in-lined code for efficiency.
160      */
161     public static synchronized java.lang.String valueOf(double x) {
162         return Js.add("", x);
163     }
164 
165     /**
166      * <p>Returns the string representation of the primitive argument.</p>
167      * @param x A primitive value.
168      * @return A string representation of the primitive argument.
169      * @since 1.0
170      * @javascript Re-compilers must report error on end-users directly using this method.
171      * As a "synchronized" static emulating method, its invocation should be re-compiled into in-lined code for efficiency.
172      */
173     public static synchronized java.lang.String valueOf(float x) {
174         return Js.add("", x);
175     }
176 
177     /**
178      * <p>Returns the string representation of the primitive argument.</p>
179      * @param x A primitive value.
180      * @return A string representation of the primitive argument.
181      * @since 1.0
182      * @javascript Re-compilers must report error on end-users directly using this method.
183      * As a "synchronized" static emulating method, its invocation should be re-compiled into in-lined code for efficiency.
184      */
185     public static synchronized java.lang.String valueOf(int x) {
186         return Js.add("", x);
187     }
188 
189     /**
190      * <p>Returns the string representation of the primitive argument.</p>
191      * @param x A primitive value.
192      * @return A string representation of the primitive argument.
193      * @since 1.0
194      * @javascript Re-compilers must report error on end-users directly using this method.
195      * As a "synchronized" static emulating method, its invocation should be re-compiled into in-lined code for efficiency.
196      */
197     public static synchronized java.lang.String valueOf(long x) {
198         return Js.add("", x);
199     }
200 
201     /**
202      * <p>Returns the string representation of the argument object.</p>
203      * @param x An object.
204      * @return A string representation of the argument.
205      * @since 1.0
206      * @javascript Re-compilers must report error on end-users directly using this method.
207      * As a "synchronized" static emulating method, its invocation should be re-compiled into in-lined code for efficiency.
208      */
209     public static synchronized java.lang.String valueOf(Object x) {
210         return Js.add("", x);
211     }
212 
213     /**
214      * <p>Returns the char value at the specified index.</p>
215      * @param index The index of the char value.
216      * @return The char value at the specified index of this string.
217      * @since 1.0
218      * @javascript Re-compilers must report error on end-users directly using this method.
219      */
220     public synchronized char charAt(int index) {
221         return StringLikes.charCodeAt(
222                 (StringLike)(java.lang.Object)this,
223                 index
224         ).charValue();
225     }
226 
227     /**
228      * <p>Compares two strings lexicographically.</p>
229      * @param s A string to be compared.
230      * @return 0 if this {@link String} is equal to the argument {@link String}; a value less than 0 
231      * if this {@link String} is lexicographically  less than the argument {@link String}; and a value greater 
232      * than 0 if this {@link String} is lexicographically  greater than the argument {@link String}.
233      * @since 1.0
234      * @javascript Re-compilers must report error on end-users directly using this method.
235      * For efficiency, the invocation of this instance emulation method with single statement can be in-lined in re-compilation.
236      */
237     public int compareTo(String s) {
238         return ((java.lang.Integer)Js.cond(
239                 Js.lt(this, s),
240                 -1, 
241                 Js.cond(
242                         Js.gt(this, s),
243                         1, 
244                         0
245                 )
246         )).intValue();
247     }
248 
249     /**
250      * <p>Compares two strings lexicographically, ignoring case differences.</p>
251      * @param s A string to be compared.
252      * @return  negative integer, zero, or a positive integer as the specified string is greater than, 
253      * equal to, or less than this String, ignoring case considerations.
254      * @since 1.0
255      * @javascript Re-compilers must report error on end-users directly using this method.
256      * For efficiency, the invocation of this instance emulation method with single statement can be in-lined in re-compilation.
257      */
258     public int compareToIgnoreCase(String s) {
259         return toUpperCase().compareTo(s.toUpperCase());
260     }
261 
262     /**
263      * <p>Concatenates the specified string to the end of this string.</p>
264      * @param s A string that is concatenated to the end of this string.
265      * @return A new string that represents the concatenation of the two.
266      * @since 1.0
267      * @javascript Re-compilers must report error on end-users directly using this method.
268      * For efficiency, the invocation of this instance emulation method with single statement can be in-lined in re-compilation.
269      */
270     public java.lang.String concat(java.lang.String s) {
271         return Js.add(this, s);
272     }
273 
274     /**
275      * <p>Tests if this string contains the specified sequence of char values.</p>
276      * @param q A sequence to search for.
277      * @return <tt>true</tt> if this string contains the specified char sequence; <tt>false</tt> otherwise.
278      * @since 1.0
279      * @javascript Re-compilers must report error on end-users directly using this method.
280      * For efficiency, the invocation of this instance emulation method with single statement can be in-lined in re-compilation.
281      */
282     public boolean contains(java.lang.CharSequence q) {
283         return matches(q.toString());
284     }
285 
286     /**
287      * <p>Tests if this string ends with the specified suffix.</p>
288      * @param suffix A suffix to be tested.
289      * @return <tt>true</tt> if the character sequence represented by the argument is a suffix of the 
290      * character sequence represented by this object; <tt>false</tt> otherwise. Note that the result will 
291      * be <tt>true</tt> if the argument is the empty string or is equal to this string as determined by 
292      * the {@link #equals(Object)} method.
293      * @since 1.0
294      * @javascript Re-compilers must report error on end-users directly using this method.
295      * For efficiency, the invocation of this instance emulation method with single statement can be in-lined in re-compilation.
296      */
297     public boolean endsWith(java.lang.String suffix) {
298         return Js.eq(
299                 StringLikes.lastIndexOf(
300                         (StringLike)(java.lang.Object)this,
301                         suffix
302                 ),
303                 length() - suffix.length()
304         );
305     }
306 
307     /**
308      * <p>Compares this string to the specified object.</p>
309      * @param o An object to compare this string against.
310      * @return <tt>true</tt> if the given object represents a string equivalent to this string; 
311      * <tt>false</tt> otherwise.
312      * @since 1.0
313      * @javascript Re-compilers must report error on end-users directly using this method.
314      * For efficiency, the invocation of this instance emulation method with single statement can be in-lined in re-compilation.
315      */
316     @Override
317     public boolean equals(java.lang.Object o) {
318         return Js.eq(this, o);
319     }
320 
321     /**
322      * <p>Compares the two strings, ignoring case considerations.</p>
323      * @param s Another string to compare this string against.
324      * @return <tt>true</tt> if the argument is not <tt>null</tt> and it represents an equivalent string ignoring case; 
325      * <tt>false</tt> otherwise.
326      * @since 1.0
327      * @javascript Re-compilers must report error on end-users directly using this method.
328      * For efficiency, the invocation of this instance emulation method with single statement can be in-lined in re-compilation.
329      */
330     public boolean equalsIgnoreCase(String s) {
331         return Js.eq(this, s) || Js.eq(toLowerCase(), s.toLowerCase());
332     }
333 
334     private static final ObjectLike hashCache = new Initializer().var();
335     private static int hashNext = 1;
336 
337     /**
338      * <p>Returns a hash code for this string.</p>
339      * @return A hash code value for this string.
340      * @since 1.0
341      * @javascript Re-compilers must report error on end-users directly using this method.
342      */
343     @Override
344     public int hashCode() {
345         java.lang.Integer hc = (java.lang.Integer)hashCache.var(toString());
346         if (Js.not(hc)) {
347             hc = hashCache.var(toString(), hashNext++);
348         }
349         return hc.intValue();
350     }
351 
352     /**
353      * <p>Returns the index within this string of the first occurrence of the specified character.</p>
354      * @param ch A character.
355      * @return The index of the first occurrence of the character in the character sequence.
356      * @since 1.0
357      * @javascript Re-compilers must report error on end-users directly using this method.
358      * For efficiency, the invocation of this instance emulation method with single statement can be in-lined in re-compilation.
359      */
360     public int indexOf(int ch) {
361         return StringLikes.indexOf(
362                 (StringLike)(java.lang.Object)this,
363                 StringLikes.fromCharCode(ch)
364         );
365     }
366 
367     /**
368      * <p>Returns the index within this string of the first occurrence of the specified character, 
369      * starting the search at the specified index.</p>
370      * @param ch A character.
371      * @param from The index to start the search from.
372      * @return The index of the first occurrence of the character in the character sequence.
373      * @since 1.0
374      * @javascript Re-compilers must report error on end-users directly using this method.
375      * For efficiency, the invocation of this instance emulation method with single statement can be in-lined in re-compilation.
376      */
377     public int indexOf(int ch, int from) {
378         return StringLikes.indexOf(
379                 (StringLike)(java.lang.Object)this,
380                 StringLikes.fromCharCode(ch),
381                 from
382         );
383     }
384 
385     /**
386      * <p>Returns the index within this string of the first occurrence of the specified string.</p>
387      * @param str A string.
388      * @return The index of the first occurrence of the string in the character sequence.
389      * @since 1.0
390      * @javascript Re-compilers must report error on end-users directly using this method.
391      * For efficiency, the invocation of this instance emulation method with single statement can be in-lined in re-compilation.
392      */
393     public int indexOf(String str) {
394         return StringLikes.indexOf(
395                 (StringLike)(java.lang.Object)this,
396                 str
397         );
398     }
399 
400     /**
401      * <p>Returns the index within this string of the first occurrence of the specified string, 
402      * starting the search at the specified index.</p>
403      * @param str A string.
404      * @param from The index to start the search from.
405      * @return The index of the first occurrence of the character in the character sequence.
406      * @since 1.0
407      * @javascript Re-compilers must report error on end-users directly using this method.
408      * For efficiency, the invocation of this instance emulation method with single statement can be in-lined in re-compilation.
409      */
410     public int indexOf(String str, int from) {
411         return StringLikes.indexOf(
412                 (StringLike)(java.lang.Object)this,
413                 str,
414                 from
415         );
416     }
417 
418     /**
419      * <p>Determines whether this is an empty string.</p>
420      * @return <tt>true</tt> if {@link #length()} is 0, otherwise <tt>false</tt>.
421      * @since 1.0
422      * @javascript Re-compilers must report error on end-users directly using this method.
423      * For efficiency, the invocation of this instance emulation method with single statement can be in-lined in re-compilation.
424      */
425     public boolean isEmpty() {
426         return Js.not(this);
427     }
428 
429     private final static ObjectLike pool = new Initializer().var();
430 
431     /**
432      * <p>Returns a canonical representation for the string object.</p>
433      * @return A string that has the same contents as this string, but is guaranteed to be from a 
434      * pool of unique strings.
435      * @since 1.0
436      * @javascript Re-compilers must report error on end-users directly using this method.
437      * For efficiency, the invocation of this instance emulation method with single statement can be in-lined in re-compilation.
438      */
439     public java.lang.String intern() {
440         java.lang.String s = (java.lang.String)pool.var(toString());
441         return Js.cond(
442                 Js.be(s), s, pool.var(toString(), toString())
443         );
444     }
445 
446     /**
447      * <p>Returns the index within this string of the last occurrence of the specified character.</p>
448      * @param ch A character.
449      * @return The index of the last occurrence of the character in the character sequence.
450      * @since 1.0
451      * @javascript Re-compilers must report error on end-users directly using this method.
452      * For efficiency, the invocation of this instance emulation method with single statement can be in-lined in re-compilation.
453      */
454     public int lastIndexOf(int ch) {
455         return StringLikes.lastIndexOf(
456                 (StringLike)(java.lang.Object)this,
457                 StringLikes.fromCharCode(ch)
458         );
459     }
460 
461     /**
462      * <p>Returns the index within this string of the last occurrence of the specified character, 
463      * starting the search at the specified index.</p>
464      * @param ch A character.
465      * @param from The index to start the search from.
466      * @return The index of the last occurrence of the character in the character sequence.
467      * @since 1.0
468      * @javascript Re-compilers must report error on end-users directly using this method.
469      * For efficiency, the invocation of this instance emulation method with single statement can be in-lined in re-compilation.
470      */
471     public int lastIndexOf(int ch, int from) {
472         return StringLikes.lastIndexOf(
473                 (StringLike)(java.lang.Object)this,
474                 StringLikes.fromCharCode(ch),
475                 from
476         );
477     }
478 
479     /**
480      * <p>Returns the index within this string of the last occurrence of the specified string.</p>
481      * @param str A string.
482      * @return The index of the last occurrence of the string in the character sequence.
483      * @since 1.0
484      * @javascript Re-compilers must report error on end-users directly using this method.
485      * For efficiency, the invocation of this instance emulation method with single statement can be in-lined in re-compilation.
486      */
487     public int lastIndexOf(String str) {
488         return StringLikes.lastIndexOf(
489                 (StringLike)(java.lang.Object)this,
490                 str
491         );
492     }
493 
494     /**
495      * <p>Returns the index within this string of the last occurrence of the specified string, 
496      * starting the search at the specified index.</p>
497      * @param str A string.
498      * @param from The index to start the search from.
499      * @return The index of the last occurrence of the character in the character sequence.
500      * @since 1.0
501      * @javascript Re-compilers must report error on end-users directly using this method.
502      * For efficiency, the invocation of this instance emulation method with single statement can be in-lined in re-compilation.
503      */
504     public int lastIndexOf(String str, int from) {
505         return StringLikes.lastIndexOf(
506                 (StringLike)(java.lang.Object)this,
507                 str,
508                 from
509         );
510     }
511 
512     /**
513      * <p>Returns the length of this string.</p>
514      * @return The length of this string.
515      * @since 1.0
516      * @javascript Re-compilers must report error on end-users directly using this method.
517      * For efficiency, the invocation of this instance emulation method with single statement can be in-lined in re-compilation.
518      */
519     public int length() {
520         return StringLikes.length((StringLike)(java.lang.Object)this);
521     }
522 
523     /**
524      * <p>Tells whether or not this string matches the given regular expression.</p>
525      * @return <tt>true</tt> iff this string matches the given regular expression.
526      * @since 1.0
527      * @javascript Re-compilers must report error on end-users directly using this method.
528      * For efficiency, the invocation of this instance emulation method with single statement can be in-lined in re-compilation.
529      */
530     public boolean matches(java.lang.String regex) {
531         return Js.re(regex).test(toString());
532     }
533 
534     /**
535      * <p>Returns a new string resulting from replacing all occurrences of <tt>oldChar</tt> in this 
536      * string with <tt>newChar</tt>.</p>
537      * @return A string derived from this string by replacing every occurrence of <tt>oldChar</tt> 
538      * with <tt>newChar</tt>.
539      * @since 1.0
540      * @javascript Re-compilers must report error on end-users directly using this method.
541      * For efficiency, the invocation of this instance emulation method with single statement can be in-lined in re-compilation.
542      */
543     public java.lang.String replace(char oldChar, char newChar) {
544         return replaceAll(
545                 StringLikes.fromCharCode(oldChar),
546                 StringLikes.fromCharCode(newChar)
547         );
548     }
549 
550     /*
551      * This method converts Java-escaped dollar signs "\$" into JavaScript-escaped
552      * dollar signs "$$", and removes all other lone backslashes, which serve as
553      * escapes in Java but are passed through literally in JavaScript.
554      * 
555      * @skip
556      */
557     static java.lang.String translateReplaceString(java.lang.String replaceStr) {
558         int pos = 0;
559         while (0 <= (pos = replaceStr.indexOf("\\", pos))) {
560             if (replaceStr.charAt(pos + 1) == '$') {
561                 replaceStr = Js.add(
562                         Js.add(
563                                 replaceStr.substring(0, pos),
564                                 "$"
565                         ),
566                         replaceStr.substring(++pos)
567                 );
568             } else {
569                 replaceStr = Js.add(
570                         replaceStr.substring(0, pos),
571                         replaceStr.substring(++pos)
572                 );
573             }
574         }
575         return replaceStr;
576     }
577 
578     /**
579      * <p>Replaces each substring of this string that matches the given regular expression with the 
580      * given replacement.</p>
581      * @param regex A regular expression to which this string is to be matched.
582      * @param replacement A string to be substituted for each match.
583      * @return The resulting string.
584      * @since 1.0
585      * @javascript Re-compilers must report error on end-users directly using this method.
586      * For efficiency, the invocation of this instance emulation method with single statement can be in-lined in re-compilation.
587      */
588     public java.lang.String replaceAll(java.lang.String regex, java.lang.String replacement) {
589         return StringLikes.replace(
590                 (StringLike)(java.lang.Object)this,
591                 Js.re(regex, "g"),
592                 replacement
593         );
594     }
595 
596     /**
597      * <p>Replaces the first substring of this string that matches the given regular expression with 
598      * the given replacement.</p>
599      * @param regex A regular expression to which this string is to be matched.
600      * @param replacement A string to be substituted for the first match.
601      * @return The resulting string.
602      * @since 1.0
603      * @javascript Re-compilers must report error on end-users directly using this method.
604      * For efficiency, the invocation of this instance emulation method with single statement can be in-lined in re-compilation.
605      */
606     public java.lang.String replaceFirst(java.lang.String regex, java.lang.String replacement) {
607         return StringLikes.replace(
608                 (StringLike)(java.lang.Object)this,
609                 Js.re(regex),
610                 replacement
611         );
612     }
613 
614     /**
615      * <p>Splits this string around matches of the given regular expression.</p>
616      * @param regex The delimiting  regular expression.
617      * @return An array of strings computed by splitting this string around matches of the given 
618      * regular expression.
619      * @since 1.0
620      * @javascript Re-compilers must report error on end-users directly using this method.
621      * For efficiency, the invocation of this instance emulation method with single statement can be in-lined in re-compilation.
622      */
623     public java.lang.String[] split(java.lang.String regex) {
624         return (java.lang.String[])(java.lang.Object)StringLikes.split(toString(), regex);
625     }
626 
627     /**
628      * <p>Splits this string around matches of the given regular expression.</p>
629      * @param regex The delimiting  regular expression.
630      * @param limit Controls the number of times the pattern is applied and therefore affects the 
631      * length of the resulting array.
632      * @return An array of strings computed by splitting this string around matches of the given 
633      * regular expression.
634      * @since 1.0
635      * @javascript Re-compilers must report error on end-users directly using this method.
636      * For efficiency, the invocation of this instance emulation method with single statement can be in-lined in re-compilation.
637      */
638     public java.lang.String[] split(java.lang.String regex, int limit) {
639         return (java.lang.String[])(java.lang.Object)StringLikes.split(toString(), regex, limit);
640     }
641 
642     /**
643      * <p>Tests if this string starts with the specified prefix.</p>
644      * @param prefix A prefix to be tested.
645      * @return <tt>true</tt> if the character sequence represented by the argument is a prefix of the 
646      * character sequence represented by this object; <tt>false</tt> otherwise. Note that the result will 
647      * be <tt>true</tt> if the argument is the empty string or is equal to this string as determined by 
648      * the {@link #equals(Object)} method.
649      * @since 1.0
650      * @javascript Re-compilers must report error on end-users directly using this method.
651      * For efficiency, the invocation of this instance emulation method with single statement can be in-lined in re-compilation.
652      */
653     public boolean startsWith(String prefix) {
654         return indexOf(prefix) == 0;
655     }
656 
657     /**
658      * <p>Tests if the substring of this string beginning at the specified index starts with the 
659      * specified prefix.</p>
660      * @param prefix A prefix to be tested.
661      * @param offset The offset index to begin looking in this string.
662      * @return <tt>true</tt> if the character sequence represented by the argument is a prefix of the 
663      * character sequence represented by this object starting at index offset; <tt>false</tt> otherwise.
664      * @since 1.0
665      * @javascript Re-compilers must report error on end-users directly using this method.
666      * For efficiency, the invocation of this instance emulation method with single statement can be in-lined in re-compilation.
667      */
668     public boolean startsWith(String prefix, int offset) {
669         return indexOf(prefix, offset) == offset;
670     }
671 
672     /**
673      * <p>Returns a new character sequence that is a subsequence of this sequence.</p>
674      * @param beginIndex The begin index, inclusive.
675      * @param endIndex The end index, exclusive.
676      * @return The specified subsequence.
677      * @since 1.0
678      * @javascript Re-compilers must report error on end-users directly using this method.
679      * For efficiency, the invocation of this instance emulation method with single statement can be in-lined in re-compilation.
680      */
681     public java.lang.CharSequence subSequence(int beginIndex, int endIndex) {
682         return substring(beginIndex, endIndex);
683     }
684 
685     /**
686      * <p>Returns a new string that is a substring of this string.</p>
687      * @param beginIndex The begin index, inclusive.
688      * @return The specified substring.
689      * @since 1.0
690      * @javascript Re-compilers must report error on end-users directly using this method.
691      * For efficiency, the invocation of this instance emulation method with single statement can be in-lined in re-compilation.
692      */
693     public synchronized java.lang.String substring(int beginIndex) {
694         return StringLikes.slice(
695                 (StringLike)(java.lang.Object)this,
696                 beginIndex
697         );
698     }
699 
700     /**
701      * <p>Returns a new string that is a substring of this string.</p>
702      * @param beginIndex The begin index, inclusive.
703      * @param endIndex The end index, exclusive.
704      * @return The specified substring.
705      * @since 1.0
706      * @javascript Re-compilers must report error on end-users directly using this method.
707      * For efficiency, the invocation of this instance emulation method with single statement can be in-lined in re-compilation.
708      */
709     public synchronized java.lang.String substring(int beginIndex, int endIndex) {
710         return StringLikes.slice(
711                 (StringLike)(java.lang.Object)this,
712                 beginIndex,
713                 endIndex
714         );
715     }
716 
717     /**
718      * <p>Converts this string to a new character array.</p>
719      * @return A newly allocated character array whose length is the length of this string and whose
720      * contents are initialized to contain the character sequence represented by this string.
721      * @since 1.0
722      * @javascript Re-compilers must report error on end-users directly using this method.
723      * For efficiency, the invocation of this instance emulation method with single statement can be in-lined in re-compilation.
724      */
725     public char[] toCharArray() {
726         int n = length();
727         char[] arr = new char[n];
728         for (int i = 0; i < n; ++i) {
729             arr[i] = charAt(i);
730         }
731         return arr;
732     }
733 
734     /**
735      * <p>Converts all of the characters in this string to lower case.</p>
736      * @return The string, converted to lower case.
737      * @since 1.0
738      * @javascript Re-compilers must report error on end-users directly using this method.
739      * For efficiency, the invocation of this instance emulation method with single statement can be in-lined in re-compilation.
740      */
741     public java.lang.String toLowerCase() {
742         return StringLikes.toLowerCase((StringLike)(java.lang.Object)this);
743     }
744 
745     /**
746      * <p>Returns this string itself.</p>
747      * @return This string itself.
748      * @since 1.0
749      * @javascript Re-compilers must report error on end-users directly using this method.
750      * A re-compiler simply replaces an invocation of this native method with the current {@link String}.
751      */
752     @Override
753     public native java.lang.String toString();
754 
755     /**
756      * <p>Converts all of the characters in this string to upper case.</p>
757      * @return The string, converted to upper case.
758      * @since 1.0
759      * @javascript Re-compilers must report error on end-users directly using this method.
760      * For efficiency, the invocation of this instance emulation method with single statement can be in-lined in re-compilation.
761      */
762     public java.lang.String toUpperCase() {
763         return StringLikes.toUpperCase((StringLike)(java.lang.Object)this);
764     }
765 
766     /**
767      * <p>Returns a copy of the string, with leading and trailing whitespace omitted.</p>
768      * @return A copy of this string with leading and trailing white space removed, or this string if 
769      * it has no leading or trailing white space.
770      * @since 1.0
771      * @javascript Re-compilers must report error on end-users directly using this method.
772      * For efficiency, the invocation of this instance emulation method with single statement can be in-lined in re-compilation.
773      */
774     public synchronized java.lang.String trim() {
775         return StringLikes.trim(this);
776     }
777 }