String builder in Java, and printf() function

tags: java input and output

Last time there was a little bit of the string class that was not completely covered, that is, it was about the string builder.

One.string builder

To continuously increase the content of a string object in Java can be achieved by continuously creating a new string object using the splicing character "+". as follows:

	        String str1="abc";
		String str2="def";
		str1+=str2;
		System.out.println(str1);

But there is a disadvantage of this, that is, every splicing needs to construct a new string object, which wastes space and time. Then the StringBuilder class in Java solves this problem. The code is as follows:

        StringBuilder strb=new StringBuilder();
	strb.append("asdas");
	strb.append("asdasd");
	String str=strb.toString();
	System.out.println(str);

Two main methods: append() adds a string to the builder, and tostring is converted to a string object.

2. Input and output

The input must first define a Scanner object and associate System.in. Import the package Java.util;

There are more methods for the Scanner class, mainly the following:

nextline() reads a line, this is mainly to read the spaces in a line. If you want to read a word, use the next() method. code show as below:

        Scanner scan=new Scanner(System.in);
	System.out.println("Read a line including spaces!");
	String str1=scan.nextLine();
	 System.out.println("Read a word separated by a space!");
	String str2=scan.next();
	System.out.println(str1);
	System.out.println(str2);

result:


Formatted output:

The printf() function of c is quoted in Java, and the output format is specified by interspersing quotation marks%.

Conversion characters for common printf:

d Decimal number x Hexadecimal number o Octal number f Fixed-point floating-point number e Exponential output floating-point number g Universal floating-point number a Hexadecimal number% s string number as follows c

	System.out.printf("%o",20);
	System.out.printf("%s", "asdafa");

Other conversion symbols:

+When printing positive and negative signs

Add a space before the positive number

-Left aligned

code show as below:

System.out.printf("%-8.6f",6.012);

The format is: "(starting character)% (whether it contains)-(left-aligned if it contains) Format mn (m represents the output length, n represents the number of digits after the decimal point) (letters) f, l, etc. to output the value Modification of form".

The syntax diagram is as follows:










Intelligent Recommendation

[Printf] sprintf string rotation value, numerical turnt character, splicing character, function detailed explanation Printf print value ...

1, numerical conversion character: Sprintf function, such as: Sprintf (s, "% d", 123); // generate "123" 2, string to turn to the value: while(*BandRate='\0')   {num=num*10+(*...

Linux C string output function puts (), fputs (), printf () Explanation

A, puts () function Detailed the puts () function is used to write the string to the standard output device (screen) and line, call the format: puts(s); Wherein s is a string variable (or string array...

JAVA advanced features --String / StringBuffer / Builder

String String objects can not be changed once created is a constant Requires a lot of string manipulation should StringBuffer / StringBuilder final result into a String object StringBuffer Thread-safe...

Java study notes - String Builder (StringBuilder)

Java study notes - String Builder (StringBuilder) String  toString() Return Type: String TypeString Function: The contents of the specified string generating a return type StringStringBuilder to ...

Java Preliminary Syntax (7) String Builder

Creating a successful string object, its length is fixed, the content cannot be compiled, although the symbol "+" can be used to achieve the purpose of additional new characters or strings, ...

More Recommendation

Java Learning Tour (20): String Builder

Creating a successful string object, the length is generally fixed, and the content cannot be changed and compiled. Although using "+" can achieve the purpose of additional new strings or ne...

printf in JAVA

Detailed explanation of usage in printf in java The full format of printf's format control: % - 0 m.n l or h format characters The following describes the items that make up the format description: 1%...

java printf

format %[arg_index$][flags][width][.precision]conversion conversion % S: format string % S: format string, and convert to upper case % D: integer format % O: integer format, and octal string % X: inte...

Java foundation - String, String Builder and String Buffer class

Article catalog String Different assignment methods Invigible StringBuilder's variability StringBuffer String String is actually an array of characters; Initialization method: 1) char[] c = {‘a&...

Copyright  DMCA © 2018-2026 - All Rights Reserved - www.programmersought.com  User Notice

Top