JAVA classic example - Connection String
Question: some strings linking solution:
Three methods:
1, No. connect directly +, the compiler will construct a StringBuffer object, and call its append method 2, their structure StringBuffer object, append () method will return to the StringBuffer object itself are cited.
3, through toString method code:
/ **
* StringBufferDemo: three ways the same tectonic string * /
(Public class StringBufferDemo
Public static void main (String argv []) (
String s1 = "Hello" + "and" + "World";
System.out.println (s1);
/ / Construction StringBuffer object, and add some strings StringBuffer sb2 = new StringBuffer ();
Sb2.append ( "Hello");
Sb2.append (',');
Sb2.append ( '');
Sb2.append ( "World");
/ / Will be converted to a string StringBuffer value, and output String s2 = sb2.toString ();
System.out.println (s2);
/ / Now repeat the above work, but adopt a more concise way / / typical "real-world" JAVA
StringBuffer sb3 = new StringBuffer (). Append ( "Hello").
Append (','). append ( ''). Append ( "World");
System.out.println (sb3.toString ());
)
)
Summary: In fact, both the revised StringBuffer number of characters, all the way append (), delete (), deleteCharAt (), insert (), replace (), reverse (), etc. only to return to invoke StringBuffer Object This program is extremely beneficial.
Tags: Connection, java string, String






