string.java

来自「kaffe Java 解释器语言,源码,Java的子集系统,开放源代码」· Java 代码 · 共 780 行 · 第 1/2 页

JAVA
780
字号
		return (-1);	}	/* Clip the index to be within the valid range (if non-empty) */	if (eIdx >= count) {		eIdx = count - 1;	}	if (eIdx < 0) {		return(-1);	}	/* Search for character */	for (int pos = eIdx; pos >= 0; pos--) {		if ( value[offset+pos] == c) {			return (pos);		}	}	return (-1);}public int length() {	return count;}public boolean regionMatches(boolean ignoreCase, int thisOffset,		String that, int thatOffset, int len) {	// Check bounds	if ((thisOffset < 0 || thisOffset + len > this.count)	    || (thatOffset < 0 || thatOffset + len > that.count)) {		return false;	}	int thisPos = this.offset + thisOffset;	int thatPos = that.offset + thatOffset;	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);}public static String valueOf( Object obj) {	return (obj == null) ? "null" : obj.toString();}public static String valueOf( boolean b) {	return ( new Boolean(b)).toString();}public static String valueOf( char c) {	return new String(new char[] { c });}public static String valueOf(char data[]) {	return new String(data);}public static String valueOf( char data[], int offset, int count) {	return new String( data, offset, count);}public static String valueOf( double d) {	return Double.toString(d);}public static String valueOf( float f) {	return Float.toString(f);}public static String valueOf( int i) {	return Integer.toString(i);}public static String valueOf( long l) {	return Long.toString(l);}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);*/  /**   * 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);  }}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?