📄 stringtools.java
字号:
}// end if
}// end for
if ( changed )
{
s = new String( ca );
}
return s;
}// end toBookTitleCase
/**
* Convert int to hex with lead zeroes
*
* @param h number you want to convert to hex
* @return 0x followed by unsigned hex 8-digit representation
* @noinspection WeakerAccess
* @see #toString(Color)
*/
public static String toHexString( int h )
{
String s = Integer.toHexString( h );
if ( s.length() < 8 )
{// pad on left with zeros
s = "00000000".substring( 0, 8 - s.length() ) + s;
}
return "0x" + s;
}
/**
* Convert an integer to a String, with left zeroes.
*
* @param i the integer to be converted
* @param len the length of the resulting string. Warning. It will chop the result on the left if it is too long.
* @return String representation of the int e.g. 007
*/
public static String toLZ( int i, int len )
{
// Since String is final, we could not add this method there.
String s = Integer.toString( i );
if ( s.length() > len )
{/* return rightmost len chars */
return s.substring( s.length() - len );
}
else if ( s.length() < len )
// pad on left with zeros
{
return "000000000000000000000000000000".substring( 0, len - s.length() ) + s;
}
else
{
return s;
}
}// end toLZ
/**
* Quick replacement for Character.toLowerCase for use with English-only. It does not deal with accented
* characters.
*
* @param c character to convert
* @return character converted to lower case
*/
public static char toLowerCase( char c )
{
return 'A' <= c && c <= 'Z' ? ( char ) ( c + ( 'a' - 'A' ) ) : c;
}
/**
* Quick replacement for Character.toLowerCase for use with English-only. It does not deal with accented
* characters.
*
* @param s String to convert
* @return String converted to lower case
*/
public static String toLowerCase( String s )
{
final char[] ca = s.toCharArray();
final int length = ca.length;
boolean changed = false;
// can't use for:each since we need the index to set.
for ( int i = 0; i < length; i++ )
{
final char c = ca[ i ];
if ( 'A' <= c && c <= 'Z' )
{
// found a char that needs conversion.
ca[ i ] = ( char ) ( c + ( 'a' - 'A' ) );
changed = true;
}
}
// give back same string if unchanged.
return changed ? new String( ca ) : s;
}
/**
* convert an integer value to unsigned hex with leading zeroes.
*
* @param value integer to convert.
* @param len how many characters you want in the result.
*/
public static String toLzHexString( int value, int len )
{
// Since String is final, we could not add this method there.
final String s = Integer.toHexString( value );
if ( s.length() > len )
{/* return rightmost len chars */
return s.substring( s.length() - len );
}
else if ( s.length() < len )
// pad on left with zeros. at most 7 will be prepended
{
return "0000000".substring( 0, len - s.length() ) + s;
}
else
{
return s;
}
}
/**
* Get #ffffff html hex number for a colour
*
* @param c Color object whose html colour number you want as a string
* @return # followed by 6 hex digits
* @noinspection WeakerAccess,SameParameterValue
* @see #toHexString(int)
*/
public static String toString( Color c )
{
String s = Integer.toHexString( c.getRGB() & 0xffffff );
if ( s.length() < 6 )
{// pad on left with zeros
s = "000000".substring( 0, 6 - s.length() ) + s;
}
return '#' + s;
}
/**
* Quick replacement for Character.toUpperCase for use with English-only. It does not deal with accented
* characters.
*
* @param c character to convert
* @return character converted to upper case
*/
public static char toUpperCase( char c )
{
return 'a' <= c && c <= 'z' ? ( char ) ( c + ( 'A' - 'a' ) ) : c;
}
/**
* Quick replacement for Character.toUpperCase for use with English-only. It does not deal with accented
* characters.
*
* @param s String to convert
* @return String converted to upper case
*/
public static String toUpperCase( String s )
{
final char[] ca = s.toCharArray();
final int length = ca.length;
boolean changed = false;
// can't use for:each since we need the index to set.
for ( int i = 0; i < length; i++ )
{
final char c = ca[ i ];
if ( 'a' <= c && c <= 'z' )
{
// found a char that needs conversion.
ca[ i ] = ( char ) ( c + ( 'A' - 'a' ) );
changed = true;
}
}
// give back same string if unchanged.
return changed ? new String( ca ) : s;
}
/**
* Removes white space from beginning this string.
*
* @param s String to process. As always the original in unchanged.
* @return this string, with leading white space removed
* @noinspection WeakerAccess,WeakerAccess,WeakerAccess,SameParameterValue,WeakerAccess
* @see #trimLeading(String,char)
* <p/>
* All characters that have codes less than or equal to <code>'\u0020'</code> (the space character) are
* considered to be white space.
*/
public static String trimLeading( String s )
{
if ( s == null )
{
return null;
}
int len = s.length();
int st = 0;
while ( ( st < len ) && ( s.charAt( st ) <= ' ' ) )
{
st++;
}
return ( st > 0 ) ? s.substring( st, len ) : s;
}// end trimLeading
/**
* trim leading characters there are on a string matching a given character.
*
* @param text text with possible trailing characters, possibly empty, but not null.
* @param c the trailing character of interest, usually ' ' or '\n'
* @return string with any of those trailing characters removed.
* @see #trimLeading(String)
*/
public static String trimLeading( String text, char c )
{
int count = countLeading( text, c );
// substring will optimise the 0 case.
return text.substring( count );
}
/**
* Removes white space from end this string.
*
* @param s String to process. As always the original in unchanged.
* @return this string, with trailing white space removed
* @see #trimTrailing(String,char)
* <p/>
* All characters that have codes less than or equal to <code>'\u0020'</code> (the space character) are
* considered to be white space.
*/
public static String trimTrailing( String s )
{
if ( s == null )
{
return null;
}
int len = s.length();
int origLen = len;
while ( ( len > 0 ) && ( s.charAt( len - 1 ) <= ' ' ) )
{
len--;
}
return ( len != origLen ) ? s.substring( 0, len ) : s;
}// end trimTrailing
/**
* trim trailing characters there are on a string matching a given character.
*
* @param text text with possible trailing characters, possibly empty, but not null.
* @param c the trailing character of interest, usually ' ' or '\n'
* @return string with any of those trailing characters removed.
* @see #trimTrailing(String)
*/
public static String trimTrailing( String text, char c )
{
int count = countTrailing( text, c );
// substring will optimise the 0 case.
return text.substring( 0, text.length() - count );
}
// --------------------------- CONSTRUCTORS ---------------------------
/**
* StringTools contains only static methods.
*/
private StringTools()
{
}
// --------------------------- main() method ---------------------------
/**
* Test harness, used in debugging
*
* @param args not used
*/
public static void main( String[] args )
{
if ( DEBUGGING )
{
System.out.println( condense( " this is spaced. " ) );
System.out.println( trimLeading( " trim " ) );
System.out.println( trimTrailing( " trim " ) );
System.out.println( trimLeading( "*****t*r*i*m****", '*' ) );
System.out.println( trimTrailing( "*****t*r*i*m****", '*' ) );
System.out.println( toString( Color.red ) );
System.out.println( toHexString( -3 ) );
System.out.println( toHexString( 3 ) );
System.out.println( countLeading( "none", ' ' ) );
System.out.println( countLeading( "*one***", '*' ) );
System.out.println( countLeading( "\n\ntw\n\n\no\n\n\n\n", '\n' ) );
System.out.println( countTrailing( "none", ' ' ) );
System.out.println( countTrailing( "***one*", '*' ) );
System.out
.println( countTrailing( "\n\n\n\nt\n\n\n\nwo\n\n",
'\n' ) );
System.out.println( quoteSQL( "Judy's Place" ) );
System.out.println( parseLongPennies( "$5.00" ) );
System.out.println( parseLongPennies( "$50" ) );
System.out.println( parseLongPennies( "50" ) );
System.out.println( parseLongPennies( "$50-" ) );
System.out.println( penniesToString( 0 ) );
System.out.println( penniesToString( -1 ) );
System.out.println( penniesToString( 20 ) );
System.out.println( penniesToString( 302 ) );
System.out.println( penniesToString( -100000 ) );
System.out
.println( toBookTitleCase(
"handbook to HIGHER consciousness" ) );
System.out
.println( toBookTitleCase( "THE HISTORY OF THE U.S.A." ) );
System.out.println( toBookTitleCase( "THE HISTORY OF THE USA" ) );
System.out.println( rightPad( "abc", 6, true ) + "*" );
System.out.println( rightPad( "abc", 2, true ) + "*" );
System.out.println( rightPad( "abc", 2, false ) + "*" );
System.out.println( rightPad( "abc", 3, true ) + "*" );
System.out.println( rightPad( "abc", 3, false ) + "*" );
System.out.println( rightPad( "abc", 0, true ) + "*" );
System.out.println( rightPad( "abc", 20, true ) + "*" );
System.out.println( rightPad( "abc", 29, true ) + "*" );
System.out.println( rightPad( "abc", 30, true ) + "*" );
System.out.println( rightPad( "abc", 31, true ) + "*" );
System.out.println( rightPad( "abc", 40, true ) + "*" );
System.out.println( toUpperCase( 'q' ) );
System.out.println( toUpperCase( 'Q' ) );
System.out
.println( toUpperCase(
"The quick brown fox was 10 feet tall." ) );
System.out
.println( toUpperCase(
"THE QUICK BROWN FOX WAS 10 FEET TALL." ) );
System.out
.println( toUpperCase(
"the quick brown fox was 10 feet tall." ) );
System.out.println( toLowerCase( 'q' ) );
System.out.println( toLowerCase( 'Q' ) );
System.out
.println( toLowerCase(
"The quick brown fox was 10 feet tall." ) );
System.out
.println( toLowerCase(
"THE QUICK BROWN FOX WAS 10 FEET TALL." ) );
System.out
.println( toLowerCase(
"the quick brown fox was 10 feet tall." ) );
System.out.println( "count instances should be 4: " + countInstances( " abab abcdefgab", "ab" ) );
}
}// end main
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -