📄 string.java
字号:
* </pre></blockquote> * * @param str the <code>String</code> that is concatenated to the end * of this <code>String</code>. * @return a string that represents the concatenation of this object's * characters followed by the string argument's characters. * @exception java.lang.NullPointerException if <code>str</code> is * <code>null</code>. */ public String concat(String str) { int otherLen = str.length(); if (otherLen == 0) { return this; } char buf[] = new char[count + otherLen]; getChars(0, count, buf, 0); str.getChars(0, otherLen, buf, count); return new String(0, count + otherLen, buf); } /** * Returns a new string resulting from replacing all occurrences of * <code>oldChar</code> in this string with <code>newChar</code>. * <p> * If the character <code>oldChar</code> does not occur in the * character sequence represented by this <code>String</code> object, * then a reference to this <code>String</code> object is returned. * Otherwise, a new <code>String</code> object is created that * represents a character sequence identical to the character sequence * represented by this <code>String</code> object, except that every * occurrence of <code>oldChar</code> is replaced by an occurrence * of <code>newChar</code>. * <p> * Examples: * <blockquote><pre> * "mesquite in your cellar".replace('e', 'o') * returns "mosquito in your collar" * "the war of baronets".replace('r', 'y') * returns "the way of bayonets" * "sparring with a purple porpoise".replace('p', 't') * returns "starring with a turtle tortoise" * "JonL".replace('q', 'x') returns "JonL" (no change) * </pre></blockquote> * * @param oldChar the old character. * @param newChar the new character. * @return a string derived from this string by replacing every * occurrence of <code>oldChar</code> with <code>newChar</code>. */ public String replace(char oldChar, char newChar) { if (oldChar != newChar) { int len = count; int i = -1; char[] val = value; /* avoid getfield opcode */ int off = offset; /* avoid getfield opcode */ while (++i < len) { if (val[off + i] == oldChar) { break; } } if (i < len) { char buf[] = new char[len]; for (int j = 0 ; j < i ; j++) { buf[j] = val[off+j]; } while (i < len) { char c = val[off + i]; buf[i] = (c == oldChar) ? newChar : c; i++; } return new String(0, len, buf); } } return this; } /** * Converts all of the characters in this String to lower case. * * @return the String, converted to lowercase. * @see Character#toLowerCase * @see String#toUpperCase */ public String toLowerCase() { int i; scan : { for(i = 0 ; i < count ; i++) { char c = value[offset+i]; if (c != Character.toLowerCase(c)) { break scan; } } return this; } char buf[] = new char[count]; System.arraycopy(value, offset, buf, 0, i); for(; i < count ; i++) { buf[i] = Character.toLowerCase(value[offset+i]); } return new String(0, count, buf); } /** * Converts all of the characters in this String to upper case. * * @return the String, converted to uppercase. * @see Character#toLowerCase * @see String#toUpperCase */ public String toUpperCase() { int i; scan : { for(i = 0 ; i < count ; i++) { char c = value[offset+i]; if (c != Character.toUpperCase(c)) { break scan; } } return this; } char buf[] = new char[count]; System.arraycopy(value, offset, buf, 0, i); for(; i < count ; i++) { buf[i] = Character.toUpperCase(value[offset+i]); } return new String(0, count, buf); } /** * Removes white space from both ends of this string. * <p> * If this <code>String</code> object represents an empty character * sequence, or the first and last characters of character sequence * represented by this <code>String</code> object both have codes * greater than <code>'\u0020'</code> (the space character), then a * reference to this <code>String</code> object is returned. * <p> * Otherwise, if there is no character with a code greater than * <code>'\u0020'</code> in the string, then a new * <code>String</code> object representing an empty string is created * and returned. * <p> * Otherwise, let <i>k</i> be the index of the first character in the * string whose code is greater than <code>'\u0020'</code>, and let * <i>m</i> be the index of the last character in the string whose code * is greater than <code>'\u0020'</code>. A new <code>String</code> * object is created, representing the substring of this string that * begins with the character at index <i>k</i> and ends with the * character at index <i>m</i>-that is, the result of * <code>this.substring(<i>k</i>, <i>m</i>+1)</code>. * <p> * This method may be used to trim whitespace from the beginning and end * of a string; in fact, it trims all ASCII control characters as well. * * @return this string, with white space removed from the front and end. */ public String trim() { int len = count; int st = 0; int off = offset; /* avoid getfield opcode */ char[] val = value; /* avoid getfield opcode */ while ((st < len) && (val[off + st] <= ' ')) { st++; } while ((st < len) && (val[off + len - 1] <= ' ')) { len--; } return ((st > 0) || (len < count)) ? substring(st, len) : this; } /** * This object (which is already a string!) is itself returned. * * @return the string itself. */ public String toString() { return this; } /** * Converts this string to a new character array. * * @return a newly allocated character array whose length is the length * of this string and whose contents are initialized to contain * the character sequence represented by this string. */ public char[] toCharArray() { char result[] = new char[count]; getChars(0, count, result, 0); return result; } /** * Returns the string representation of the <code>Object</code> argument. * * @param obj an <code>Object</code>. * @return if the argument is <code>null</code>, then a string equal to * <code>"null"</code>; otherwise, the value of * <code>obj.toString()</code> is returned. * @see java.lang.Object#toString() */ public static String valueOf(Object obj) { return (obj == null) ? "null" : obj.toString(); } /** * Returns the string representation of the <code>char</code> array * argument. The contents of the character array are copied; subsequent * modification of the character array does not affect the newly * created string. * * @param data a <code>char</code> array. * @return a newly allocated string representing the same sequence of * characters contained in the character array argument. */ public static String valueOf(char data[]) { return new String(data); } /** * Returns the string representation of a specific subarray of the * <code>char</code> array argument. * <p> * The <code>offset</code> argument is the index of the first * character of the subarray. The <code>count</code> argument * specifies the length of the subarray. The contents of the subarray * are copied; subsequent modification of the character array does not * affect the newly created string. * * @param data the character array. * @param offset the initial offset into the value of the * <code>String</code>. * @param count the length of the value of the <code>String</code>. * @return a newly allocated string representing the sequence of * characters contained in the subarray of the character array * argument. * @exception NullPointerException if <code>data</code> is * <code>null</code>. * @exception IndexOutOfBoundsException if <code>offset</code> is * negative, or <code>count</code> is negative, or * <code>offset+count</code> is larger than * <code>data.length</code>. */ public static String valueOf(char data[], int offset, int count) { return new String(data, offset, count); } /** * Returns the string representation of the <code>boolean</code> argument. * * @param b a <code>boolean</code>. * @return if the argument is <code>true</code>, a string equal to * <code>"true"</code> is returned; otherwise, a string equal to * <code>"false"</code> is returned. */ public static String valueOf(boolean b) { return b ? "true" : "false"; } /** * Returns the string representation of the <code>char</code> * argument. * * @param c a <code>char</code>. * @return a newly allocated string of length <code>1</code> containing * as its single character the argument <code>c</code>. */ public static String valueOf(char c) { char data[] = {c}; return new String(0, 1, data); } /** * Returns the string representation of the <code>int</code> argument. * <p> * The representation is exactly the one returned by the * <code>Integer.toString</code> method of one argument. * * @param i an <code>int</code>. * @return a newly allocated string containing a string representation of * the <code>int</code> argument. * @see java.lang.Integer#toString(int, int) */ public static String valueOf(int i) { return Integer.toString(i, 10); } /** * Returns the string representation of the <code>long</code> argument. * <p> * The representation is exactly the one returned by the * <code>Long.toString</code> method of one argument. * * @param l a <code>long</code>. * @return a newly allocated string containing a string representation of * the <code>long</code> argument. * @see java.lang.Long#toString(long) */ public static String valueOf(long l) { return Long.toString(l, 10); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -