标签:
to convert a string, or a portion of a string, into an array of characters
palindrome.getChars(0, len, tempCharArray, 0);
You have seen the use of the printf()
and format()
methods to print output with formatted numbers. The String
class has an equivalent class method, format()
, that returns a String
object rather than a PrintStream
object.
Using String‘s
static format()
method allows you to create a formatted string that you can reuse, as opposed to a one-time print statement. For example, instead of
System.out.printf("The value of the float " + "variable is %f, while " + "the value of the " + "integer variable is %d, " + "and the string is %s", floatVar, intVar, stringVar);
you can write
String fs; fs = String.format("The value of the float " + "variable is %f, while " + "the value of the " + "integer variable is %d, " + " and the string is %s", floatVar, intVar, stringVar); System.out.println(fs);
valueOf
that converts a string to an object of that typeparseFloat()
method is more direct than the valueOf()
method.float a = (Float.valueOf(args[0])).floatValue(); // Float a = Float.valueOf(args[0]); float b = (Float.valueOf(args[1])).floatValue();
float a = Float.parseFloat(args[0]); float b = Float.parseFloat(args[1]);
Sometimes you need to convert a number to a string because you need to operate on the value in its string form. There are several easy ways to convert a number to a string:
int i; // Concatenate "i" with an empty string; conversion is handled for you. String s1 = "" + i;
or
// The valueOf class method. String s2 = String.valueOf(i);
Each of the Number
subclasses includes a class method, toString()
, that will convert its primitive type to a string. For example:
int i; double d; String s3 = Integer.toString(i); String s4 = Double.toString(d);
标签:
原文地址:http://www.cnblogs.com/morningdew/p/5700643.html