📄 string.java
字号:
if (!ignoreCase) { while (len-- > 0) { if (this.value[thisPos] != that.value[thatPos]) { return false; } thisPos++; thatPos++; } } else { while (len-- > 0) { if (Character.toLowerCase(this.value[thisPos]) != Character.toLowerCase(that.value[thatPos]) && Character.toUpperCase(this.value[thisPos]) != Character.toUpperCase(that.value[thatPos])) { return false; } thisPos++; thatPos++; } } return true;}public boolean regionMatches( int toffset, String other, int ooffset, int len) { return regionMatches( false, toffset, other, ooffset, len);}public String replace(char oldChar, char newChar) { if (oldChar == newChar) { return (this); } char buf[] = new char[count]; boolean replaced = false; for (int pos = 0; pos < count; pos++) { char cc = value[offset+pos]; if ( cc == oldChar) { replaced = true; buf[pos] = newChar; } else { buf[pos] = cc; } } if (!replaced) { return (this); } else { return (new String( 0, count, buf)); }}public boolean startsWith( String prefix) { return regionMatches( false, 0, prefix, 0, prefix.count);}public boolean startsWith( String prefix, int toffset) { return regionMatches( false, toffset, prefix, 0, prefix.count);}public String substring( int sIdx) { return substring( sIdx, count);}/* * shared data */public String substring( int sIdx, int eIdx) { if ( sIdx < 0) throw new StringIndexOutOfBoundsException( sIdx); if ( eIdx > count) throw new StringIndexOutOfBoundsException( eIdx); if ( sIdx > eIdx) throw new StringIndexOutOfBoundsException( eIdx-sIdx); if ( ( sIdx == 0) && ( eIdx == count ) ) return this; return new String( offset+sIdx, offset+eIdx, value);}public CharSequence subSequence( int sIdx, int eIdx) { return substring( sIdx, eIdx);}public char[] toCharArray() { char buf[] = new char[count]; if ( count > 0) getChars( 0, count, buf, 0); return buf;}public String toLowerCase() { return toLowerCase( Locale.getDefault());}public String toLowerCase( Locale lcl) { char buf[] = new char[count]; for (int pos = 0; pos < count; pos++) buf[pos] = Character.toLowerCase( value[offset+pos]); return new String( 0, count, buf);}public String toString() { return this;}public String toUpperCase() { return toUpperCase( Locale.getDefault());}public String toUpperCase( Locale lcl) { char buf[] = new char[count]; for (int pos=0; pos < count; pos++) buf[pos] = Character.toUpperCase( value[offset+pos]); return new String( 0, count, buf);}public String trim() { int i0 = offset; int i1 = offset+count-1; for ( ;(i0 <= i1) && (value[i0] <= ' '); i0++ ); if ( i0 > i1 ) return ""; for ( ;(i1 > i0) && (value[i1] <= ' '); i1-- ); return substring( i0-offset, i1+1-offset);} /* valueOf methods taken from GNU Classpath */ /** * Returns a String representation of an Object. This is "null" if the * object is null, otherwise it is <code>obj.toString()</code> (which * can be null). * * @param obj the Object * @return the string conversion of obj */ public static String valueOf(Object obj) { return obj == null ? "null" : obj.toString(); } /** * Returns a String representation of a character array. Subsequent * changes to the array do not affect the String. * * @param data the character array * @return a String containing the same character sequence as data * @throws NullPointerException if data is null * @see #valueOf(char[], int, int) * @see #String(char[]) */ public static String valueOf(char[] data) { return valueOf (data, 0, data.length); } /** * Returns a String representing the character sequence of the char array, * starting at the specified offset, and copying chars up to the specified * count. Subsequent changes to the array do not affect the String. * * @param data character array * @param offset position (base 0) to start copying out of data * @param count the number of characters from data to copy * @return String containing the chars from data[offset..offset+count] * @throws NullPointerException if data is null * @throws IndexOutOfBoundsException if (offset < 0 || count < 0 * || offset + count > data.length) * (while unspecified, this is a StringIndexOutOfBoundsException) * @see #String(char[], int, int) */ public static String valueOf(char[] data, int offset, int count) { return new String(data, offset, count, false); } /** * Returns a String representing a boolean. * * @param b the boolean * @return "true" if b is true, else "false" */ public static String valueOf(boolean b) { return b ? "true" : "false"; } /** * Returns a String representing a character. * * @param c the character * @return String containing the single character c */ public static String valueOf(char c) { // Package constructor avoids an array copy. return new String(new char[] { c }, 0, 1, true); } /** * Returns a String representing an integer. * * @param i the integer * @return String containing the integer in base 10 * @see Integer#toString(int) */ public static String valueOf(int i) { // See Integer to understand why we call the two-arg variant. return Integer.toString(i, 10); } /** * Returns a String representing a long. * * @param l the long * @return String containing the long in base 10 * @see Long#toString(long) */ public static String valueOf(long l) { return Long.toString(l); } /** * Returns a String representing a float. * * @param f the float * @return String containing the float * @see Float#toString(float) */ public static String valueOf(float f) { return Float.toString(f); } /** * Returns a String representing a double. * * @param d the double * @return String containing the double * @see Double#toString(double) */ public static String valueOf(double d) { return Double.toString(d); }public String intern() { return interned ? this : intern0(this);}private native static synchronized String intern0(String str);/* Custom only ....protected void finalize() throws Throwable { if (interned == true) { unintern0(this); } super.finalize();}final native public static synchronized void unintern0(String str);*/ /** * Creates a new String using the character sequence represented by * the StringBuilder. Subsequent changes to buf do not affect the String. * * @param buffer StringBuilder to copy * @throws NullPointerException if buffer is null */ public String(StringBuilder buffer) { this(buffer.value, 0, buffer.count); } /** * Special constructor which can share an array when safe to do so. * * @param data the characters to copy * @param offset the location to start from * @param count the number of characters to use * @param dont_copy true if the array is trusted, and need not be copied * @throws NullPointerException if chars is null * @throws StringIndexOutOfBoundsException if bounds check fails */ String(char[] data, int offset, int count, boolean dont_copy) { if (offset < 0 || count < 0 || offset + count > data.length) throw new StringIndexOutOfBoundsException(); if (dont_copy) { value = data; this.offset = offset; } else { value = new char[count]; System.arraycopy(data, offset, value, 0, count); this.offset = 0; } this.count = count; } /** * Returns the value array of the given string if it is zero based or a * copy of it that is zero based (stripping offset and making length equal * to count). Used for accessing the char[]s of gnu.java.lang.CharData. * Package private for use in Character. */ static char[] zeroBasedStringValue(String s) { char[] value; if (s.offset == 0 && s.count == s.value.length) value = s.value; else { int count = s.count; value = new char[count]; System.arraycopy(s.value, s.offset, value, 0, count); } return value; } /** * Test if this String matches a regular expression. This is shorthand for * <code>{@link Pattern}.matches(regex, this)</code>. * * @param regex the pattern to match * @return true if the pattern matches * @throws NullPointerException if regex is null * @throws PatternSyntaxException if regex is invalid * @see Pattern#matches(String, CharSequence) * @since 1.4 */ public boolean matches(String regex) { return Pattern.matches(regex, this); } /** * Replaces the first substring match of the regular expression with a * given replacement. This is shorthand for <code>{@link Pattern} * .compile(regex).matcher(this).replaceFirst(replacement)</code>. * * @param regex the pattern to match * @param replacement the replacement string * @return the modified string * @throws NullPointerException if regex or replacement is null * @throws PatternSyntaxException if regex is invalid * @see #replaceAll(String, String) * @see Pattern#compile(String) * @see Pattern#matcher(CharSequence) * @see Matcher#replaceFirst(String) * @since 1.4 */ public String replaceFirst(String regex, String replacement) { return Pattern.compile(regex).matcher(this).replaceFirst(replacement); } /** * Replaces all matching substrings of the regular expression with a * given replacement. This is shorthand for <code>{@link Pattern} * .compile(regex).matcher(this).replaceAll(replacement)</code>. * * @param regex the pattern to match * @param replacement the replacement string * @return the modified string * @throws NullPointerException if regex or replacement is null * @throws PatternSyntaxException if regex is invalid * @see #replaceFirst(String, String) * @see Pattern#compile(String) * @see Pattern#matcher(CharSequence) * @see Matcher#replaceAll(String) * @since 1.4 */ public String replaceAll(String regex, String replacement) { return Pattern.compile(regex).matcher(this).replaceAll(replacement); } /** * Split this string around the matches of a regular expression. Each * element of the returned array is the largest block of characters not * terminated by the regular expression, in the order the matches are found. * * <p>The limit affects the length of the array. If it is positive, the * array will contain at most n elements (n - 1 pattern matches). If * negative, the array length is unlimited, but there can be trailing empty * entries. if 0, the array length is unlimited, and trailing empty entries * are discarded. * * <p>For example, splitting "boo:and:foo" yields:<br> * <table border=0> * <th><td>Regex</td> <td>Limit</td> <td>Result</td></th> * <tr><td>":"</td> <td>2</td> <td>{ "boo", "and:foo" }</td></tr> * <tr><td>":"</td> <td>t</td> <td>{ "boo", "and", "foo" }</td></tr> * <tr><td>":"</td> <td>-2</td> <td>{ "boo", "and", "foo" }</td></tr> * <tr><td>"o"</td> <td>5</td> <td>{ "b", "", ":and:f", "", "" }</td></tr> * <tr><td>"o"</td> <td>-2</td> <td>{ "b", "", ":and:f", "", "" }</td></tr> * <tr><td>"o"</td> <td>0</td> <td>{ "b", "", ":and:f" }</td></tr> * </table> * * <p>This is shorthand for * <code>{@link Pattern}.compile(regex).split(this, limit)</code>. * * @param regex the pattern to match * @param limit the limit threshold * @return the array of split strings * @throws NullPointerException if regex or replacement is null * @throws PatternSyntaxException if regex is invalid * @see Pattern#compile(String) * @see Pattern#split(CharSequence, int) * @since 1.4 */ public String[] split(String regex, int limit) { return Pattern.compile(regex).split(this, limit); } /** * Split this string around the matches of a regular expression. Each * element of the returned array is the largest block of characters not * terminated by the regular expression, in the order the matches are found. * The array length is unlimited, and trailing empty entries are discarded, * as though calling <code>split(regex, 0)</code>. * * @param regex the pattern to match * @return the array of split strings * @throws NullPointerException if regex or replacement is null * @throws PatternSyntaxException if regex is invalid * @see #split(String, int) * @see Pattern#compile(String) * @see Pattern#split(CharSequence, int) * @since 1.4 */ public String[] split(String regex) { return Pattern.compile(regex).split(this, 0); } /** * Returns true iff this String contains the sequence of Characters * described in s. * @param s the CharSequence * @return true iff this String contains s */ public boolean contains (CharSequence s) { return this.indexOf(s.toString()) != -1; } /** * Returns a string that is this string with all instances of the sequence * represented by <code>target</code> replaced by the sequence in * <code>replacement</code>. * @param target the sequence to be replaced * @param replacement the sequence used as the replacement * @return the string constructed as above */ public String replace (CharSequence target, CharSequence replacement) { String targetString = target.toString(); String replaceString = replacement.toString(); int targetLength = target.length(); int replaceLength = replacement.length(); int startPos = this.indexOf(targetString); StringBuilder result = new StringBuilder(this); while (startPos != -1) { // Replace the target with the replacement result.replace(startPos, startPos + targetLength, replaceString); // Search for a new occurrence of the target startPos = result.indexOf(targetString, startPos + replaceLength); } return result.toString(); } /** * Return the index into this String that is offset from the given index by * <code>codePointOffset</code> code points. * @param index the index at which to start * @param codePointOffset the number of code points to offset * @return the index into this String that is <code>codePointOffset</code> * code points offset from <code>index</code>. * * @throws IndexOutOfBoundsException if index is negative or larger than the * length of this string. * @throws IndexOutOfBoundsException if codePointOffset is positive and the * substring starting with index has fewer than codePointOffset code points. * @throws IndexOutOfBoundsException if codePointOffset is negative and the * substring ending with index has fewer than (-codePointOffset) code points. * @since 1.5 */ public int offsetByCodePoints(int index, int codePointOffset) { if (index < 0 || index > count) throw new IndexOutOfBoundsException(); return Character.offsetByCodePoints(value, offset, count, offset + index, codePointOffset); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -