📄 stringconverter.java
字号:
String y = stok.nextToken(); int xval = 0, yval = 0; try { xval = Integer.parseInt(x); yval = Integer.parseInt(y); } catch (NumberFormatException e) { throw new DataFormatException(e.getMessage()); } return new Point(xval, yval); } /** * Converts the given value into an SWT point. * Returns the given default value if the * value does not represent a point. * * @param value the value to be converted * @param dflt the default value * @return the value as a point, or the default value */ public static Point asPoint(String value, Point dflt) { try { return asPoint(value); } catch (DataFormatException e) { return dflt; } } /** * Converts the given value into an SWT rectangle. * This method fails if the value does not represent a rectangle. * <p> * A valid rectangle representation is a string of the form * <code><it>x</it>,<it>y</it>,<it>width</it>,<it>height</it></code> * where <code><it>x</it></code>, <code><it>y</it></code>, * <code><it>width</it></code>, and <code><it>height</it></code> * are valid ints. * </p> * * @param value the value to be converted * @return the value as a rectangle * @exception DataFormatException if the given value does not represent * a rectangle */ public static Rectangle asRectangle(String value) throws DataFormatException { if (value == null) { throw new DataFormatException( "Null doesn't represent a valid rectangle"); //$NON-NLS-1$ } StringTokenizer stok = new StringTokenizer(value, ","); //$NON-NLS-1$ String x = stok.nextToken(); String y = stok.nextToken(); String width = stok.nextToken(); String height = stok.nextToken(); int xval = 0, yval = 0, wval = 0, hval = 0; try { xval = Integer.parseInt(x); yval = Integer.parseInt(y); wval = Integer.parseInt(width); hval = Integer.parseInt(height); } catch (NumberFormatException e) { throw new DataFormatException(e.getMessage()); } return new Rectangle(xval, yval, wval, hval); } /** * Converts the given value into an SWT rectangle. * Returns the given default value if the * value does not represent a rectangle. * * @param value the value to be converted * @param dflt the default value * @return the value as a rectangle, or the default value */ public static Rectangle asRectangle(String value, Rectangle dflt) { try { return asRectangle(value); } catch (DataFormatException e) { return dflt; } } /** * Converts the given value into an SWT RGB color value. * This method fails if the value does not represent an RGB * color value. * <p> * A valid RGB color value representation is a string of the form * <code><it>red</it>,<it>green</it></code>,<it>blue</it></code> where * <code><it>red</it></code>, <it>green</it></code>, and * <code><it>blue</it></code> are valid ints. * </p> * * @param value the value to be converted * @return the value as an RGB color value * @exception DataFormatException if the given value does not represent * an RGB color value */ public static RGB asRGB(String value) throws DataFormatException { if (value == null) { throw new DataFormatException("Null doesn't represent a valid RGB"); //$NON-NLS-1$ } StringTokenizer stok = new StringTokenizer(value, ","); //$NON-NLS-1$ try { String red = stok.nextToken(); String green = stok.nextToken(); String blue = stok.nextToken(); int rval = 0, gval = 0, bval = 0; try { rval = Integer.parseInt(red); gval = Integer.parseInt(green); bval = Integer.parseInt(blue); } catch (NumberFormatException e) { throw new DataFormatException(e.getMessage()); } return new RGB(rval, gval, bval); } catch (NoSuchElementException e) { throw new DataFormatException(e.getMessage()); } } /** * Converts the given value into an SWT RGB color value. * Returns the given default value if the * value does not represent an RGB color value. * * @param value the value to be converted * @param dflt the default value * @return the value as a RGB color value, or the default value */ public static RGB asRGB(String value, RGB dflt) { try { return asRGB(value); } catch (DataFormatException e) { return dflt; } } /** * Converts the given double value to a string. * Equivalent to <code>String.valueOf(value)</code>. * * @param value the double value * @return the string representing the given double */ public static String asString(double value) { return String.valueOf(value); } /** * Converts the given float value to a string. * Equivalent to <code>String.valueOf(value)</code>. * * @param value the float value * @return the string representing the given float */ public static String asString(float value) { return String.valueOf(value); } /** * Converts the given int value to a string. * Equivalent to <code>String.valueOf(value)</code>. * * @param value the int value * @return the string representing the given int */ public static String asString(int value) { return String.valueOf(value); } /** * Converts the given long value to a string. * Equivalent to <code>String.valueOf(value)</code>. * * @param value the long value * @return the string representing the given long */ public static String asString(long value) { return String.valueOf(value); } /** * Converts the given boolean object to a string. * Equivalent to <code>String.valueOf(value.booleanValue())</code>. * * @param value the boolean object * @return the string representing the given boolean value */ public static String asString(Boolean value) { Assert.isNotNull(value); return String.valueOf(value.booleanValue()); } /** * Converts the given double object to a string. * Equivalent to <code>String.valueOf(value.doubleValue())</code>. * * @param value the double object * @return the string representing the given double value */ public static String asString(Double value) { Assert.isNotNull(value); return String.valueOf(value.doubleValue()); } /** * Converts the given float object to a string. * Equivalent to <code>String.valueOf(value.floatValue())</code>. * * @param value the float object * @return the string representing the given float value */ public static String asString(Float value) { Assert.isNotNull(value); return String.valueOf(value.floatValue()); } /** * Converts the given integer object to a string. * Equivalent to <code>String.valueOf(value.intValue())</code>. * * @param value the integer object * @return the string representing the given integer value */ public static String asString(Integer value) { Assert.isNotNull(value); return String.valueOf(value.intValue()); } /** * Converts the given long object to a string. * Equivalent to <code>String.valueOf(value.longValue())</code>. * * @param value the long object * @return the string representing the given long value */ public static String asString(Long value) { Assert.isNotNull(value); return String.valueOf(value.longValue()); } /** * Converts a font data array to a string. The string representation is * that of asString(FontData) seperated by ';' * * @param value The font data. * @return The string representation of the font data arra. * @since 3.0 */ public static String asString(FontData[] value) { StringBuffer buffer = new StringBuffer(); for (int i = 0; i < value.length; i++) { buffer.append(asString(value[i])); if (i != value.length - 1) { buffer.append(FONT_SEPARATOR); } } return buffer.toString(); } /** * Converts a font data object to a string. The string representation is * "font name-style-height" (for example "Times New Roman-bold-36"). * @param value The font data. * @return The string representation of the font data object. */ public static String asString(FontData value) { Assert.isNotNull(value); StringBuffer buffer = new StringBuffer(); buffer.append(value.getName()); buffer.append(SEPARATOR); int style = value.getStyle(); boolean bold = (style & SWT.BOLD) == SWT.BOLD; boolean italic = (style & SWT.ITALIC) == SWT.ITALIC; if (bold && italic) { buffer.append(BOLD_ITALIC); } else if (bold) { buffer.append(BOLD); } else if (italic) { buffer.append(ITALIC); } else { buffer.append(REGULAR); } buffer.append(SEPARATOR); buffer.append(value.getHeight()); return buffer.toString(); } /** * Converts the given SWT point object to a string. * <p> * The string representation of a point has the form * <code><it>x</it>,<it>y</it></code> where * <code><it>x</it></code> and <code><it>y</it></code> * are string representations of integers. * </p> * * @param value the point object * @return the string representing the given point */ public static String asString(Point value) { Assert.isNotNull(value); StringBuffer buffer = new StringBuffer(); buffer.append(value.x); buffer.append(','); buffer.append(value.y); return buffer.toString(); } /** * Converts the given SWT rectangle object to a string. * <p> * The string representation of a rectangle has the form * <code><it>x</it>,<it>y</it>,<it>width</it>,<it>height</it></code> * where <code><it>x</it></code>, <code><it>y</it></code>, * <code><it>width</it></code>, and <code><it>height</it></code> * are string representations of integers. * </p> * * @param value the rectangle object * @return the string representing the given rectangle */ public static String asString(Rectangle value) { Assert.isNotNull(value); StringBuffer buffer = new StringBuffer(); buffer.append(value.x); buffer.append(','); buffer.append(value.y); buffer.append(','); buffer.append(value.width); buffer.append(','); buffer.append(value.height); return buffer.toString(); } /** * Converts the given SWT RGB color value object to a string. * <p> * The string representation of an RGB color value has the form * <code><it>red</it>,<it>green</it></code>,<it>blue</it></code> where * <code><it>red</it></code>, <it>green</it></code>, and * <code><it>blue</it></code> are string representations of integers. * </p> * * @param value the RGB color value object * @return the string representing the given RGB color value */ public static String asString(RGB value) { Assert.isNotNull(value); StringBuffer buffer = new StringBuffer(); buffer.append(value.red); buffer.append(','); buffer.append(value.green); buffer.append(','); buffer.append(value.blue); return buffer.toString(); } /** * Converts the given boolean value to a string. * Equivalent to <code>String.valueOf(value)</code>. * * @param value the boolean value * @return the string representing the given boolean */ public static String asString(boolean value) { return String.valueOf(value); } /** * Returns the given string with all whitespace characters removed. * <p> * All characters that have codes less than or equal to <code>'\u0020'</code> * (the space character) are considered to be a white space. * </p> * * @param s the source string * @return the string with all whitespace characters removed */ public static String removeWhiteSpaces(String s) { //check for no whitespace (common case) boolean found = false; int wsIndex = -1; int size = s.length(); for (int i = 0; i < size; i++) { found = Character.isWhitespace(s.charAt(i)); if (found) { wsIndex = i; break; } } if (!found) { return s; } StringBuffer result = new StringBuffer(s.substring(0, wsIndex)); for (int i = wsIndex + 1; i < size; i++) { char ch = s.charAt(i); if (!Character.isWhitespace(ch)) { result.append(ch); } } return result.toString(); } /** * Converts a font data object to a string representation for display. * The string representation is * "font name-style-height" (for example "Times New Roman-bold-36"). * @param value The font data. * @return The string representation of the font data object. * @deprecated use asString(FontData) */ public static String asDisplayableString(FontData value) { Assert.isNotNull(value); StringBuffer buffer = new StringBuffer(); buffer.append(value.getName()); buffer.append(SEPARATOR); int style = value.getStyle(); boolean bold = (style & SWT.BOLD) == SWT.BOLD; boolean italic = (style & SWT.ITALIC) == SWT.ITALIC; if (bold && italic) { buffer.append(JFaceResources.getString("BoldItalicFont")); //$NON-NLS-1$ } else if (bold) { buffer.append(JFaceResources.getString("BoldFont")); //$NON-NLS-1$ } else if (italic) { buffer.append(JFaceResources.getString("ItalicFont")); //$NON-NLS-1$ } else { buffer.append(JFaceResources.getString("RegularFont")); //$NON-NLS-1$ } buffer.append(SEPARATOR); buffer.append(value.getHeight()); return buffer.toString(); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -