Common methods and advanced skills of string spelling in Java, with examples and explanations, plus comments and actual operation effect descriptions
Table of contents
1. Use the "+" operator to concatenate strings
2. Use the StringBuilder class to concatenate strings
3. Use the String.format() method to format the string
4. Use the String.concat() method to concatenate strings
5. Use the String.join() method to join strings
6. Use the StringBuffer class to concatenate strings (thread safety)
7. Use the StringTokenizer class to splice strings
8. Use the Arrays.stream() method to splice string arrays
7. Use the StringUtils class to concatenate strings (need to import the Apache Commons Lang library)
8. Use the StringWriter class to splice strings
9. Use the StringJoiner class to join strings
10. Use the String.repeat() method to copy strings
11. Use the Joiner class of Guava library to splice strings (Guava library needs to be imported)
12. Use the String.toCharArray() method to splice character arrays
13. Use the String.valueOf() method to stitch other types of data
14. Use the StringWriter class and the PrintWriter class to splice strings
15. Use the String.format() method to splice multiple variables
16. Use the reverse() method of the StringBuffer class to concatenate strings in reverse order
17. Use the String.format() method for number formatting
18. Use the String.substring() method to intercept part of the string for splicing
19. Use the String.format() method to insert variables
20. Use the String.valueOf() method to splice basic data types and objects
21. Use the append() method of the StringBuffer class to concatenate strings
22. Use the append() method of the StringBuilder class to concatenate strings
23. Use the StringJoiner class to join strings
25. Use the Joiner class of Guava library to splice strings (Guava library needs to be imported)
26. Use Java 8's Stream API to concatenate strings
27. Use Java 8's StringJoiner class to join strings
28. Use the Collectors.joining() method of Java 8 to join strings
30. Use the String.concat() method to concatenate strings
31. Use the String.format() method to splice multiple variables
32. Use the append() method of the StringBuilder class to concatenate strings
33. Use the append() method of the StringBuffer class to concatenate strings
In Java, there are many common methods and advanced techniques that can be used to concatenate strings. Here are 20 examples, with notes and descriptions of how they actually work:
1. Use the "+" operator to concatenate strings
String str1 = "Hello";
String str2 = "World";
String result = str1 + " " + str2;
System.out.println(result); // 输出: Hello World
2. Use the StringBuilder class to concatenate strings
StringBuilder sb = new StringBuilder();
sb.append("Hello");
sb.append(" ");
sb.append("World");
String result = sb.toString();
System.out.println(result); // 输出: Hello World
3. Use the String.format() method to format the string
String result = String.format("Hello %s", "World");
System.out.println(result); // 输出: Hello World
4. Use the String.concat() method to concatenate strings
String str1 = "Hello";
String str2 = "World";
String result = str1.concat(" ").concat(str2);
System.out.println(result); // 输出: Hello World
5. Use the String.join() method to join strings
String str1 = "Hello";
String str2 = "World";
String result = String.join(" ", str1, str2);
System.out.println(result); // 输出: Hello World
6. Use the StringBuffer class to concatenate strings (thread safety)
StringBuffer sb = new StringBuffer();
sb.append("Hello");
sb.append(" ");
sb.append("World");
String result = sb.toString();
System.out.println(result); // 输出: Hello World
7. Use the StringTokenizer class to splice strings
String str = "Hello,World";
StringTokenizer tokenizer = new StringTokenizer(str, ",");
StringBuilder sb = new StringBuilder();
while (tokenizer.hasMoreTokens()) {
sb.append(tokenizer.nextToken());
sb.append(" ");
}
String result = sb.toString().trim();
System.out.println(result); // 输出: Hello World
8. Use the Arrays.stream() method to splice string arrays
String[] arr = {"Hello", "World"};
String result = Arrays.stream(arr).collect(Collectors.joining(" "));
System.out.println(result); // 输出: Hello World
7. Use the StringUtils class to concatenate strings (need to import the Apache Commons Lang library)
String str1 = "Hello";
String str2 = "World";
String result = StringUtils.join(new String[]{str1, str2}, " ");
System.out.println(result); // 输出: Hello World
8. Use the StringWriter class to splice strings
StringWriter writer = new StringWriter();
writer.append("Hello");
writer.append(" ");
writer.append("World");
String result = writer.toString();
System.out.println(result); // 输出: Hello World
9. Use the StringJoiner class to join strings
StringJoiner joiner = new StringJoiner(" ");
joiner.add("Hello");
joiner.add("World");
String result = joiner.toString();
System.out.println(result); // 输出: Hello World
10. Use the String.repeat() method to copy strings
String str = "Hello ";
String result = str.repeat(3);
System.out.println(result); // 输出: Hello Hello Hello
11. Use the Joiner class of Guava library to splice strings (Guava library needs to be imported)
String str1 = "Hello";
String str2 = "World";
String result = Joiner.on(" ").join(str1, str2);
System.out.println(result); // 输出: Hello World
12. Use the String.toCharArray() method to splice character arrays
char[] arr = {'H', 'e', 'l', 'l', 'o'};
String result = new String(arr);
System.out.println(result); // 输出: Hello
13. Use the String.valueOf() method to stitch other types of data
int num = 42;
String result = "The answer is " + String.valueOf(num);
System.out.println(result); // 输出: The answer is 42
14. Use the StringWriter class and the PrintWriter class to splice strings
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
pw.print("Hello ");
pw.print("World");
String result = sw.toString();
System.out.println(result); // 输出: Hello World
15. Use the String.format() method to splice multiple variables
String name = "Alice";
int age = 25;
String result = String.format("My name is %s and I am %d years old.", name, age);
System.out.println(result); // 输出: My name is Alice and I am 25 years old.
16. Use the reverse() method of the StringBuffer class to concatenate strings in reverse order
String str = "Hello";
StringBuffer sb = new StringBuffer(str);
String result = sb.reverse().toString();
System.out.println(result); // 输出: olleH
17. Use the String.format() method for number formatting
int num = 42;
String result = String.format("The number is %03d", num);
System.out.println(result); // 输出: The number is 042
18. Use the String.substring() method to intercept part of the string for splicing
String str = "Hello World";
String result = str.substring(0, 5) + str.substring(6);
System.out.println(result); // 输出: HelloWorld
19. Use the String.format() method to insert variables
String name = "Alice";
int age = 25;
String result = String.format("My name is %s and I am %d years old.", name, age);
System.out.println(result); // 输出: My name is Alice and I am 25 years old.
20. Use the String.valueOf() method to splice basic data types and objects
int num = 42;
double pi = 3.14159;
String str = "Hello";
boolean flag = true;
String result = String.valueOf(num) + String.valueOf(pi) + str + String.valueOf(flag);
System.out.println(result); // 输出: 42.03.14159Hellotrue
21. Use the append() method of the StringBuffer class to concatenate strings
StringBuffer sb = new StringBuffer();
sb.append("Hello");
sb.append(" ");
sb.append("World");
String result = sb.toString();
System.out.println(result); // 输出: Hello World
22. Use the append() method of the StringBuilder class to concatenate strings
StringBuilder sb = new StringBuilder();
sb.append("Hello");
sb.append(" ");
sb.append("World");
String result = sb.toString();
System.out.println(result); // 输出: Hello World
23. Use the StringJoiner class to join strings
StringJoiner joiner = new StringJoiner(", ");
joiner.add("Apple");
joiner.add("Banana");
joiner.add("Orange");
String result = joiner.toString();
System.out.println(result); // 输出: Apple, Banana, Orange
24. Use the StringUtils class of the Apache Commons Lang library to splice strings (need to import the Apache Commons Lang library)
String[] arr = {"Hello", "World"};
String result = StringUtils.join(arr, " ");
System.out.println(result); // 输出: Hello World
25. Use the Joiner class of Guava library to splice strings (Guava library needs to be imported)
List<String> list = Arrays.asList("Hello", "World");
String result = Joiner.on(" ").join(list);
System.out.println(result); // 输出: Hello World
26. Use Java 8's Stream API to concatenate strings
List<String> list = Arrays.asList("Hello", "World");
String result = list.stream().collect(Collectors.joining(" "));
System.out.println(result); // 输出: Hello World
27. Use Java 8's StringJoiner class to join strings
StringJoiner joiner = new StringJoiner(" ");
joiner.add("Hello");
joiner.add("World");
String result = joiner.toString();
System.out.println(result); // 输出: Hello World
28. Use the Collectors.joining() method of Java 8 to join strings
List<String> list = Arrays.asList("Hello", "World");
String result = list.stream().collect(Collectors.joining(" "));
System.out.println(result); // 输出: Hello World
29. Use the join() method of the StringUtils class to splice strings (need to import the Apache Commons Lang library)
String[] arr = {"Hello", "World"};
String result = StringUtils.join(arr, " ");
System.out.println(result); // 输出: Hello World
30. Use the String.concat() method to concatenate strings
String str1 = "Hello";
String str2 = "World";
String result = str1.concat(" ").concat(str2);
System.out.println(result); // 输出: Hello World
31. Use the String.format() method to splice multiple variables
String name = "Alice";
int age = 25;
String result = String.format("My name is %s and I am %d years old.", name, age);
System.out.println(result); // 输出: My name is Alice and I am 25 years old.
32. Use the append() method of the StringBuilder class to concatenate strings
StringBuilder sb = new StringBuilder();
sb.append("Hello");
sb.append(" ");
sb.append("World");
String result = sb.toString();
System.out.println(result); // 输出: Hello World
33. Use the append() method of the StringBuffer class to concatenate strings
StringBuffer sb = new StringBuffer();
sb.append("Hello");
sb.append(" ");
sb.append("World");
String result = sb.toString();
System.out.println(result); // 输出: Hello World