StringBuilder splicing (intercepting) the desired string
String stitching
import ; /* * Use StringBuilder to splice the desired string * It can be used to splice query conditions during development. For a novice, if you have a better way, please correct me if you have any errors. Thank you very much * */ public class testStringBuilder { public static void main(String[] args) { StringBuilder sb =new StringBuilder(); ArrayList<String> list = new ArrayList<>();//Define a List and add a string (four serial numbers) ("2022121212"); ("2022121211"); ("2022121213"); ("2022121214"); ("The original output list is:"+list);//Output original List for (int i = 0; i <() ; i++) { //Loop out the string element in the list and use a ternary expression to determine whether it is the last element. If it is the last element, there is no need to splice commas. //I am developing myself and need to query the database using IN conditions. // select *from user_info where serinalno in ('2022121212','2022121211','2022121213','2022121214'); // I thought of using the splicing string method to query the database, so I recorded it sb = (i!=()-1) ?("'" + (i) + "',"):("'" + (i) + "'"); } (sb);//Output string spliced } }
Two different classes can be used to splice strings:
- One is StringBuild
- The other is StringBuffer
When different from the String class, StringBuild and StringBuffer can be modified multiple times without generating new unused objects.
When using the StringBuffer class, the StringBuffer object itself is operated every time instead of generating a new object. Therefore, if you need to modify the string, it is recommended to use StringBuffer.
The StringBuilder class was proposed in Java 5. The biggest difference between it and StringBuffer is that the StringBuilder method is not thread-safe (cannot be accessed synchronously).
Since StringBuilder has speed advantages over StringBuffer, it is recommended to use the StringBuilder class in most cases.
Tripartite expressions use:
If the above example does not use ternary expressions, you can also write this way. The effect is the same. The ternary expression makes the code more concise. It is recommended to use ternary expressions.
Insert code slice here ```import ; /* * Use StringBuilder to splice the desired string * It can be used to splice query conditions during development. For a novice, if you have a better way, please correct me if you have any errors. Thank you very much * */ public class testStringBuilder { public static void main(String[] args) { StringBuilder sb =new StringBuilder(); ArrayList<String> list = new ArrayList<>();//Define a List and add a string (four serial numbers) ("2022121212"); ("2022121211"); ("2022121213"); ("2022121214"); ("The original output list is:"+list);//Output original List for (int i = 0; i <() ; i++) { // sb = (i!=()-1 ?("'" + (i) + "',"):("'" + (i) + "'"); if (i != () - 1) { ("'" + (i) + "',"); } else { ("'" + (i) + "'"); } } (sb);//Output string spliced } } //Trial expression syntax// (Relational expression) ? Expression 1: Expression 2;//The relationship expression is true, then expression 1 is executed, otherwise expression 2 is executed//Output maximum age// int dadAge = 39; // int mamAge = 37; // int max = mamAge > dadAge ? mamAge : dadAge; // ("The oldest is:"+max+"age");
String intercept
String interception uses substring(), which can intercept a certain part of a string. Syntax(from, to)
For example:
public class testSubString { public static void main(String[] args) { //The origin date of intercepting ID card information String idCard="532165200010104361"; ((6,14));//Here 6 corresponds to the eighth bit of the above number (the position at the beginning of the intercept), and 14 corresponds to the 15th bit of the above number (the position at the end of the intercept) //The output is as follows: // 20001010 } }
Summarize
The above is personal experience. I hope you can give you a reference and I hope you can support me more.