Java basics-string processing (StringBuffer)
String processing is most often used as a String type object in Java. At the same time, String also provides a lot of functions to operate strings (you can refer to the materials). This article will introduce how StringBuffer operates strings. The StringBuffer uses a buffer and is also an operation String. However, unlike the String class, the content in the String class cannot be changed once declared, but the point of its memory address is changed, the content in StringBuffer can be changed. For StringBuffer, It is a specific operation class. Therefore, objects cannot be instantiated by direct value assignment like String, and must be constructed. StringBuffer connection character operations when the content of a string needs to be changed frequently, use StringBuffer to use the append () method in StringBuffer to complete string connection operations instance operation 1: string connection operation (append) code: package String processing; public class StringBufferDemo01 {public static void main (String args []) {StringBuffer buf = new StringBuffer (); // declare the StringBuffer object buf. append ("Hello"); // Add content buf to StringBuffer. append ("World "). append ("!!! "); // You can call the append () method buf continuously. append ("\ n"); // Add an escape character buf. append ("number = "). append (1 ). append ("\ n"); // Add a number buf. append ("character = "). append ('C '). append ("\ n"); // Add the character buf. append ("Boolean = "). append (true); // Add a Boolean value System. out. println (buf); // directly outputs the object and calls toString () }}; print the result: Hello World !!! Number = 1 character = C Boolean = true instance operation 2: String add content (insert) public class StringBufferDemo03 {public static void main (String args []) {StringBuffer buf = new StringBuffer (); // declare the StringBuffer object buf. append ("World !! "); // Add content buf. insert (0, "Hello"); // Add the content System before the first content. out. println (buf); buf. insert (buf. length (), "my yes ~ "); // Add the content System. out. println (buf) ;}}; print the content Hello World !! Hello World !! Even me ~ Instance operation 3: reverse package String processing; public class StringBufferDemo04 {public static void main (String args []) {StringBuffer buf = new StringBuffer (); // declare the StringBuffer object buf. append ("World !! "); // Add content buf. insert (0, "Hello"); // Add the content String str = buf before the first content. reverse (). toString (); // reverse the content and change it to the String type System. out. println (str); // output content}; printed content :!! DlroW olleH instance operation 4: replace the specified content of the String (replace) copy the code public class StringBufferDemo05 {public static void main (String args []) {StringBuffer buf = new StringBuffer (); // declare the StringBuffer object buf. append ("Hello "). append ("World !! "); // Add content buf to StringBuffer. replace (6, 11, "my Jesus"); // replace the world content with System. out. println ("result after content replacement:" + buf); // output content }}; printed content: result after content replacement: Hello, my !! Instance operation 5: String truncation (substring, delete) package String processing; public class StringBufferDemo06 {public static void main (String args []) {StringBuffer buf = new StringBuffer (); // declare the StringBuffer object buf. append ("Hello "). append ("World !! "); // Add content buf to StringBuffer. replace (6, 11, "my Jesus"); // replace the world content with String str = buf. substring (6, 11); // intercept content in the specified range // String str = buf. delete (6, 11 ). toString (); // Delete the content in the specified range System. out. println (buf); System. out. println ("result after content replacement:" + str); // output content }}; print the result Hello me !! The result after the content is replaced: my dear! Instance operation 6: query the specified content (indexOf) package String for processing; public class StringBufferDemo08 {public static void main (String args []) {StringBuffer buf = new StringBuffer (); // declare the StringBuffer object buf. append ("Hello "). append ("World !! "); // Add content to StringBuffer if (buf. indexOf ("Hello") =-1) {System. out. println ("no specified content found");} else {// if not 01, the System is found. out. println ("you can find the specified content") ;}}; printed content can find the specified content. In StringBuffer, there are still many functions that will not be described one by one! For more information, see the following section. We will introduce a case where String and StringBuffer are used to operate strings. lh. bean; import java. io. bufferedReader; import java. io. IOException; import java. io. inputStreamReader; public class StringUtil {private String money; // The numeric amount before conversion private String submoneyCN [] = {"," Pick "," success "," success "}; // private String numberCNN [] = {"zero", "one", "two", "three", "Si", "Wu ", "land", "yellow"}; // an array of upper-case numbers private String je =" Lu Zhengyu "; // an upper-case number (0-9) private String cdw =" tens of thousands of yuan, hundreds of millions yuan, and tens of thousands of yuan "; // amount unit public StringUtil () {}// default constructor public void setMoney (String money) {this. money = money;} public String getMoney () {return convert (this. money);}/*** convert the numeric amount to the capital amount * @ return returns the converted capital amount */public String convert (String money) {String formatCN = ""; int point = money. indexOf (". "); // determines whether the string contains the decimal point if (point! =-1) {String money1 = money. substring (0, point); // the part before the decimal point String money1_1 = (new StringBuffer (money1 ). reverse ()). toString (); // String in reverse order String money2 = money. substring (point + 1); // after the decimal point, if (money2.length () <2) {// if the decimal point is less than two digits, use "0" to complete if (money2.length () = 0) money2 = "00"; else money2 + = "0 ";} else // if the number of digits after the decimal point is greater than two digits, only the first two digits of money2 = money. substring (point + 1, point + 3); int len = money1_1.length (); // The number of digits before the decimal point after the string is reversed. Nt pos = len-1; String sigle = ""; boolean allhavenum = false; boolean havenum = false; boolean mark = false; // set a switch variable if the current number is "0 ", set this value to true. If it is not set to "0", set it to false/** the following code reads the part on the left of the decimal point */while (pos> = 0) {sigle = money1_1.substring (pos, pos + 1); // truncates 1 digit, for example, a Number 1234.56, and reverses the string on the left. The value is 4321, the intercepted value is 1/*** to read the code of "million units. * Read 10024531042.34. * Decimal point: 24013542001 * The initial value of pos is 10 * The initial value of mark is false * The initial value of havenum is false **/if (pos> = 8 & pos <12) {if (! Sigle. equals ("0") {// if the current value is not "0" if (! Mark) {// if the first digit of the current value is not "0" formatCN + = numberCNN [Integer. parseInt (sigle)] + submoneyCN [pos % 4];} else {// if the current value is not "0 ", however, the first digit of this value is "0" if (allhavenum) {// if there is a number not "0" before the current value. This condition is used to process user input values such as: 0012.34 value formatCN + = "0";} formatCN + = numberCNN [Integer. parseInt (sigle)] + submoneyCN [pos % 4]; mark = false;} havenum = true; allhavenum = true; // The variable allhavenum indicates whether there is a number that is not "0" on the left of the decimal point. true indicates yes, false indicates none} else {// if the current value is "0" mark = true;} if (pos % 4 = 0 & havenum) {// if the current number is the last digit of the unit and the number is not "0", formatCN + = ""; havenum = false ;}} /** read the code of "10 thousand units" */if (pos> = 4 & amp; pos <8) {if (! Sigle. equals ("0") {if (! Mark) formatCN + = numberCNN [Integer. parseInt (sigle)] + submoneyCN [pos % 4]; else {if (allhavenum) {formatCN + = "0";} formatCN + = numberCNN [Integer. parseInt (sigle)] + submoneyCN [pos % 4]; mark = false;} havenum = true; allhavenum = true;} else {mark = true ;} if (pos % 4 = 0 & havenum) {formatCN + = "Ten Thousand"; havenum = false ;}} /** read the code "count, Count, Count, count, and count" */if (pos> = 0 & amp; pos <4) {if (! Sigle. equals ("0") {if (! Mark) formatCN + = numberCNN [Integer. parseInt (sigle)] + submoneyCN [pos % 4]; else {if (allhavenum) {formatCN + = "0";} formatCN + = numberCNN [Integer. parseInt (sigle)] + submoneyCN [pos % 4]; mark = false;} havenum = true; allhavenum = true;} else {mark = true;} pos --;} /** read the decimal point */if (allhavenum) // For example, 00.34 cannot be read as: RMB 3-4 points. the variable allhavenum indicates whether there are digits in the content on the left of the decimal point in formatCN + = "Yuan"; else // If the left of the decimal point is 0, for example: 00.34, the value should be read: zero RMB, 3 cents, 4 cents, formatCN = "Zero RMB ";/ ** The following code reads the right part of the decimal point */if (money2.equals ("00") formatCN + = "whole"; else {/** read angle, for example, 120.34: 1 cent 2 yuan collection and 3 cent 4 cent; 123.04 reading: 1 cent 2 pick 3 yuan 0 4 cent */if (money2.startsWith ("0 ") | (allhavenum & money1.endsWith ("0") {formatCN + = "0";} if (! Money2.startsWith ("0") {formatCN + = numberCNN [Integer. parseInt (money2.substring (12.30)] + "";} // read the score, for example: read 1 pick 2 yuan 3 angle zero score formatCN + = numberCNN [Integer. parseInt (money2.substring (1)] + "points" ;}} the else {formatCN = "input format is incorrect! Format: 888.00 ";} return formatCN;} public static void main (String [] args) throws IOException {System. out. println ("input money to be converted:"); BufferedReader br = new BufferedReader (new InputStreamReader (System. in); String money = br. readLine (); StringUtil SU = new StringUtil (); System. out. println (SU. convert (money ));}}