📄 stringutil.java
字号:
/**
* Returns the given string filled (on the right) up to the specified
* length with the given character. <br>
* Example: rightPadCh( "34", 5, 'X' ) --> "34XXX"
*/
public String rightPadCh( String str, int len, char ch )
{
return this.padCh( str, len, ch, false ) ;
} // rightPadCh()
// -------------------------------------------------------------------------
/**
* Returns the given string filled (on the right) up to the specified
* length with spaces. <br>
* Example: rightPad( "88", 6 ) --> "88 "
*/
public String rightPad( String str, int len )
{
return this.rightPadCh( str, len, CH_SPACE ) ;
} // rightPad()
// -------------------------------------------------------------------------
/**
* Returns the given integer as string filled (on the right) up to the
* specified length with the given character. <br>
* Example: rightPad( "32", 4, '#' ) --> "32##"
*/
public String rightPadCh( int value, int len, char fillChar )
{
return this.rightPadCh( Integer.toString(value), len, fillChar ) ;
} // rightPad()
// -------------------------------------------------------------------------
/**
* Returns the given integer as string filled (on the right) up to the
* specified length with spaces. <br>
* Example: rightPad( "17", 5 ) --> "17 "
*/
public String rightPad( int value, int len )
{
return this.rightPadCh( value, len, CH_SPACE ) ;
} // rightPad()
// -------------------------------------------------------------------------
/**
* Returns the given string filled equally left and right
* up to the specified length with the given character. <br>
* Example: centerCh( "A", 5, '_' ) --> "__A__" <br>
* Example: centerCh( "XX", 7, '+' ) --> "++XX+++"
*/
public String centerCh( String str, int len, char ch )
{
String buffer = null ;
int missing = len - str.length() ;
int half = 0 ;
if ( missing <= 0 )
return str ;
half = missing / 2 ;
buffer = this.rightPadCh( str, len-half, ch ) ;
return this.leftPadCh( buffer, len, ch ) ;
} // centerCh()
// -------------------------------------------------------------------------
/**
* Returns the given string filled (on the right and right)
* up to the specified length with spaces. <br>
* Example: center( "Mike", 10 ) --> " Mike "
*/
public String center( String str, int len )
{
return this.centerCh( str, len, CH_SPACE ) ;
} // center()
// -------------------------------------------------------------------------
/**
* Returns the given string array extended by one element
* that hold the specified string.
*/
public String[] append( String[] strings, String string )
{
String[] appStr = { string } ;
return this.append( strings, appStr ) ;
} // append()
// -------------------------------------------------------------------------
/**
* Returns an array of strings that contains all strings given
* by the first and second string array. The strings from the
* second array will be added at the end of the first array.
*
* @param strings The array of string to which to append
* @param appendStrings The string to be appended to the first array
*/
public String[] append( String[] strings, String[] appendStrings )
{
String[] newStrings = null ;
if ( strings == null )
return appendStrings ;
if ( appendStrings == null )
return strings ;
newStrings = new String[strings.length + appendStrings.length] ;
System.arraycopy( strings, 0, newStrings, 0, strings.length ) ;
System.arraycopy( appendStrings, 0, newStrings, strings.length, appendStrings.length ) ;
return newStrings ;
} // append()
// -------------------------------------------------------------------------
/**
* Returns an array of strings that contains all strings given
* in the first plus the specified string to append, if it is not
* already in the given array.
*/
public String[] appendIfNotThere( String[] strings, String appendString )
{
if ( this.contains( strings, appendString ) )
return strings ;
else
return this.append( strings, appendString ) ;
} // appendIfNotThere()
// -------------------------------------------------------------------------
/**
* Returns an array of strings that contains all strings given
* in the first plus all strings of the second array that are not
* already in the first array.
*/
public String[] appendIfNotThere( String[] strings, String[] appendStrings )
{
String[] newStrings = strings ;
if ( appendStrings == null )
return newStrings ;
for ( int i = 0 ; i < appendStrings.length ; i++ )
{
newStrings = this.appendIfNotThere( newStrings, appendStrings[i] ) ;
}
return newStrings ;
} // appendIfNotThere()
// -------------------------------------------------------------------------
/**
* Removes all string of the second array from the first array.
* Returns a new array of string that contains all remaining strings of the
* original strings array.
*
* @param strings The array from which to remove the strings
* @param removeStrings The strings to be removed
*/
public String[] remove( String[] strings, String[] removeStrings )
{
if ( ( strings == null ) || ( removeStrings == null )
|| ( strings.length == 0 ) || ( removeStrings.length == 0 ) )
{
return strings ;
}
return this.removeFromStringArray( strings, removeStrings ) ;
} // remove()
// -------------------------------------------------------------------------
/**
* Removes the given string from the specified string array.
* Returns a new array of string that contains all remaining strings of the
* original strings array.
*
* @param strings The array from which to remove the string
* @param removeString The string to be removed
*/
public String[] remove( String[] strings, String removeString )
{
String[] removeStrings = { removeString } ;
return this.remove( strings, removeStrings ) ;
} // remove()
// -------------------------------------------------------------------------
/**
* Removes all null values from the given string array.
* Returns a new string array that contains all none null values of the
* input array.
*
* @param strings The array to be cleared of null values
*/
public String[] removeNull( String[] strings )
{
if ( strings == null )
return strings ;
return this.removeFromStringArray( strings, null ) ;
} // removeNull()
// -------------------------------------------------------------------------
/**
* Returns a string that contains all given strings concatenated
* and separated by the specified separator.
*
* @param strings The array of strings that should be concatenated
* @param separator The separator between the strings
* @return One string containing the concatenated strings separated by separator
*/
public String asString ( String[] strings, String separator )
{
StringBuffer buffer = null ;
buffer = new StringBuffer( strings.length * 20 ) ;
if ( strings.length > 0 )
{
buffer.append( strings[0].toString() ) ;
for ( int i = 1 ; i < strings.length ; i++ )
{
buffer.append( separator ) ;
if ( strings[i] != null )
buffer.append( strings[i] ) ;
}
}
return buffer.toString() ;
} // asString()
// -------------------------------------------------------------------------
/**
* Returns a string that contains all given strings concatenated
* and separated by comma.
*
* @param strings The array of strings that should be concatenated
* @return One string containing the concatenated strings separated by comma (",")
*/
public String asString ( String[] strings )
{
return this.asString( strings, "," ) ;
} // asString()
// -------------------------------------------------------------------------
/**
* Returns the index of the first string in the given string array that
* matches the specified string pattern.
* If no string is found in the array the result is -1.
*
* @param strArray An array of string (may contain null elements)
* @param pattern The pattern the searched string must match
* @return The index of the matching string in the array or -1 if not found
*/
public int indexOf( String[] strArray, StringPattern pattern )
{
if ( ( strArray == null ) || ( strArray.length == 0 ) )
return -1 ;
boolean found = false ;
for ( int i = 0 ; i < strArray.length ; i++ )
{
if ( strArray[i] == null )
{
if ( pattern == null )
found = true ;
}
else
{
if ( pattern != null )
found = pattern.matches( strArray[i] ) ;
}
if ( found )
return i ;
}
return -1 ;
} // indexOf()
// -------------------------------------------------------------------------
/**
* Returns the index of the specified string in the given string array.
* It returns the index of the first occurrence of the string.
* If the string is not found in the array the result is -1.
* The comparison of the strings is case-sensitive!
*
* @param strArray An array of string (may contain null elements)
* @param searchStr The string to be looked up in the array (null allowed)
* @return The index of the string in the array or -1 if not found
*/
public int indexOf( String[] strArray, String searchStr )
{
return this.indexOfString( strArray, searchStr, false ) ;
} // indexOf()
// -------------------------------------------------------------------------
/**
* Returns the index of the specified string in the given string array.
* It returns the index of the first occurrence of the string.
* If the string is not found in the array the result is -1.
* The comparison of the strings is case-insensitive!
*
* @param strArray An array of string (may contain null elements)
* @param searchStr The string to be looked up in the array (null allowed)
* @return The index of the string in the array or -1 if not found
*/
public int indexOfIgnoreCase( String[] strArray, String searchStr )
{
return this.indexOfString( strArray, searchStr, true ) ;
} // indexOfIgnoreCase()
// -------------------------------------------------------------------------
/**
* Returns whether or not the specified string can be found
* in the given string array.
*
* @param strArray An array of string (may contain null elements)
* @param searchStr The string to be looked up in the array (null allowed)
* @param ignoreCase Defines whether or not the comparison is case-sensitive.
* @return true, if the specified array contains the given string
*/
public boolean contains( String[] strArray, String searchStr, boolean ignoreCase )
{
if ( ignoreCase )
return this.containsIgnoreCase( strArray, searchStr ) ;
else
return this.contains( strArray, searchStr ) ;
} // contains()
// -------------------------------------------------------------------------
/**
* Returns whether or not a string can be found in the given string array
* that matches the specified string pattern.
*
* @param strArray An array of string (may contain null elements)
* @param pattern The string pattern to match against in the array (null allowed)
* @return true, if the specified array contains a string matching the pattern
*/
public boolean contains( String[] strArray, StringPattern pattern)
{
return ( this.indexOf( strArray, pattern ) >= 0 ) ;
} // contains()
// -------------------------------------------------------------------------
/**
* Returns whether or not the specified string can be found
* in the given string array.
* The comparison of the strings is case-sensitive!
*
* @param strArray An array of string (may contain null elements)
* @param searchStr The string to be looked up in the array (null allowed)
* @return true, if the specified array contains the given string
*/
public boolean contains( String[] strArray, String searchStr )
{
return ( this.indexOf( strArray, searchStr ) >= 0 ) ;
} // contains()
// -------------------------------------------------------------------------
/**
* Returns whether or not the specified string can be found
* in the given string array.
* The comparison of the strings is case-insensitive!
*
* @param strArray An array of string (may contain null elements)
* @param searchStr The string to be looked up in the array (null allowed)
* @return true, if the specified array contains the given string
*/
public boolean containsIgnoreCase( String[] strArray, String searchStr )
{
return ( this.indexOfIgnoreCase( strArray, searchStr ) >= 0 ) ;
} // containsIgnoreCase()
// -------------------------------------------------------------------------
/**
* Returns all elements of string array <i>from</i> in a new array
* from index start up to the end.
* If start index is larger than the array's length, an empty array will be
* returned.
*
* @param from The string array the elements should be copied from
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -