0001: /*
0002: *
0003: *
0004: * Copyright 1990-2007 Sun Microsystems, Inc. All Rights Reserved.
0005: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
0006: *
0007: * This program is free software; you can redistribute it and/or
0008: * modify it under the terms of the GNU General Public License version
0009: * 2 only, as published by the Free Software Foundation.
0010: *
0011: * This program is distributed in the hope that it will be useful, but
0012: * WITHOUT ANY WARRANTY; without even the implied warranty of
0013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
0014: * General Public License version 2 for more details (a copy is
0015: * included at /legal/license.txt).
0016: *
0017: * You should have received a copy of the GNU General Public License
0018: * version 2 along with this work; if not, write to the Free Software
0019: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
0020: * 02110-1301 USA
0021: *
0022: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
0023: * Clara, CA 95054 or visit www.sun.com if you need additional
0024: * information or have any questions.
0025: */
0026:
0027: package java.lang;
0028:
0029: import java.io.UnsupportedEncodingException;
0030: import com.sun.cldc.i18n.*;
0031: import com.sun.cldchi.jvm.JVM;
0032:
0033: /**
0034: * The <code>String</code> class represents character strings. All
0035: * string literals in Java programs, such as <code>"abc"</code>, are
0036: * implemented as instances of this class.
0037: * <p>
0038: * Strings are constant; their values cannot be changed after they
0039: * are created. String buffers support mutable strings.
0040: * Because String objects are immutable they can be shared. For example:
0041: * <p><blockquote><pre>
0042: * String str = "abc";
0043: * </pre></blockquote><p>
0044: * is equivalent to:
0045: * <p><blockquote><pre>
0046: * char data[] = {'a', 'b', 'c'};
0047: * String str = new String(data);
0048: * </pre></blockquote><p>
0049: * Here are some more examples of how strings can be used:
0050: * <p><blockquote><pre>
0051: * System.out.println("abc");
0052: * String cde = "cde";
0053: * System.out.println("abc" + cde);
0054: * String c = "abc".substring(2,3);
0055: * String d = cde.substring(1, 2);
0056: * </pre></blockquote>
0057: * <p>
0058: * The class <code>String</code> includes methods for examining
0059: * individual characters of the sequence, for comparing strings, for
0060: * searching strings, for extracting substrings, and for creating a
0061: * copy of a string with all characters translated to uppercase or to
0062: * lowercase.
0063: * <p>
0064: * The Java language provides special support for the string
0065: * concatenation operator ( + ), and for conversion of
0066: * other objects to strings. String concatenation is implemented
0067: * through the <code>StringBuffer</code> class and its
0068: * <code>append</code> method.
0069: * String conversions are implemented through the method
0070: * <code>toString</code>, defined by <code>Object</code> and
0071: * inherited by all classes in Java. For additional information on
0072: * string concatenation and conversion, see
0073: * <i>The Java Language Specification</i>.
0074: *
0075: * @version 12/17/01 (CLDC 1.1)
0076: * @see java.lang.Object#toString()
0077: * @see java.lang.StringBuffer
0078: * @see java.lang.StringBuffer#append(boolean)
0079: * @see java.lang.StringBuffer#append(char)
0080: * @see java.lang.StringBuffer#append(char[])
0081: * @see java.lang.StringBuffer#append(char[], int, int)
0082: * @see java.lang.StringBuffer#append(int)
0083: * @see java.lang.StringBuffer#append(long)
0084: * @see java.lang.StringBuffer#append(java.lang.Object)
0085: * @see java.lang.StringBuffer#append(java.lang.String)
0086: * @since JDK1.0, CLDC 1.0
0087: */
0088: public final class String {
0089:
0090: /** The value is used for character storage. */
0091: private char value[];
0092:
0093: /** The offset is the first index of the storage that is used. */
0094: private int offset;
0095:
0096: /** The count is the number of characters in the String. */
0097: private int count;
0098:
0099: /**
0100: * Initializes a newly created <code>String</code> object so that it
0101: * represents an empty character sequence.
0102: */
0103: public String() {
0104: value = new char[0];
0105: }
0106:
0107: /**
0108: * Initializes a newly created <code>String</code> object so that it
0109: * represents the same sequence of characters as the argument; in other
0110: * words, the newly created string is a copy of the argument string.
0111: *
0112: * @param value a <code>String</code>.
0113: */
0114: public String(String value) {
0115: count = value.length();
0116: this .value = new char[count];
0117: value.getChars(0, count, this .value, 0);
0118: }
0119:
0120: /**
0121: * Allocates a new <code>String</code> so that it represents the
0122: * sequence of characters currently contained in the character array
0123: * argument. The contents of the character array are copied; subsequent
0124: * modification of the character array does not affect the newly created
0125: * string.
0126: *
0127: * @param value the initial value of the string.
0128: * @throws NullPointerException if <code>value</code> is <code>null</code>.
0129: */
0130: public String(char value[]) {
0131: this .count = value.length;
0132: this .value = new char[count];
0133: JVM.unchecked_char_arraycopy(value, 0, this .value, 0, count);
0134: }
0135:
0136: /**
0137: * Allocates a new <code>String</code> that contains characters from
0138: * a subarray of the character array argument. The <code>offset</code>
0139: * argument is the index of the first character of the subarray and
0140: * the <code>count</code> argument specifies the length of the
0141: * subarray. The contents of the subarray are copied; subsequent
0142: * modification of the character array does not affect the newly
0143: * created string.
0144: *
0145: * @param value array that is the source of characters.
0146: * @param offset the initial offset.
0147: * @param count the length.
0148: * @exception IndexOutOfBoundsException if the <code>offset</code>
0149: * and <code>count</code> arguments index characters outside
0150: * the bounds of the <code>value</code> array.
0151: * @exception NullPointerException if <code>value</code> is
0152: * <code>null</code>.
0153: */
0154: public String(char value[], int offset, int count) {
0155: if (offset < 0) {
0156: throw new StringIndexOutOfBoundsException(
0157: /* #ifdef VERBOSE_EXCEPTIONS */
0158: /// skipped offset
0159: /* #endif */
0160: );
0161: }
0162: if (count < 0) {
0163: throw new StringIndexOutOfBoundsException(
0164: /* #ifdef VERBOSE_EXCEPTIONS */
0165: /// skipped count
0166: /* #endif */
0167: );
0168: }
0169: // Note: offset or count might be near -1>>>1.
0170: if (offset > value.length - count) {
0171: throw new StringIndexOutOfBoundsException(
0172: /* #ifdef VERBOSE_EXCEPTIONS */
0173: /// skipped offset + count
0174: /* #endif */
0175: );
0176: }
0177:
0178: this .value = new char[count];
0179: this .count = count;
0180: JVM.unchecked_char_arraycopy(value, offset, this .value, 0,
0181: count);
0182: }
0183:
0184: /**
0185: * Construct a new <code>String</code> by converting the specified
0186: * subarray of bytes using the specified character encoding. The length of
0187: * the new <code>String</code> is a function of the encoding, and hence may
0188: * not be equal to the length of the subarray.
0189: *
0190: * @param bytes The bytes to be converted into characters
0191: * @param off Index of the first byte to convert
0192: * @param len Number of bytes to convert
0193: * @param enc The name of a character encoding
0194: *
0195: * @exception UnsupportedEncodingException
0196: * If the named encoding is not supported
0197: * @since JDK1.1
0198: */
0199: public String(byte bytes[], int off, int len, String enc)
0200: throws UnsupportedEncodingException {
0201: this (Helper.byteToCharArray(bytes, off, len, enc));
0202: }
0203:
0204: /**
0205: * Construct a new <code>String</code> by converting the specified array
0206: * of bytes using the specified character encoding. The length of the new
0207: * <code>String</code> is a function of the encoding, and hence may not be
0208: * equal to the length of the byte array.
0209: *
0210: * @param bytes The bytes to be converted into characters
0211: * @param enc The name of a supported character encoding
0212: *
0213: * @exception UnsupportedEncodingException
0214: * If the named encoding is not supported
0215: * @since JDK1.1
0216: */
0217: public String(byte bytes[], String enc)
0218: throws UnsupportedEncodingException {
0219: this (bytes, 0, bytes.length, enc);
0220: }
0221:
0222: /**
0223: * Construct a new <code>String</code> by converting the specified
0224: * subarray of bytes using the platform's default character encoding. The
0225: * length of the new <code>String</code> is a function of the encoding, and
0226: * hence may not be equal to the length of the subarray.
0227: *
0228: * @param bytes The bytes to be converted into characters
0229: * @param off Index of the first byte to convert
0230: * @param len Number of bytes to convert
0231: * @since JDK1.1
0232: */
0233: public String(byte bytes[], int off, int len) {
0234: this (Helper.byteToCharArray(bytes, off, len));
0235: }
0236:
0237: /**
0238: * Construct a new <code>String</code> by converting the specified array
0239: * of bytes using the platform's default character encoding. The length of
0240: * the new <code>String</code> is a function of the encoding, and hence may
0241: * not be equal to the length of the byte array.
0242: *
0243: * @param bytes The bytes to be converted into characters
0244: * @since JDK1.1
0245: */
0246: public String(byte bytes[]) {
0247: this (bytes, 0, bytes.length);
0248: }
0249:
0250: /**
0251: * Allocates a new string that contains the sequence of characters
0252: * currently contained in the string buffer argument. The contents of
0253: * the string buffer are copied; subsequent modification of the string
0254: * buffer does not affect the newly created string.
0255: *
0256: * @param buffer a <code>StringBuffer</code>.
0257: * @throws NullPointerException If <code>buffer</code> is
0258: * <code>null</code>.
0259: */
0260: public String(StringBuffer buffer) {
0261: synchronized (buffer) {
0262: buffer.setShared();
0263: this .value = buffer.getValue();
0264: this .offset = 0;
0265: this .count = buffer.length();
0266: }
0267: }
0268:
0269: // Package private constructor which shares value array for speed.
0270: String(int offset, int count, char value[]) {
0271: this .value = value;
0272: this .offset = offset;
0273: this .count = count;
0274: }
0275:
0276: /**
0277: * Returns the length of this string.
0278: * The length is equal to the number of 16-bit
0279: * Unicode characters in the string.
0280: *
0281: * @return the length of the sequence of characters represented by this
0282: * object.
0283: */
0284: public int length() {
0285: return count;
0286: }
0287:
0288: /**
0289: * Returns the character at the specified index. An index ranges
0290: * from <code>0</code> to <code>length() - 1</code>. The first character
0291: * of the sequence is at index <code>0</code>, the next at index
0292: * <code>1</code>, and so on, as for array indexing.
0293: *
0294: * @param index the index of the character.
0295: * @return the character at the specified index of this string.
0296: * The first character is at index <code>0</code>.
0297: * @exception IndexOutOfBoundsException if the <code>index</code>
0298: * argument is negative or not less than the length of this
0299: * string.
0300: */
0301: public char charAt(int index) {
0302: if ((index < 0) || (index >= count)) {
0303: throw new StringIndexOutOfBoundsException(
0304: /* #ifdef VERBOSE_EXCEPTIONS */
0305: /// skipped index
0306: /* #endif */
0307: );
0308: }
0309: return value[index + offset];
0310: }
0311:
0312: /**
0313: * Copies characters from this string into the destination character
0314: * array.
0315: * <p>
0316: * The first character to be copied is at index <code>srcBegin</code>;
0317: * the last character to be copied is at index <code>srcEnd-1</code>
0318: * (thus the total number of characters to be copied is
0319: * <code>srcEnd-srcBegin</code>). The characters are copied into the
0320: * subarray of <code>dst</code> starting at index <code>dstBegin</code>
0321: * and ending at index:
0322: * <p><blockquote><pre>
0323: * dstbegin + (srcEnd-srcBegin) - 1
0324: * </pre></blockquote>
0325: *
0326: * @param srcBegin index of the first character in the string
0327: * to copy.
0328: * @param srcEnd index after the last character in the string
0329: * to copy.
0330: * @param dst the destination array.
0331: * @param dstBegin the start offset in the destination array.
0332: * @exception IndexOutOfBoundsException If any of the following
0333: * is true:
0334: * <ul><li><code>srcBegin</code> is negative.
0335: * <li><code>srcBegin</code> is greater than <code>srcEnd</code>
0336: * <li><code>srcEnd</code> is greater than the length of this
0337: * string
0338: * <li><code>dstBegin</code> is negative
0339: * <li><code>dstBegin+(srcEnd-srcBegin)</code> is larger than
0340: * <code>dst.length</code></ul>
0341: * @exception NullPointerException if <code>dst</code> is <code>null</code>
0342: */
0343: public void getChars(int srcBegin, int srcEnd, char dst[],
0344: int dstBegin) {
0345: if (srcBegin < 0) {
0346: throw new StringIndexOutOfBoundsException(
0347: /* #ifdef VERBOSE_EXCEPTIONS */
0348: /// skipped srcBegin
0349: /* #endif */
0350: );
0351: }
0352: if (srcEnd > count) {
0353: throw new StringIndexOutOfBoundsException(
0354: /* #ifdef VERBOSE_EXCEPTIONS */
0355: /// skipped srcEnd
0356: /* #endif */
0357: );
0358: }
0359: if (srcBegin > srcEnd) {
0360: throw new StringIndexOutOfBoundsException(
0361: /* #ifdef VERBOSE_EXCEPTIONS */
0362: /// skipped srcEnd - srcBegin
0363: /* #endif */
0364: );
0365: }
0366: // NOTE: dst not checked, cannot use unchecked arraycopy
0367: System.arraycopy(value, offset + srcBegin, dst, dstBegin,
0368: srcEnd - srcBegin);
0369: }
0370:
0371: /**
0372: * Convert this <code>String</code> into bytes according to the specified
0373: * character encoding, storing the result into a new byte array.
0374: *
0375: * @param enc A character-encoding name
0376: * @return The resultant byte array
0377: *
0378: * @exception UnsupportedEncodingException
0379: * If the named encoding is not supported
0380: * @since JDK1.1
0381: */
0382: public byte[] getBytes(String enc)
0383: throws UnsupportedEncodingException {
0384: return Helper.charToByteArray(value, offset, count, enc);
0385: }
0386:
0387: /**
0388: * Convert this <code>String</code> into bytes according to the platform's
0389: * default character encoding, storing the result into a new byte array.
0390: *
0391: * @return the resultant byte array.
0392: * @since JDK1.1
0393: */
0394: public byte[] getBytes() {
0395: return Helper.charToByteArray(value, offset, count);
0396: }
0397:
0398: /**
0399: * Compares this string to the specified object.
0400: * The result is <code>true</code> if and only if the argument is not
0401: * <code>null</code> and is a <code>String</code> object that represents
0402: * the same sequence of characters as this object.
0403: *
0404: * @param anObject the object to compare this <code>String</code>
0405: * against.
0406: * @return <code>true</code> if the <code>String </code>are equal;
0407: * <code>false</code> otherwise.
0408: * @see java.lang.String#compareTo(java.lang.String)
0409: * @see java.lang.String#equalsIgnoreCase(java.lang.String)
0410: */
0411: /*
0412: public native boolean equals(Object anObject);
0413: */
0414: public boolean equals(Object anObject) {
0415: if (this == anObject) {
0416: return true;
0417: }
0418: if (anObject instanceof String) {
0419: String anotherString = (String) anObject;
0420: int n = count;
0421: if (n == anotherString.count) {
0422: char v1[] = value;
0423: char v2[] = anotherString.value;
0424: int i = offset;
0425: int j = anotherString.offset;
0426: while (n-- != 0) {
0427: if (v1[i++] != v2[j++]) {
0428: return false;
0429: }
0430: }
0431: return true;
0432: }
0433: }
0434: return false;
0435: }
0436:
0437: /**
0438: * Compares this <code>String</code> to another <code>String</code>,
0439: * ignoring case considerations. Two strings are considered equal
0440: * ignoring case if they are of the same length, and corresponding
0441: * characters in the two strings are equal ignoring case.
0442: * <p>
0443: * Two characters <code>c1</code> and <code>c2</code> are considered
0444: * the same, ignoring case if at least one of the following is true:
0445: * <ul><li>The two characters are the same (as compared by the
0446: * <code>==</code> operator).
0447: * <li>Applying the method {@link java.lang.Character#toUpperCase(char)}
0448: * to each character produces the same result.
0449: * <li>Applying the method {@link java.lang.Character#toLowerCase(char)}
0450: * to each character produces the same result.</ul>
0451: *
0452: * @param anotherString the <code>String</code> to compare this
0453: * <code>String</code> against.
0454: * @return <code>true</code> if the argument is not <code>null</code>
0455: * and the <code>String</code>s are equal,
0456: * ignoring case; <code>false</code> otherwise.
0457: * @see #equals(Object)
0458: * @see java.lang.Character#toLowerCase(char)
0459: * @see java.lang.Character#toUpperCase(char)
0460: */
0461: public boolean equalsIgnoreCase(String anotherString) {
0462: return (anotherString != null)
0463: && (anotherString.count == count)
0464: && regionMatches(true, 0, anotherString, 0, count);
0465: }
0466:
0467: /**
0468: * Compares two strings lexicographically.
0469: * The comparison is based on the Unicode value of each character in
0470: * the strings. The character sequence represented by this
0471: * <code>String</code> object is compared lexicographically to the
0472: * character sequence represented by the argument string. The result is
0473: * a negative integer if this <code>String</code> object
0474: * lexicographically precedes the argument string. The result is a
0475: * positive integer if this <code>String</code> object lexicographically
0476: * follows the argument string. The result is zero if the strings
0477: * are equal; <code>compareTo</code> returns <code>0</code> exactly when
0478: * the {@link #equals(Object)} method would return <code>true</code>.
0479: * <p>
0480: * This is the definition of lexicographic ordering. If two strings are
0481: * different, then either they have different characters at some index
0482: * that is a valid index for both strings, or their lengths are different,
0483: * or both. If they have different characters at one or more index
0484: * positions, let <i>k</i> be the smallest such index; then the string
0485: * whose character at position <i>k</i> has the smaller value, as
0486: * determined by using the < operator, lexicographically precedes the
0487: * other string. In this case, <code>compareTo</code> returns the
0488: * difference of the two character values at position <i>k</i> in
0489: * the two string -- that is, the value:
0490: * <blockquote><pre>
0491: * this.charAt(k)-anotherString.charAt(k)
0492: * </pre></blockquote>
0493: * If there is no index position at which they differ, then the shorter
0494: * string lexicographically precedes the longer string. In this case,
0495: * <code>compareTo</code> returns the difference of the lengths of the
0496: * strings -- that is, the value:
0497: * <blockquote><pre>
0498: * this.length()-anotherString.length()
0499: * </pre></blockquote>
0500: *
0501: * @param anotherString the <code>String</code> to be compared.
0502: * @return the value <code>0</code> if the argument string is equal to
0503: * this string; a value less than <code>0</code> if this string
0504: * is lexicographically less than the string argument; and a
0505: * value greater than <code>0</code> if this string is
0506: * lexicographically greater than the string argument.
0507: * @exception java.lang.NullPointerException if <code>anotherString</code>
0508: * is <code>null</code>.
0509: */
0510: public int compareTo(String anotherString) {
0511: int len1 = count;
0512: int len2 = anotherString.count;
0513: int n = Math.min(len1, len2);
0514: char v1[] = value;
0515: char v2[] = anotherString.value;
0516: int i = offset;
0517: int j = anotherString.offset;
0518:
0519: if (i == j) {
0520: int k = i;
0521: int lim = n + i;
0522: while (k < lim) {
0523: char c1 = v1[k];
0524: char c2 = v2[k];
0525: if (c1 != c2) {
0526: return c1 - c2;
0527: }
0528: k++;
0529: }
0530: } else {
0531: while (n-- != 0) {
0532: char c1 = v1[i++];
0533: char c2 = v2[j++];
0534: if (c1 != c2) {
0535: return c1 - c2;
0536: }
0537: }
0538: }
0539: return len1 - len2;
0540: }
0541:
0542: /**
0543: * Tests if two string regions are equal.
0544: * <p>
0545: * A substring of this <tt>String</tt> object is compared to a substring
0546: * of the argument <tt>other</tt>. The result is <tt>true</tt> if these
0547: * substrings represent character sequences that are the same, ignoring
0548: * case if and only if <tt>ignoreCase</tt> is true. The substring of
0549: * this <tt>String</tt> object to be compared begins at index
0550: * <tt>toffset</tt> and has length <tt>len</tt>. The substring of
0551: * <tt>other</tt> to be compared begins at index <tt>ooffset</tt> and
0552: * has length <tt>len</tt>. The result is <tt>false</tt> if and only if
0553: * at least one of the following is true:
0554: * <ul><li><tt>toffset</tt> is negative.
0555: * <li><tt>ooffset</tt> is negative.
0556: * <li><tt>toffset+len</tt> is greater than the length of this
0557: * <tt>String</tt> object.
0558: * <li><tt>ooffset+len</tt> is greater than the length of the other
0559: * argument.
0560: * <li>There is some nonnegative integer <i>k</i> less than <tt>len</tt>
0561: * such that:
0562: * <blockquote><pre>
0563: * this.charAt(toffset+k) != other.charAt(ooffset+k)
0564: * </pre></blockquote>
0565: * <li><tt>ignoreCase</tt> is <tt>true</tt> and there is some nonnegative
0566: * integer <i>k</i> less than <tt>len</tt> such that:
0567: * <blockquote><pre>
0568: * Character.toLowerCase(this.charAt(toffset+k)) !=
0569: Character.toLowerCase(other.charAt(ooffset+k))
0570: * </pre></blockquote>
0571: * and:
0572: * <blockquote><pre>
0573: * Character.toUpperCase(this.charAt(toffset+k)) !=
0574: * Character.toUpperCase(other.charAt(ooffset+k))
0575: * </pre></blockquote>
0576: * </ul>
0577: *
0578: * @param ignoreCase if <code>true</code>, ignore case when comparing
0579: * characters.
0580: * @param toffset the starting offset of the subregion in this
0581: * string.
0582: * @param other the string argument.
0583: * @param ooffset the starting offset of the subregion in the string
0584: * argument.
0585: * @param len the number of characters to compare.
0586: * @return <code>true</code> if the specified subregion of this string
0587: * matches the specified subregion of the string argument;
0588: * <code>false</code> otherwise. Whether the matching is exact
0589: * or case insensitive depends on the <code>ignoreCase</code>
0590: * argument.
0591: */
0592: public boolean regionMatches(boolean ignoreCase, int toffset,
0593: String other, int ooffset, int len) {
0594: char ta[] = value;
0595: int to = offset + toffset;
0596: int tlim = offset + count;
0597: char pa[] = other.value;
0598: int po = other.offset + ooffset;
0599:
0600: // Note: toffset, ooffset, or len might be near -1>>>1.
0601: if ((ooffset < 0) || (toffset < 0)
0602: || (toffset > (long) count - len)
0603: || (ooffset > (long) other.count - len)) {
0604: return false;
0605: }
0606: while (len-- > 0) {
0607: char c1 = ta[to++];
0608: char c2 = pa[po++];
0609: if (c1 == c2)
0610: continue;
0611: if (ignoreCase) {
0612: // If characters don't match but case may be ignored,
0613: // try converting both characters to uppercase.
0614: // If the results match, then the comparison scan should
0615: // continue.
0616: char u1 = Character.toUpperCase(c1);
0617: char u2 = Character.toUpperCase(c2);
0618: if (u1 == u2)
0619: continue;
0620: // Unfortunately, conversion to uppercase does not work properly
0621: // for the Georgian alphabet, which has strange rules about case
0622: // conversion. So we need to make one last check before
0623: // exiting.
0624: if (Character.toLowerCase(u1) == Character
0625: .toLowerCase(u2))
0626: continue;
0627: }
0628: return false;
0629: }
0630: return true;
0631: }
0632:
0633: /**
0634: * Tests if this string starts with the specified prefix beginning
0635: * at the specified index.
0636: *
0637: * @param prefix the prefix.
0638: * @param toffset where to begin looking in the string.
0639: * @return <code>true</code> if the character sequence represented by the
0640: * argument is a prefix of the substring of this object starting
0641: * at index <code>toffset</code>; <code>false</code> otherwise.
0642: * The result is <code>false</code> if <code>toffset</code> is
0643: * negative or greater than the length of this
0644: * <code>String</code> object; otherwise the result is the same
0645: * as the result of the expression
0646: * <pre>
0647: * this.subString(toffset).startsWith(prefix)
0648: * </pre>
0649: * @exception java.lang.NullPointerException if <code>prefix</code> is
0650: * <code>null</code>.
0651: */
0652: public boolean startsWith(String prefix, int toffset) {
0653: char ta[] = value;
0654: int to = offset + toffset;
0655: int tlim = offset + count;
0656: char pa[] = prefix.value;
0657: int po = prefix.offset;
0658: int pc = prefix.count;
0659: // Note: toffset might be near -1>>>1.
0660: if ((toffset < 0) || (toffset > count - pc)) {
0661: return false;
0662: }
0663: while (--pc >= 0) {
0664: if (ta[to++] != pa[po++]) {
0665: return false;
0666: }
0667: }
0668: return true;
0669: }
0670:
0671: /**
0672: * Tests if this string starts with the specified prefix.
0673: *
0674: * @param prefix the prefix.
0675: * @return <code>true</code> if the character sequence represented by the
0676: * argument is a prefix of the character sequence represented by
0677: * this string; <code>false</code> otherwise.
0678: * Note also that <code>true</code> will be returned if the
0679: * argument is an empty string or is equal to this
0680: * <code>String</code> object as determined by the
0681: * {@link #equals(Object)} method.
0682: * @exception java.lang.NullPointerException if <code>prefix</code> is
0683: * <code>null</code>.
0684: * @since JDK1.0
0685: */
0686: public boolean startsWith(String prefix) {
0687: return startsWith(prefix, 0);
0688: }
0689:
0690: /**
0691: * Tests if this string ends with the specified suffix.
0692: *
0693: * @param suffix the suffix.
0694: * @return <code>true</code> if the character sequence represented by the
0695: * argument is a suffix of the character sequence represented by
0696: * this object; <code>false</code> otherwise. Note that the
0697: * result will be <code>true</code> if the argument is the
0698: * empty string or is equal to this <code>String</code> object
0699: * as determined by the {@link #equals(Object)} method.
0700: * @exception java.lang.NullPointerException if <code>suffix</code> is
0701: * <code>null</code>.
0702: */
0703: public boolean endsWith(String suffix) {
0704: return startsWith(suffix, count - suffix.count);
0705: }
0706:
0707: /**
0708: * Returns a hashcode for this string. The hashcode for a
0709: * <code>String</code> object is computed as
0710: * <blockquote><pre>
0711: * s[0]*31^(n-1) + s[1]*31^(n-2) + ... + s[n-1]
0712: * </pre></blockquote>
0713: * using <code>int</code> arithmetic, where <code>s[i]</code> is the
0714: * <i>i</i>th character of the string, <code>n</code> is the length of
0715: * the string, and <code>^</code> indicates exponentiation.
0716: * (The hash value of the empty string is zero.)
0717: *
0718: * @return a hash code value for this object.
0719: */
0720: public native int hashCode();
0721:
0722: /*
0723: public int hashCode() {
0724: int h = 0;
0725: int off = offset;
0726: char val[] = value;
0727: int len = count;
0728:
0729: for (int i = 0; i < len; i++) {
0730: h = 31*h + val[off++];
0731: }
0732: return h;
0733: }
0734: */
0735:
0736: /**
0737: * Returns the index within this string of the first occurrence of the
0738: * specified character. If a character with value <code>ch</code> occurs
0739: * in the character sequence represented by this <code>String</code>
0740: * object, then the index of the first such occurrence is returned --
0741: * that is, the smallest value <i>k</i> such that:
0742: * <blockquote><pre>
0743: * this.charAt(<i>k</i>) == ch
0744: * </pre></blockquote>
0745: * is <code>true</code>. If no such character occurs in this string,
0746: * then <code>-1</code> is returned.
0747: *
0748: * @param ch a character.
0749: * @return the index of the first occurrence of the character in the
0750: * character sequence represented by this object, or
0751: * <code>-1</code> if the character does not occur.
0752: */
0753: public native int indexOf(int ch);
0754:
0755: /*
0756: public int indexOf(int ch) {
0757: return indexOf(ch, 0);
0758: }
0759: */
0760:
0761: /**
0762: * Returns the index within this string of the first occurrence of the
0763: * specified character, starting the search at the specified index.
0764: * <p>
0765: * If a character with value <code>ch</code> occurs in the character
0766: * sequence represented by this <code>String</code> object at an index
0767: * no smaller than <code>fromIndex</code>, then the index of the first
0768: * such occurrence is returned--that is, the smallest value <i>k</i>
0769: * such that:
0770: * <blockquote><pre>
0771: * (this.charAt(<i>k</i>) == ch) && (<i>k</i> >= fromIndex)
0772: * </pre></blockquote>
0773: * is true. If no such character occurs in this string at or after
0774: * position <code>fromIndex</code>, then <code>-1</code> is returned.
0775: * <p>
0776: * There is no restriction on the value of <code>fromIndex</code>. If it
0777: * is negative, it has the same effect as if it were zero: this entire
0778: * string may be searched. If it is greater than the length of this
0779: * string, it has the same effect as if it were equal to the length of
0780: * this string: <code>-1</code> is returned.
0781: *
0782: * @param ch a character.
0783: * @param fromIndex the index to start the search from.
0784: * @return the index of the first occurrence of the character in the
0785: * character sequence represented by this object that is greater
0786: * than or equal to <code>fromIndex</code>, or <code>-1</code>
0787: * if the character does not occur.
0788: */
0789: public native int indexOf(int ch, int fromIndex);
0790:
0791: /************
0792: * public int indexOf(int ch, int fromIndex) {
0793: * int max = offset + count;
0794: * char v[] = value;
0795: *
0796: * if (fromIndex < 0) {
0797: * fromIndex = 0;
0798: * } else if (fromIndex >= count) {
0799: * // Note: fromIndex might be near -1>>>1.
0800: * return -1;
0801: * }
0802: * for (int i = offset + fromIndex ; i < max ; i++) {
0803: * if (v[i] == ch) {
0804: * return i - offset;
0805: * }
0806: * }
0807: * return -1;
0808: * }
0809: ******/
0810:
0811: /**
0812: * Returns the index within this string of the last occurrence of the
0813: * specified character. That is, the index returned is the largest
0814: * value <i>k</i> such that:
0815: * <blockquote><pre>
0816: * this.charAt(<i>k</i>) == ch
0817: * </pre></blockquote>
0818: * is true.
0819: * The String is searched backwards starting at the last character.
0820: *
0821: * @param ch a character.
0822: * @return the index of the last occurrence of the character in the
0823: * character sequence represented by this object, or
0824: * <code>-1</code> if the character does not occur.
0825: */
0826: public native int lastIndexOf(int ch);
0827:
0828: /*
0829: public int lastIndexOf(int ch) {
0830: return lastIndexOf(ch, count - 1);
0831: }
0832: */
0833:
0834: /**
0835: * Returns the index within this string of the last occurrence of the
0836: * specified character, searching backward starting at the specified
0837: * index. That is, the index returned is the largest value <i>k</i>
0838: * such that:
0839: * <blockquote><pre>
0840: * (this.charAt(k) == ch) && (k <= fromIndex)
0841: * </pre></blockquote>
0842: * is true.
0843: *
0844: * @param ch a character.
0845: * @param fromIndex the index to start the search from. There is no
0846: * restriction on the value of <code>fromIndex</code>. If it is
0847: * greater than or equal to the length of this string, it has
0848: * the same effect as if it were equal to one less than the
0849: * length of this string: this entire string may be searched.
0850: * If it is negative, it has the same effect as if it were -1:
0851: * -1 is returned.
0852: * @return the index of the last occurrence of the character in the
0853: * character sequence represented by this object that is less
0854: * than or equal to <code>fromIndex</code>, or <code>-1</code>
0855: * if the character does not occur before that point.
0856: */
0857: public native int lastIndexOf(int ch, int fromIndex);
0858:
0859: /*
0860: public int lastIndexOf(int ch, int fromIndex) {
0861: int min = offset;
0862: char v[] = value;
0863:
0864: for (int i = offset + ((fromIndex >= count) ? count - 1 : fromIndex) ; i >= min ; i--) {
0865: if (v[i] == ch) {
0866: return i - offset;
0867: }
0868: }
0869: return -1;
0870: }
0871: */
0872:
0873: /**
0874: * Returns the index within this string of the first occurrence of the
0875: * specified substring. The integer returned is the smallest value
0876: * <i>k</i> such that:
0877: * <blockquote><pre>
0878: * this.startsWith(str, <i>k</i>)
0879: * </pre></blockquote>
0880: * is <code>true</code>.
0881: *
0882: * @param str any string.
0883: * @return if the string argument occurs as a substring within this
0884: * object, then the index of the first character of the first
0885: * such substring is returned; if it does not occur as a
0886: * substring, <code>-1</code> is returned.
0887: * @exception java.lang.NullPointerException if <code>str</code> is
0888: * <code>null</code>.
0889: */
0890: public int indexOf(String str) {
0891: return indexOf(str, 0);
0892: }
0893:
0894: /**
0895: * Returns the index within this string of the first occurrence of the
0896: * specified substring, starting at the specified index. The integer
0897: * returned is the smallest value <i>k</i> such that:
0898: * <blockquote><pre>
0899: * this.startsWith(str, <i>k</i>) && (<i>k</i> >= fromIndex)
0900: * </pre></blockquote>
0901: * is <code>true</code>.
0902: * <p>
0903: * There is no restriction on the value of <code>fromIndex</code>. If
0904: * it is negative, it has the same effect as if it were zero: this entire
0905: * string may be searched. If it is greater than the length of this
0906: * string, it has the same effect as if it were equal to the length of
0907: * this string: <code>-1</code> is returned.
0908: *
0909: * @param str the substring to search for.
0910: * @param fromIndex the index to start the search from.
0911: * @return If the string argument occurs as a substring within this
0912: * object at a starting index no smaller than
0913: * <code>fromIndex</code>, then the index of the first character
0914: * of the first such substring is returned. If it does not occur
0915: * as a substring starting at <code>fromIndex</code> or beyond,
0916: * <code>-1</code> is returned.
0917: * @exception java.lang.NullPointerException if <code>str</code> is
0918: * <code>null</code>
0919: */
0920: public int indexOf(String str, int fromIndex) {
0921: char v1[] = value;
0922: char v2[] = str.value;
0923: int max = offset + (count - str.count);
0924: if (fromIndex >= count) {
0925: if (count == 0 && fromIndex == 0 && str.count == 0) {
0926: /* There is an empty string at index 0 in an empty string. */
0927: return 0;
0928: }
0929: /* Note: fromIndex might be near -1>>>1 */
0930: return -1;
0931: }
0932: if (fromIndex < 0) {
0933: fromIndex = 0;
0934: }
0935: if (str.count == 0) {
0936: return fromIndex;
0937: }
0938:
0939: int strOffset = str.offset;
0940: char first = v2[strOffset];
0941: int i = offset + fromIndex;
0942:
0943: startSearchForFirstChar: while (true) {
0944:
0945: /* Look for first character. */
0946: while (i <= max && v1[i] != first) {
0947: i++;
0948: }
0949: if (i > max) {
0950: return -1;
0951: }
0952:
0953: /* Found first character, now look at the rest of v2 */
0954: int j = i + 1;
0955: int end = j + str.count - 1;
0956: int k = strOffset + 1;
0957: while (j < end) {
0958: if (v1[j++] != v2[k++]) {
0959: i++;
0960: /* Look for str's first char again. */
0961: continue startSearchForFirstChar;
0962: }
0963: }
0964: return i - offset; /* Found whole string. */
0965: }
0966: }
0967:
0968: /**
0969: * Returns a new string that is a substring of this string. The
0970: * substring begins with the character at the specified index and
0971: * extends to the end of this string. <p>
0972: * Examples:
0973: * <blockquote><pre>
0974: * "unhappy".substring(2) returns "happy"
0975: * "Harbison".substring(3) returns "bison"
0976: * "emptiness".substring(9) returns "" (an empty string)
0977: * </pre></blockquote>
0978: *
0979: * @param beginIndex the beginning index, inclusive.
0980: * @return the specified substring.
0981: * @exception IndexOutOfBoundsException if
0982: * <code>beginIndex</code> is negative or larger than the
0983: * length of this <code>String</code> object.
0984: */
0985: public String substring(int beginIndex) {
0986: return substring(beginIndex, count);
0987: }
0988:
0989: /**
0990: * Returns a new string that is a substring of this string. The
0991: * substring begins at the specified <code>beginIndex</code> and
0992: * extends to the character at index <code>endIndex - 1</code>.
0993: * Thus the length of the substring is <code>endIndex-beginIndex</code>.
0994: * <p>
0995: * Examples:
0996: * <blockquote><pre>
0997: * "hamburger".substring(4, 8) returns "urge"
0998: * "smiles".substring(1, 5) returns "mile"
0999: * </pre></blockquote>
1000: *
1001: * @param beginIndex the beginning index, inclusive.
1002: * @param endIndex the ending index, exclusive.
1003: * @return the specified substring.
1004: * @exception IndexOutOfBoundsException if the
1005: * <code>beginIndex</code> is negative, or
1006: * <code>endIndex</code> is larger than the length of
1007: * this <code>String</code> object, or
1008: * <code>beginIndex</code> is larger than
1009: * <code>endIndex</code>.
1010: */
1011: public String substring(int beginIndex, int endIndex) {
1012: if (beginIndex < 0) {
1013: throw new StringIndexOutOfBoundsException(
1014: /* #ifdef VERBOSE_EXCEPTIONS */
1015: /// skipped beginIndex
1016: /* #endif */
1017: );
1018: }
1019: if (endIndex > count) {
1020: throw new StringIndexOutOfBoundsException(
1021: /* #ifdef VERBOSE_EXCEPTIONS */
1022: /// skipped endIndex
1023: /* #endif */
1024: );
1025: }
1026: if (beginIndex > endIndex) {
1027: throw new StringIndexOutOfBoundsException(
1028: /* #ifdef VERBOSE_EXCEPTIONS */
1029: /// skipped endIndex - beginIndex
1030: /* #endif */
1031: );
1032: }
1033: return ((beginIndex == 0) && (endIndex == count)) ? this
1034: : new String(offset + beginIndex,
1035: endIndex - beginIndex, value);
1036: }
1037:
1038: /**
1039: * Concatenates the specified string to the end of this string.
1040: * <p>
1041: * If the length of the argument string is <code>0</code>, then this
1042: * <code>String</code> object is returned. Otherwise, a new
1043: * <code>String</code> object is created, representing a character
1044: * sequence that is the concatenation of the character sequence
1045: * represented by this <code>String</code> object and the character
1046: * sequence represented by the argument string.<p>
1047: * Examples:
1048: * <blockquote><pre>
1049: * "cares".concat("s") returns "caress"
1050: * "to".concat("get").concat("her") returns "together"
1051: * </pre></blockquote>
1052: *
1053: * @param str the <code>String</code> that is concatenated to the end
1054: * of this <code>String</code>.
1055: * @return a string that represents the concatenation of this object's
1056: * characters followed by the string argument's characters.
1057: * @exception java.lang.NullPointerException if <code>str</code> is
1058: * <code>null</code>.
1059: */
1060: public String concat(String str) {
1061: int otherLen = str.length();
1062: if (otherLen == 0) {
1063: return this ;
1064: }
1065: char buf[] = new char[count + otherLen];
1066: getChars(0, count, buf, 0);
1067: str.getChars(0, otherLen, buf, count);
1068: return new String(0, count + otherLen, buf);
1069: }
1070:
1071: /**
1072: * Returns a new string resulting from replacing all occurrences of
1073: * <code>oldChar</code> in this string with <code>newChar</code>.
1074: * <p>
1075: * If the character <code>oldChar</code> does not occur in the
1076: * character sequence represented by this <code>String</code> object,
1077: * then a reference to this <code>String</code> object is returned.
1078: * Otherwise, a new <code>String</code> object is created that
1079: * represents a character sequence identical to the character sequence
1080: * represented by this <code>String</code> object, except that every
1081: * occurrence of <code>oldChar</code> is replaced by an occurrence
1082: * of <code>newChar</code>.
1083: * <p>
1084: * Examples:
1085: * <blockquote><pre>
1086: * "mesquite in your cellar".replace('e', 'o')
1087: * returns "mosquito in your collar"
1088: * "the war of baronets".replace('r', 'y')
1089: * returns "the way of bayonets"
1090: * "sparring with a purple porpoise".replace('p', 't')
1091: * returns "starring with a turtle tortoise"
1092: * "JonL".replace('q', 'x') returns "JonL" (no change)
1093: * </pre></blockquote>
1094: *
1095: * @param oldChar the old character.
1096: * @param newChar the new character.
1097: * @return a string derived from this string by replacing every
1098: * occurrence of <code>oldChar</code> with <code>newChar</code>.
1099: */
1100: public String replace(char oldChar, char newChar) {
1101: if (oldChar != newChar) {
1102: int len = count;
1103: int i = -1;
1104: char[] val = value; /* avoid getfield opcode */
1105: int off = offset; /* avoid getfield opcode */
1106:
1107: while (++i < len) {
1108: if (val[off + i] == oldChar) {
1109: break;
1110: }
1111: }
1112: if (i < len) {
1113: char buf[] = new char[len];
1114: for (int j = 0; j < i; j++) {
1115: buf[j] = val[off + j];
1116: }
1117: while (i < len) {
1118: char c = val[off + i];
1119: buf[i] = (c == oldChar) ? newChar : c;
1120: i++;
1121: }
1122: return new String(0, len, buf);
1123: }
1124: }
1125: return this ;
1126: }
1127:
1128: /**
1129: * Converts all of the characters in this <code>String</code> to lower case.
1130: *
1131: * @return the String, converted to lowercase.
1132: * @see Character#toLowerCase
1133: * @see String#toUpperCase
1134: */
1135: public String toLowerCase() {
1136: int i;
1137:
1138: scan: {
1139: for (i = 0; i < count; i++) {
1140: char c = value[offset + i];
1141: if (c != Character.toLowerCase(c)) {
1142: break scan;
1143: }
1144: }
1145: return this ;
1146: }
1147:
1148: char buf[] = new char[count];
1149:
1150: JVM.unchecked_char_arraycopy(value, offset, buf, 0, i);
1151:
1152: for (; i < count; i++) {
1153: buf[i] = Character.toLowerCase(value[offset + i]);
1154: }
1155: return new String(0, count, buf);
1156: }
1157:
1158: /**
1159: * Converts all of the characters in this <code>String</code> to upper case.
1160: *
1161: * @return the String, converted to uppercase.
1162: * @see Character#toLowerCase
1163: * @see String#toUpperCase
1164: */
1165: public String toUpperCase() {
1166: int i;
1167:
1168: scan: {
1169: for (i = 0; i < count; i++) {
1170: char c = value[offset + i];
1171: if (c != Character.toUpperCase(c)) {
1172: break scan;
1173: }
1174: }
1175: return this ;
1176: }
1177:
1178: char buf[] = new char[count];
1179:
1180: JVM.unchecked_char_arraycopy(value, offset, buf, 0, i);
1181:
1182: for (; i < count; i++) {
1183: buf[i] = Character.toUpperCase(value[offset + i]);
1184: }
1185: return new String(0, count, buf);
1186: }
1187:
1188: /**
1189: * Removes white space from both ends of this string.
1190: * <p>
1191: * If this <code>String</code> object represents an empty character
1192: * sequence, or the first and last characters of character sequence
1193: * represented by this <code>String</code> object both have codes
1194: * greater than <code>'\u0020'</code> (the space character), then a
1195: * reference to this <code>String</code> object is returned.
1196: * <p>
1197: * Otherwise, if there is no character with a code greater than
1198: * <code>'\u0020'</code> in the string, then a new
1199: * <code>String</code> object representing an empty string is created
1200: * and returned.
1201: * <p>
1202: * Otherwise, let <i>k</i> be the index of the first character in the
1203: * string whose code is greater than <code>'\u0020'</code>, and let
1204: * <i>m</i> be the index of the last character in the string whose code
1205: * is greater than <code>'\u0020'</code>. A new <code>String</code>
1206: * object is created, representing the substring of this string that
1207: * begins with the character at index <i>k</i> and ends with the
1208: * character at index <i>m</i>-that is, the result of
1209: * <code>this.substring(<i>k</i>, <i>m</i>+1)</code>.
1210: * <p>
1211: * This method may be used to trim whitespace from the beginning and end
1212: * of a string; in fact, it trims all ASCII control characters as well.
1213: *
1214: * @return this string, with white space removed from the front and end.
1215: */
1216: public String trim() {
1217: int len = count;
1218: int st = 0;
1219: int off = offset; /* avoid getfield opcode */
1220: char[] val = value; /* avoid getfield opcode */
1221:
1222: while ((st < len) && (val[off + st] <= ' ')) {
1223: st++;
1224: }
1225: while ((st < len) && (val[off + len - 1] <= ' ')) {
1226: len--;
1227: }
1228: return ((st > 0) || (len < count)) ? substring(st, len) : this ;
1229: }
1230:
1231: /**
1232: * This object (which is already a string!) is itself returned.
1233: *
1234: * @return the string itself.
1235: */
1236: public String toString() {
1237: return this ;
1238: }
1239:
1240: /**
1241: * Converts this string to a new character array.
1242: *
1243: * @return a newly allocated character array whose length is the length
1244: * of this string and whose contents are initialized to contain
1245: * the character sequence represented by this string.
1246: */
1247: public char[] toCharArray() {
1248: char result[] = new char[count];
1249: getChars(0, count, result, 0);
1250: return result;
1251: }
1252:
1253: /**
1254: * Returns the string representation of the <code>Object</code> argument.
1255: *
1256: * @param obj an <code>Object</code>.
1257: * @return if the argument is <code>null</code>, then a string equal to
1258: * <code>"null"</code>; otherwise, the value of
1259: * <code>obj.toString()</code> is returned.
1260: * @see java.lang.Object#toString()
1261: */
1262: public static String valueOf(Object obj) {
1263: return (obj == null) ? "null" : obj.toString();
1264: }
1265:
1266: /**
1267: * Returns the string representation of the <code>char</code> array
1268: * argument. The contents of the character array are copied; subsequent
1269: * modification of the character array does not affect the newly
1270: * created string.
1271: *
1272: * @param data a <code>char</code> array.
1273: * @return a newly allocated string representing the same sequence of
1274: * characters contained in the character array argument.
1275: */
1276: public static String valueOf(char data[]) {
1277: return new String(data);
1278: }
1279:
1280: /**
1281: * Returns the string representation of a specific subarray of the
1282: * <code>char</code> array argument.
1283: * <p>
1284: * The <code>offset</code> argument is the index of the first
1285: * character of the subarray. The <code>count</code> argument
1286: * specifies the length of the subarray. The contents of the subarray
1287: * are copied; subsequent modification of the character array does not
1288: * affect the newly created string.
1289: *
1290: * @param data the character array.
1291: * @param offset the initial offset into the value of the
1292: * <code>String</code>.
1293: * @param count the length of the value of the <code>String</code>.
1294: * @return a newly allocated string representing the sequence of
1295: * characters contained in the subarray of the character array
1296: * argument.
1297: * @exception NullPointerException if <code>data</code> is
1298: * <code>null</code>.
1299: * @exception IndexOutOfBoundsException if <code>offset</code> is
1300: * negative, or <code>count</code> is negative, or
1301: * <code>offset+count</code> is larger than
1302: * <code>data.length</code>.
1303: */
1304: public static String valueOf(char data[], int offset, int count) {
1305: return new String(data, offset, count);
1306: }
1307:
1308: /**
1309: * Returns the string representation of the <code>boolean</code> argument.
1310: *
1311: * @param b a <code>boolean</code>.
1312: * @return if the argument is <code>true</code>, a string equal to
1313: * <code>"true"</code> is returned; otherwise, a string equal to
1314: * <code>"false"</code> is returned.
1315: */
1316: public static String valueOf(boolean b) {
1317: return b ? "true" : "false";
1318: }
1319:
1320: /**
1321: * Returns the string representation of the <code>char</code>
1322: * argument.
1323: *
1324: * @param c a <code>char</code>.
1325: * @return a newly allocated string of length <code>1</code> containing
1326: * as its single character the argument <code>c</code>.
1327: */
1328: public static String valueOf(char c) {
1329: char data[] = { c };
1330: return new String(0, 1, data);
1331: }
1332:
1333: /**
1334: * Returns the string representation of the <code>int</code> argument.
1335: * <p>
1336: * The representation is exactly the one returned by the
1337: * <code>Integer.toString</code> method of one argument.
1338: *
1339: * @param i an <code>int</code>.
1340: * @return a newly allocated string containing a string representation of
1341: * the <code>int</code> argument.
1342: * @see java.lang.Integer#toString(int, int)
1343: */
1344: public static String valueOf(int i) {
1345: return Integer.toString(i, 10);
1346: }
1347:
1348: /**
1349: * Returns the string representation of the <code>long</code> argument.
1350: * <p>
1351: * The representation is exactly the one returned by the
1352: * <code>Long.toString</code> method of one argument.
1353: *
1354: * @param l a <code>long</code>.
1355: * @return a newly allocated string containing a string representation of
1356: * the <code>long</code> argument.
1357: * @see java.lang.Long#toString(long)
1358: */
1359: public static String valueOf(long l) {
1360: return Long.toString(l, 10);
1361: }
1362:
1363: /**
1364: * Returns the string representation of the <code>float</code> argument.
1365: * <p>
1366: * The representation is exactly the one returned by the
1367: * <code>Float.toString</code> method of one argument.
1368: *
1369: * @param f a <code>float</code>.
1370: * @return a newly allocated string containing a string representation of
1371: * the <code>float</code> argument.
1372: * @see java.lang.Float#toString(float)
1373: * @since CLDC 1.1
1374: */
1375: public static String valueOf(float f) {
1376: return Float.toString(f);
1377: }
1378:
1379: /**
1380: * Returns the string representation of the <code>double</code> argument.
1381: * <p>
1382: * The representation is exactly the one returned by the
1383: * <code>Double.toString</code> method of one argument.
1384: *
1385: * @param d a <code>double</code>.
1386: * @return a newly allocated string containing a string representation of
1387: * the <code>double</code> argument.
1388: * @see java.lang.Double#toString(double)
1389: * @since CLDC 1.1
1390: */
1391: public static String valueOf(double d) {
1392: return Double.toString(d);
1393: }
1394:
1395: /**
1396: * Returns a canonical representation for the string object.
1397: * <p>
1398: * A pool of strings, initially empty, is maintained privately by the
1399: * class <code>String</code>.
1400: * <p>
1401: * When the intern method is invoked, if the pool already contains a
1402: * string equal to this <code>String</code> object as determined by
1403: * the {@link #equals(Object)} method, then the string from the pool is
1404: * returned. Otherwise, this <code>String</code> object is added to the
1405: * pool and a reference to this <code>String</code> object is returned.
1406: * <p>
1407: * It follows that for any two strings <code>s</code> and <code>t</code>,
1408: * <code>s.intern() == t.intern()</code> is <code>true</code>
1409: * if and only if <code>s.equals(t)</code> is <code>true</code>.
1410: * <p>
1411: * All literal strings and string-valued constant expressions are
1412: * interned. String literals are defined in Section 3.10.5 of the
1413: * <a href="http://java.sun.com/docs/books/jls/html/">Java Language
1414: * Specification</a>
1415: *
1416: * @return a string that has the same contents as this string, but is
1417: * guaranteed to be from a pool of unique strings.
1418: * @since CLDC 1.1
1419: */
1420: public native String intern();
1421: }
|