📄 stringutil.java
字号:
* @param elementSeparator The separator between the elements of the list
* @param keyValueSeparator The separator between the keys and values
*/
public Map asMap( String str, String elementSeparator,
String keyValueSeparator )
{
return this.toMap( str, elementSeparator, keyValueSeparator, null ) ;
} // asMap()
// -------------------------------------------------------------------------
/**
* Returns the given map object with all key-value pairs of the
* specified string added to it.
* <br>
* The separator between the keys and values is assumed to be "=".
* <p>
* <b>Be aware that all leading and trailing whitespaces of keys and values
* will be removed!</b>
*
* @param str The string that contains the list of key-value pairs
* @param elementSeparator The separator between the elements of the list
* @param map The map to which the key-value pairs are added
*/
public Map toMap( String str, String elementSeparator, Map map )
{
return this.toMap( str, elementSeparator, null, map ) ;
} // toMap()
// -------------------------------------------------------------------------
/**
* Adds all key-value pairs of the given string to the specified map.
* <br>
* The separator between the elements is assumed to be ","
* and "=" between key and value.
* <p>
* <b>Be aware that all leading and trailing whitespaces of keys and values
* will be removed!</b>
*
* @param str The string that contains the list of key-value pairs
* @param map The map to which the key-value pairs are added
*/
public Map toMap( String str, Map map )
{
return this.toMap( str, null, null, map ) ;
} // toMap()
// -------------------------------------------------------------------------
/**
* Adds all key-value pairs of the given string to a new properties object.
* <br>
* The separator between the elements is assumed to be ","
* and "=" between key and value.
* <p>
* <b>Be aware that all leading and trailing whitespaces of keys and values
* will be removed!</b>
*
* @param str The string that contains the list of key-value pairs
*/
public Properties asProperties( String str )
{
return this.toProperties( str, null ) ;
} // asProperties()
// -------------------------------------------------------------------------
/**
* Adds all key-value pairs of the given string to the specified properties.
* <br>
* The separator between the elements is assumed to be ","
* and "=" between key and value.
* <p>
* <b>Be aware that all leading and trailing whitespaces of keys and values
* will be removed!</b>
*
* @param str The string that contains the list of key-value pairs
* @param properties The properties where the key-value pairs should be added
*/
public Properties toProperties( String str, Properties properties )
{
Properties props = ( properties == null ) ? new Properties() : properties ;
return (Properties)this.toMap( str, null, null, props ) ;
} // toProperties()
// -------------------------------------------------------------------------
// =========================================================================
// PROTECTED INSTANCE METHODS
// =========================================================================
/**
* Cuts off all leading and trailing occurences of separator in text.
*/
protected String trimSeparator( String text, String separator )
{
int sepLen = separator.length() ;
while ( text.startsWith( separator ) )
text = text.substring( separator.length() ) ;
while ( text.endsWith( separator ) )
text = text.substring( 0, text.length() - sepLen ) ;
return text ;
} // trimSeparator()
// -------------------------------------------------------------------------
/**
* Returns an array of substrings of the given text. <br>
* The separators between the substrings are the given delimiters.
* Each character in the delimiter string is treated as a separator.
*
* @param text The string that should be splitted into substrings
* @param delimiters All characters that should be recognized as a separator or substrings
* @param all If true, empty elements will be returned, otherwise thye are skipped
* @return An array of substrings of the given text
*/
protected String[] parts( String text, String delimiters, boolean all )
{
ArrayList result = null ;
StringTokenizer tokenizer = null ;
if ( text == null )
return null ;
if ( ( delimiters == null ) || ( delimiters.length() == 0 ) )
{
String[] resultArray = { text } ;
return resultArray ;
}
if ( text.length() == 0 )
{
return new String[0] ;
}
else
{
result = new ArrayList() ;
tokenizer = new StringTokenizer( text, delimiters, all ) ;
if ( all )
this.collectParts( result, tokenizer, delimiters ) ;
else
this.collectParts( result, tokenizer ) ;
}
return (String[])result.toArray( new String[0] ) ;
} // parts()
// -------------------------------------------------------------------------
protected void collectParts( List list, StringTokenizer tokenizer )
{
while( tokenizer.hasMoreTokens() )
{
list.add( tokenizer.nextToken() ) ;
}
} // collectParts()
// -------------------------------------------------------------------------
protected void collectParts( List list, StringTokenizer tokenizer, String delimiter )
{
String token ;
boolean lastWasDelimiter = false ;
while( tokenizer.hasMoreTokens() )
{
token = tokenizer.nextToken() ;
if ( delimiter.indexOf( token ) >= 0 )
{
if ( lastWasDelimiter )
list.add( "" ) ;
lastWasDelimiter = true ;
}
else
{
list.add( token ) ;
lastWasDelimiter = false ;
}
}
} // collectParts()
// -------------------------------------------------------------------------
/**
* Returns the given text split up into an array of strings, at
* the occurrances of the separator string.
*
* In contrary to method parts() the separator is a one or many
* character sequence delimiter. That is, only the exact sequence
* of the characters in separator identifies the end of a substring.
* Parameter all defines whether empty strings between consecutive
* separators are added to the result or not.
*
* @see #parts(String, String, boolean)
* @param text The text to be split up
* @param separator The string that separates the substrings
* @param all If true, empty strings are added, otherwise skipped
* @return An array of substrings not containing any separator anymore
*/
protected String[] substrings( String text, String separator, boolean all )
{
int index = 0 ;
int start = 0 ;
int sepLen = 0 ;
int strLen = 0 ;
String str = text ;
ArrayList strings = new ArrayList() ;
if ( text == null )
return new String[0] ;
if ( ( separator == null ) || ( separator.length() == 0 ) )
{
if ( text.length() == 0 )
return new String[0] ;
String[] resultArray = { text } ;
return resultArray ;
}
if ( ! all )
str = this.trimSeparator( text, separator ) ;
strLen = str.length() ;
if ( strLen > 0 )
{
sepLen = separator.length() ;
index = str.indexOf( separator, start ) ;
while ( index >= 0 )
{
if ( all )
{
if ( index > 0 )
{
strings.add( str.substring( start, index ) ) ;
}
}
else
{
if ( index > ( start + sepLen ) )
strings.add( str.substring( start, index ) ) ;
}
start = index + sepLen ;
index = str.indexOf( separator, start ) ;
}
if ( start < strLen )
strings.add( str.substring( start ) ) ;
}
return (String[])strings.toArray( new String[0] ) ;
} // substrings()
// -------------------------------------------------------------------------
protected String padCh( String str, int len, char ch, boolean left )
{
StringBuffer buffer = null ;
int missing = len - str.length() ;
if ( missing <= 0 )
return str ;
buffer = new StringBuffer( len ) ;
if ( ! left )
buffer.append( str ) ;
for ( int i = 1 ; i <= missing ; i++ )
buffer.append( ch ) ;
if ( left )
buffer.append( str ) ;
return buffer.toString() ;
} // padCh()
// -------------------------------------------------------------------------
protected int indexOfString( String[] strArray, String searchStr, boolean ignoreCase )
{
if ( ( strArray == null ) || ( strArray.length == 0 ) )
return -1 ;
boolean found = false ;
for ( int i = 0 ; i < strArray.length ; i++ )
{
if ( strArray[i] == null )
{
if ( searchStr == null )
found = true ;
}
else
{
if ( ignoreCase )
found = strArray[i].equalsIgnoreCase( searchStr ) ;
else
found = strArray[i].equals( searchStr ) ;
}
if ( found )
return i ;
}
return -1 ;
} // indexOfString()
// -------------------------------------------------------------------------
/**
* Returns the substring of the given string that comes before the
* first occurance of the specified separator.
* If the string starts with a separator, the result will be an empty string.
* If the string doesn't contain the separator the method returns null or
* the whole string, depending on the returnNull flag.
*
* @param str The string of which the prefix is desired
* @param separator Separates the prefix from the rest of the string
* @param returnNull Specifies if null will be returned if no separator is found
*/
protected String prefix( String str, String separator, boolean returnNull )
{
if ( str == null )
return null ;
if ( separator == null )
return ( returnNull ? null : str ) ;
int index = str.indexOf( separator ) ;
if ( index >= 0 )
return str.substring( 0, index ) ;
else
return ( returnNull ? null : str ) ;
} // prefix()
// -------------------------------------------------------------------------
/**
* Returns the substring of the given string that comes after the
* first occurance of the specified separator.
* If the string ends with a separator, the result will be an empty string.
* If the string doesn't contain the separator the method returns null or
* the whole string, depending on the returnNull flag.
*
* @param str The string of which the suffix is desired
* @param separator Separates the suffix from the rest of the string
* @param returnNull Specifies if null will be returned if no separator is found
*/
protected String suffix( String str, String separator, boolean returnNull )
{
if ( str == null )
return null ;
if ( separator == null )
return ( returnNull ? null : str ) ;
int index = str.indexOf( separator ) ;
if ( index >= 0 )
return str.substring( index + separator.length() ) ;
else
return ( returnNull ? null : str ) ;
} // suffix()
// -------------------------------------------------------------------------
/**
* Removes the given strings from the array.
* If removeStrings is null it means that all null values are removed from
* the first array.
*/
protected String[] removeFromStringArray( String[] strings, String[] removeStrings )
{
List list ;
boolean remains ;
list = new ArrayList( strings.length ) ;
for (int i = 0; i < strings.length; i++)
{
if ( removeStrings == null )
{
remains = strings[i] != null ;
}
else
{
remains = ! this.contains( removeStrings, strings[i] ) ;
}
if ( remains )
{
list.add( strings[i] ) ;
}
}
return (String[])list.toArray( new String[list.size()] ) ;
} // removeFromStringArray()
// -------------------------------------------------------------------------
} // class StringUtil
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -