Skip to content

Commit ab38058

Browse files
committed
更新 Java 字符串相关的内容和使用的版本
1 parent 31d47be commit ab38058

40 files changed

Lines changed: 1112 additions & 0 deletions
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
## Java Strings
2+
3+
This module contains articles about strings in Java.
4+
5+
### Relevant Articles:
6+
- [Use char[] Array over a String for Manipulating Passwords in Java?](https://www.baeldung.com/java-storing-passwords)
7+
- [Compact Strings in Java 9](https://www.baeldung.com/java-9-compact-string)
8+
- [String Not Empty Test Assertions in Java](https://www.baeldung.com/java-assert-string-not-empty)
9+
- [String Performance Hints](https://www.baeldung.com/java-string-performance)
10+
- [Java Localization – Formatting Messages](https://www.baeldung.com/java-localization-messages-formatting)
11+
- [Java – Generate Random String](https://www.baeldung.com/java-random-string)
12+
- [Java String Interview Questions and Answers](https://www.baeldung.com/java-string-interview-questions)
13+
- [Java Multi-line String](https://www.baeldung.com/java-multiline-string)
14+
- [Guide to Java String Pool](https://www.baeldung.com/java-string-pool)
15+
- [Fixing “constant string too long” Build Error](https://www.baeldung.com/java-constant-string-too-long-error)
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.baeldung.java9.compactstring;
2+
3+
import java.util.List;
4+
import java.util.stream.IntStream;
5+
6+
import static java.util.stream.Collectors.toList;
7+
8+
public class CompactStringDemo {
9+
10+
public static void main(String[] args) {
11+
long startTime = System.currentTimeMillis();
12+
List strings = IntStream.rangeClosed(1, 10_000_000)
13+
.mapToObj(Integer::toString).collect(toList());
14+
long totalTime = System.currentTimeMillis() - startTime;
15+
System.out.println("Generated " + strings.size() + " strings in "
16+
+ totalTime + " ms.");
17+
18+
startTime = System.currentTimeMillis();
19+
String appended = (String) strings.stream().limit(100_000)
20+
.reduce("", (left, right) -> left.toString() + right.toString());
21+
totalTime = System.currentTimeMillis() - startTime;
22+
System.out.println("Created string of length " + appended.length()
23+
+ " in " + totalTime + " ms.");
24+
}
25+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.baeldung.localization;
2+
3+
import java.text.ParseException;
4+
import java.util.Arrays;
5+
import java.util.List;
6+
import java.util.Locale;
7+
8+
public class App {
9+
10+
/**
11+
* Runs all available formatter
12+
* @throws ParseException
13+
*/
14+
public static void main(String[] args) {
15+
List<Locale> locales = Arrays.asList(new Locale[] { Locale.UK, Locale.ITALY, Locale.FRANCE, Locale.forLanguageTag("pl-PL") });
16+
Localization.run(locales);
17+
JavaSEFormat.run(locales);
18+
ICUFormat.run(locales);
19+
}
20+
21+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.baeldung.localization;
2+
3+
import com.ibm.icu.text.MessageFormat;
4+
5+
import java.util.List;
6+
import java.util.Locale;
7+
import java.util.ResourceBundle;
8+
9+
public class ICUFormat {
10+
11+
public static String getLabel(Locale locale, Object[] data) {
12+
ResourceBundle bundle = ResourceBundle.getBundle("formats", locale);
13+
String format = bundle.getString("label-icu");
14+
MessageFormat formatter = new MessageFormat(format, locale);
15+
return formatter.format(data);
16+
}
17+
18+
public static void run(List<Locale> locales) {
19+
System.out.println("ICU formatter");
20+
locales.forEach(locale -> System.out.println(getLabel(locale, new Object[] { "Alice", "female", 0 })));
21+
locales.forEach(locale -> System.out.println(getLabel(locale, new Object[] { "Alice", "female", 1 })));
22+
locales.forEach(locale -> System.out.println(getLabel(locale, new Object[] { "Alice", "female", 2 })));
23+
locales.forEach(locale -> System.out.println(getLabel(locale, new Object[] { "Alice", "female", 3 })));
24+
locales.forEach(locale -> System.out.println(getLabel(locale, new Object[] { "Bob", "male", 0 })));
25+
locales.forEach(locale -> System.out.println(getLabel(locale, new Object[] { "Bob", "male", 1 })));
26+
locales.forEach(locale -> System.out.println(getLabel(locale, new Object[] { "Bob", "male", 2 })));
27+
locales.forEach(locale -> System.out.println(getLabel(locale, new Object[] { "Bob", "male", 3 })));
28+
}
29+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.baeldung.localization;
2+
3+
import java.text.MessageFormat;
4+
import java.util.Date;
5+
import java.util.List;
6+
import java.util.Locale;
7+
import java.util.ResourceBundle;
8+
9+
public class JavaSEFormat {
10+
11+
public static String getLabel(Locale locale, Object[] data) {
12+
ResourceBundle bundle = ResourceBundle.getBundle("formats", locale);
13+
final String pattern = bundle.getString("label");
14+
final MessageFormat formatter = new MessageFormat(pattern, locale);
15+
return formatter.format(data);
16+
}
17+
18+
public static void run(List<Locale> locales) {
19+
System.out.println("Java formatter");
20+
final Date date = new Date(System.currentTimeMillis());
21+
locales.forEach(locale -> System.out.println(getLabel(locale, new Object[] { date, "Alice", 0 })));
22+
locales.forEach(locale -> System.out.println(getLabel(locale, new Object[] { date, "Alice", 2 })));
23+
}
24+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.baeldung.localization;
2+
3+
import java.util.List;
4+
import java.util.Locale;
5+
import java.util.ResourceBundle;
6+
7+
public class Localization {
8+
9+
public static String getLabel(Locale locale) {
10+
final ResourceBundle bundle = ResourceBundle.getBundle("messages", locale);
11+
return bundle.getString("label");
12+
}
13+
14+
public static void run(List<Locale> locales) {
15+
locales.forEach(locale -> System.out.println(getLabel(locale)));
16+
}
17+
18+
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package com.baeldung.multiline;
2+
3+
import com.google.common.base.Joiner;
4+
import com.google.common.collect.ImmutableList;
5+
6+
import java.io.IOException;
7+
import java.io.PrintWriter;
8+
import java.io.StringWriter;
9+
import java.nio.file.Files;
10+
import java.nio.file.Paths;
11+
12+
public class MultiLineString {
13+
14+
String newLine = System.getProperty("line.separator");
15+
16+
public String stringConcatenation() {
17+
return "Get busy living"
18+
.concat(newLine)
19+
.concat("or")
20+
.concat(newLine)
21+
.concat("get busy dying.")
22+
.concat(newLine)
23+
.concat("--Stephen King");
24+
}
25+
26+
public String stringJoin() {
27+
return String.join(newLine,
28+
"Get busy living",
29+
"or",
30+
"get busy dying.",
31+
"--Stephen King");
32+
}
33+
34+
public String stringBuilder() {
35+
return new StringBuilder()
36+
.append("Get busy living")
37+
.append(newLine)
38+
.append("or")
39+
.append(newLine)
40+
.append("get busy dying.")
41+
.append(newLine)
42+
.append("--Stephen King")
43+
.toString();
44+
}
45+
46+
public String stringWriter() {
47+
StringWriter stringWriter = new StringWriter();
48+
PrintWriter printWriter = new PrintWriter(stringWriter);
49+
printWriter.println("Get busy living");
50+
printWriter.println("or");
51+
printWriter.println("get busy dying.");
52+
printWriter.println("--Stephen King");
53+
return stringWriter.toString();
54+
}
55+
56+
public String guavaJoiner() {
57+
return Joiner.on(newLine).join(ImmutableList.of("Get busy living",
58+
"or",
59+
"get busy dying.",
60+
"--Stephen King"));
61+
}
62+
63+
public String loadFromFile() throws IOException {
64+
return new String(Files.readAllBytes(Paths.get("src/main/resources/stephenking.txt")));
65+
}
66+
67+
68+
// public String textBlocks() {
69+
//
70+
// // THIS ONLY FOR JDK 15
71+
// return """
72+
// Get busy living
73+
// or
74+
// get busy dying.
75+
// --Stephen King""";
76+
// }
77+
}

0 commit comments

Comments
 (0)