📄 javafun.txt
字号:
package lightningboard.util;
import java.util.*;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.io.OutputStreamWriter;
import java.text.DecimalFormat;
import java.text.NumberFormat;
public class StringUtil
{
private static final String EOL = System.getProperty("line.separator");
private static PrintWriter handle = new PrintWriter(new OutputStreamWriter(System.out));
public static char fillChar = ' ';
private static char alignment;
private static boolean separator;
private static char base;
private static boolean scientific;
private static boolean zeroFill;
private static int width;
private static int decimals;
private static String pattern;
/*************************************************************
Formats the passed value using the passed format descriptor
and returns the result as a string.
@param value the value to be formatted.
@param fd the format descriptor.
@return the formatted value as a string.
**************************************************************/
public static String format(byte value, String fd)
{
return formatInteger(value, fd, 8);
}
/*************************************************************
Formats the passed value using the passed format descriptor
and returns the result as a string.
@param value the value to be formatted.
@param fd the format descriptor.
@return the formatted value as a string.
**************************************************************/
public static String format(char value, String fd)
{
return formatInteger(value, fd, 16);
}
/*************************************************************
Formats the passed value using the passed format descriptor
and returns the result as a string.
@param value the value to be formatted.
@param fd the format descriptor.
@return the formatted value as a string.
**************************************************************/
public static String format(double value, String fd)
{
extractAttributes(fd);
String s1;
if(base == 'B')
{
s1 = Long.toBinaryString(Double.doubleToLongBits(value));
s1 = repeat(64, '0') + s1;
s1 = s1.substring(s1.length() - 64);
} else if(base == 'X')
{
s1 = Long.toHexString(Double.doubleToLongBits(value));
s1 = repeat(16, '0') + s1;
s1 = s1.substring(s1.length() - 16);
} else
{
pattern = decimals != -1 ? "." + repeat(decimals, '0') : ".#";
if(scientific)
pattern = "0" + pattern + "E0";
else
pattern = separator ? "#,##0" + pattern : "0" + pattern;
s1 = (new DecimalFormat(pattern)).format(value);
}
return size(s1);
}
/*************************************************************
Formats the passed value using the passed format descriptor
and returns the result as a string.
@param value the value to be formatted.
@param fd the format descriptor.
@return the formatted value as a string.
**************************************************************/
public static String format(float value, String fd)
{
extractAttributes(fd);
String s1;
if(base == 'B')
{
s1 = Integer.toBinaryString(Float.floatToIntBits(value));
s1 = repeat(32, '0') + s1;
s1 = s1.substring(s1.length() - 32);
} else if(base == 'X')
{
s1 = Integer.toHexString(Float.floatToIntBits(value));
s1 = repeat(8, '0') + s1;
s1 = s1.substring(s1.length() - 8);
} else
{
pattern = decimals != -1 ? "." + repeat(decimals, '0') : ".#";
if(scientific)
pattern = "0" + pattern + "E0";
else
pattern = separator ? "#,##0" + pattern : "0" + pattern;
s1 = (new DecimalFormat(pattern)).format(value);
}
return size(s1);
}
/*************************************************************
Formats the passed value using the passed format descriptor
and returns the result as a string.
@param value the value to be formatted.
@param fd the format descriptor.
@return the formatted value as a string.
**************************************************************/
public static String format(int value, String fd)
{
return formatInteger(value, fd, 32);
}
/*************************************************************
Formats the passed value using the passed format descriptor
and returns the result as a string.
@param value the value to be formatted.
@param fd the format descriptor.
@return the formatted value as a string.
**************************************************************/
public static String format(long value, String fd)
{
return formatInteger(value, fd, 64);
}
/*************************************************************
Formats the passed value using the passed format descriptor
and returns the result as a string.
@param value the value to be formatted.
@param fd the format descriptor.
@return the formatted value as a string.
**************************************************************/
public static String format(short value, String fd)
{
return formatInteger(value, fd, 16);
}
/* Formats the passed value using the passed format descriptor
* and returns the result as a string.
*/
private static String formatInteger(long l, String s, int i)
{
extractAttributes(s);
String s1;
if(base == 'B')
{
s1 = Long.toBinaryString(l);
s1 = repeat(64, '0') + s1;
s1 = s1.substring(s1.length() - i);
} else
if(base == 'X')
{
s1 = Long.toHexString(l);
s1 = repeat(16, '0') + s1;
s1 = s1.substring(s1.length() - i / 4);
} else
if(separator)
{
s1 = (new DecimalFormat("#,###")).format(l);
} else
{
s1 = String.valueOf(l);
if(zeroFill)
s1 = repeat(width - s1.length(), '0') + s1;
}
return size(s1);
}
// Gets information from the passed format descriptor.
private static void extractAttributes(String s)
{
s = s.toUpperCase();
alignment = 'R';
separator = false;
base = 'D';
scientific = false;
zeroFill = false;
width = -1;
decimals = -1;
int i = s.indexOf(76, 0);
if(i > -1)
{
alignment = 'L';
s = s.substring(0, i) + s.substring(i + 1);
}
i = s.indexOf(67, 0);
if(i > -1)
{
alignment = 'C';
s = s.substring(0, i) + s.substring(i + 1);
}
i = s.indexOf(44, 0);
if(i > -1)
{
separator = true;
pattern = pattern + ",###";
s = s.substring(0, i) + s.substring(i + 1);
}
i = s.indexOf(88, 0);
if(i > -1)
{
base = 'X';
s = s.substring(0, i) + s.substring(i + 1);
} else
{
i = s.indexOf(66, 0);
if(i > -1)
{
base = 'B';
s = s.substring(0, i) + s.substring(i + 1);
}
}
i = s.indexOf(83, 0);
if(i > -1)
{
scientific = true;
s = s.substring(0, i) + s.substring(i + 1);
}
i = s.indexOf(90, 0);
if(i > -1)
{
zeroFill = true;
s = s.substring(0, i) + s.substring(i + 1);
}
i = s.indexOf(46, 0);
if(i > -1)
{
decimals = Integer.parseInt(s.substring(i + 1));
s = s.substring(0, i);
}
if(s.length() > 0)
width = Integer.parseInt(s);
}
/*************************************************************
Output the passed value to the standard output device using
the passed format descriptor. No trailing End-Of-Line
character is printed.
@param value the value to be printed.
@param fd the format descriptor.
**************************************************************/
public static void print(byte value, String fd)
{
handle.print(format(value, fd));
handle.flush();
}
/*************************************************************
Output the passed value to the standard output device using
the passed format descriptor. No trailing End-Of-Line
character is printed.
@param value the value to be printed.
@param fd the format descriptor.
**************************************************************/
public static void print(char value, String fd)
{
handle.print(format(value, fd));
handle.flush();
}
/*************************************************************
Output the passed value to the standard output device using
the passed format descriptor. No trailing End-Of-Line
character is printed.
@param value the value to be printed.
@param fd the format descriptor.
**************************************************************/
public static void print(double value, String fd)
{
handle.print(format(value, fd));
handle.flush();
}
/*************************************************************
Output the passed value to the standard output device using
the passed format descriptor. No trailing End-Of-Line
character is printed.
@param value the value to be printed.
@param fd the format descriptor.
**************************************************************/
public static void print(float value, String fd)
{
handle.print(format(value, fd));
handle.flush();
}
/*************************************************************
Output the passed value to the standard output device using
the passed format descriptor. No trailing End-Of-Line
character is printed.
@param value the value to be printed.
@param fd the format descriptor.
**************************************************************/
public static void print(int value, String fd)
{
handle.print(format(value, fd));
handle.flush();
}
/*************************************************************
Output the passed value to the standard output device using
the passed format descriptor. No trailing End-Of-Line
character is printed.
@param value the value to be printed.
@param fd the format descriptor.
**************************************************************/
public static void print(long value, String fd)
{
handle.print(format(value, fd));
handle.flush();
}
/*************************************************************
Output the passed value to the standard output device using
the passed format descriptor. No trailing End-Of-Line
character is printed.
@param value the value to be printed.
@param fd the format descriptor.
**************************************************************/
public static void print(short value, String fd)
{
handle.print(format(value, fd));
handle.flush();
}
/*************************************************************
Output the passed value to the standard output device using
the passed format descriptorand followed by an End-Of-Line
marker.
@param value the value to be printed.
@param fd the format descriptor.
**************************************************************/
public static void println(byte value, String fd)
{
print(value, fd);
handle.print(EOL);
handle.flush();
}
/*************************************************************
Output the passed value to the standard output device using
the passed format descriptorand followed by an End-Of-Line
marker.
@param value the value to be printed.
@param fd the format descriptor.
**************************************************************/
public static void println(char value, String fd)
{
print(value, fd);
handle.print(EOL);
handle.flush();
}
/*************************************************************
Output the passed value to the standard output device using
the passed format descriptorand followed by an End-Of-Line
marker.
@param value the value to be printed.
@param fd the format descriptor.
**************************************************************/
public static void println(double value, String fd)
{
print(value, fd);
handle.print(EOL);
handle.flush();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -