JAVA-22238 | moved articles into correct locations. (#14297)
This commit is contained in:
committed by
GitHub
parent
b4a8dfbbff
commit
171fdc7b7e
@@ -1,16 +1,16 @@
|
||||
## Java Strings
|
||||
|
||||
This module contains articles about strings in Java.
|
||||
This is a generic module contains articles about strings in Java.
|
||||
Listed here there are only those articles that does not fit into other core-java-string-* modules as:
|
||||
- [core-java-string-operations](../core-java-string-operations)
|
||||
- [core-java-string-apis](../core-java-string-apis)
|
||||
- [core-java-string-conversion](../core-java-string-conversions)
|
||||
- [core-java-string-algorithms](../core-java-string-algorithms)
|
||||
|
||||
### Relevant Articles:
|
||||
- [Use char[] Array Over a String for Manipulating Passwords in Java?](https://www.baeldung.com/java-storing-passwords)
|
||||
- [Compact Strings in Java 9](https://www.baeldung.com/java-9-compact-string)
|
||||
- [String Not Empty Test Assertions in Java](https://www.baeldung.com/java-assert-string-not-empty)
|
||||
- [String Performance Hints](https://www.baeldung.com/java-string-performance)
|
||||
- [Java Localization – Formatting Messages](https://www.baeldung.com/java-localization-messages-formatting)
|
||||
- [Java – Generate Random String](https://www.baeldung.com/java-random-string)
|
||||
- [Java String Interview Questions and Answers](https://www.baeldung.com/java-string-interview-questions)
|
||||
- [Java Multi-line String](https://www.baeldung.com/java-multiline-string)
|
||||
- [Guide to Java String Pool](https://www.baeldung.com/java-string-pool)
|
||||
- [Fixing “constant string too long” Build Error](https://www.baeldung.com/java-constant-string-too-long-error)
|
||||
- [Reuse StringBuilder for Efficiency](https://www.baeldung.com/java-reuse-stringbuilder-for-efficiency)
|
||||
|
||||
@@ -29,11 +29,6 @@
|
||||
<artifactId>jmh-generator-annprocess</artifactId>
|
||||
<version>${jmh-generator.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.ibm.icu</groupId>
|
||||
<artifactId>icu4j</artifactId>
|
||||
<version>${icu4j.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
@@ -47,7 +42,6 @@
|
||||
</build>
|
||||
|
||||
<properties>
|
||||
<icu4j.version>61.1</icu4j.version>
|
||||
<maven.compiler.release>15</maven.compiler.release>
|
||||
</properties>
|
||||
|
||||
|
||||
@@ -1,25 +0,0 @@
|
||||
package com.baeldung.java9.compactstring;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.stream.IntStream;
|
||||
|
||||
import static java.util.stream.Collectors.toList;
|
||||
|
||||
public class CompactStringDemo {
|
||||
|
||||
public static void main(String[] args) {
|
||||
long startTime = System.currentTimeMillis();
|
||||
List strings = IntStream.rangeClosed(1, 10_000_000)
|
||||
.mapToObj(Integer::toString).collect(toList());
|
||||
long totalTime = System.currentTimeMillis() - startTime;
|
||||
System.out.println("Generated " + strings.size() + " strings in "
|
||||
+ totalTime + " ms.");
|
||||
|
||||
startTime = System.currentTimeMillis();
|
||||
String appended = (String) strings.stream().limit(100_000)
|
||||
.reduce("", (left, right) -> left.toString() + right.toString());
|
||||
totalTime = System.currentTimeMillis() - startTime;
|
||||
System.out.println("Created string of length " + appended.length()
|
||||
+ " in " + totalTime + " ms.");
|
||||
}
|
||||
}
|
||||
@@ -1,21 +0,0 @@
|
||||
package com.baeldung.localization;
|
||||
|
||||
import java.text.ParseException;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
|
||||
public class App {
|
||||
|
||||
/**
|
||||
* Runs all available formatter
|
||||
* @throws ParseException
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
List<Locale> locales = Arrays.asList(new Locale[] { Locale.UK, Locale.ITALY, Locale.FRANCE, Locale.forLanguageTag("pl-PL") });
|
||||
Localization.run(locales);
|
||||
JavaSEFormat.run(locales);
|
||||
ICUFormat.run(locales);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,29 +0,0 @@
|
||||
package com.baeldung.localization;
|
||||
|
||||
import com.ibm.icu.text.MessageFormat;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.ResourceBundle;
|
||||
|
||||
public class ICUFormat {
|
||||
|
||||
public static String getLabel(Locale locale, Object[] data) {
|
||||
ResourceBundle bundle = ResourceBundle.getBundle("formats", locale);
|
||||
String format = bundle.getString("label-icu");
|
||||
MessageFormat formatter = new MessageFormat(format, locale);
|
||||
return formatter.format(data);
|
||||
}
|
||||
|
||||
public static void run(List<Locale> locales) {
|
||||
System.out.println("ICU formatter");
|
||||
locales.forEach(locale -> System.out.println(getLabel(locale, new Object[] { "Alice", "female", 0 })));
|
||||
locales.forEach(locale -> System.out.println(getLabel(locale, new Object[] { "Alice", "female", 1 })));
|
||||
locales.forEach(locale -> System.out.println(getLabel(locale, new Object[] { "Alice", "female", 2 })));
|
||||
locales.forEach(locale -> System.out.println(getLabel(locale, new Object[] { "Alice", "female", 3 })));
|
||||
locales.forEach(locale -> System.out.println(getLabel(locale, new Object[] { "Bob", "male", 0 })));
|
||||
locales.forEach(locale -> System.out.println(getLabel(locale, new Object[] { "Bob", "male", 1 })));
|
||||
locales.forEach(locale -> System.out.println(getLabel(locale, new Object[] { "Bob", "male", 2 })));
|
||||
locales.forEach(locale -> System.out.println(getLabel(locale, new Object[] { "Bob", "male", 3 })));
|
||||
}
|
||||
}
|
||||
@@ -1,24 +0,0 @@
|
||||
package com.baeldung.localization;
|
||||
|
||||
import java.text.MessageFormat;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.ResourceBundle;
|
||||
|
||||
public class JavaSEFormat {
|
||||
|
||||
public static String getLabel(Locale locale, Object[] data) {
|
||||
ResourceBundle bundle = ResourceBundle.getBundle("formats", locale);
|
||||
final String pattern = bundle.getString("label");
|
||||
final MessageFormat formatter = new MessageFormat(pattern, locale);
|
||||
return formatter.format(data);
|
||||
}
|
||||
|
||||
public static void run(List<Locale> locales) {
|
||||
System.out.println("Java formatter");
|
||||
final Date date = new Date(System.currentTimeMillis());
|
||||
locales.forEach(locale -> System.out.println(getLabel(locale, new Object[] { date, "Alice", 0 })));
|
||||
locales.forEach(locale -> System.out.println(getLabel(locale, new Object[] { date, "Alice", 2 })));
|
||||
}
|
||||
}
|
||||
@@ -1,18 +0,0 @@
|
||||
package com.baeldung.localization;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.ResourceBundle;
|
||||
|
||||
public class Localization {
|
||||
|
||||
public static String getLabel(Locale locale) {
|
||||
final ResourceBundle bundle = ResourceBundle.getBundle("messages", locale);
|
||||
return bundle.getString("label");
|
||||
}
|
||||
|
||||
public static void run(List<Locale> locales) {
|
||||
locales.forEach(locale -> System.out.println(getLabel(locale)));
|
||||
}
|
||||
|
||||
}
|
||||
File diff suppressed because one or more lines are too long
@@ -1,2 +0,0 @@
|
||||
label=On {0, date, short} {1} has sent you {2, choice, 0#no messages|1#a message|2#two messages|2<{2,number,integer} messages}.
|
||||
label-icu={0} has sent you {2, plural, =0 {no messages} =1 {a message} other {{2, number, integer} messages}}.
|
||||
@@ -1,2 +0,0 @@
|
||||
label={0, date, short}, {1}{2, choice, 0# ne|0<} vous a envoy\u00e9 {2, choice, 0#aucun message|1#un message|2#deux messages|2<{2,number,integer} messages}.
|
||||
label-icu={0} {2, plural, =0 {ne } other {}}vous a envoy\u00e9 {2, plural, =0 {aucun message} =1 {un message} other {{2, number, integer} messages}}.
|
||||
@@ -1,2 +0,0 @@
|
||||
label={0, date, short} {1} ti ha inviato {2, choice, 0#nessun messagio|1#un messaggio|2#due messaggi|2<{2, number, integer} messaggi}.
|
||||
label-icu={0} {2, plural, =0 {non } other {}}ti ha inviato {2, plural, =0 {nessun messaggio} =1 {un messaggio} other {{2, number, integer} messaggi}}.
|
||||
@@ -1,2 +0,0 @@
|
||||
label=W {0, date, short} {1}{2, choice, 0# nie|0<} wys\u0142a\u0142a ci {2, choice, 0#\u017Cadnych wiadomo\u015Bci|1#wiadomo\u015B\u0107|2#dwie wiadomo\u015Bci|2<{2, number, integer} wiadomo\u015Bci}.
|
||||
label-icu={0} {2, plural, =0 {nie } other {}}{1, select, male {wys\u0142a\u0142} female {wys\u0142a\u0142a} other {wys\u0142a\u0142o}} ci {2, plural, =0 {\u017Cadnej wiadomo\u015Bci} =1 {wiadomo\u015B\u0107} other {{2, number, integer} wiadomo\u015Bci}}.
|
||||
@@ -1 +0,0 @@
|
||||
label=Alice has sent you a message.
|
||||
@@ -1 +0,0 @@
|
||||
label=Alice vous a envoy\u00e9 un message.
|
||||
@@ -1 +0,0 @@
|
||||
label=Alice ti ha inviato un messaggio.
|
||||
@@ -1 +0,0 @@
|
||||
label=Alice wys\u0142a\u0142a ci wiadomo\u015B\u0107.
|
||||
@@ -1,72 +0,0 @@
|
||||
package com.baeldung.localization;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.Locale;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
public class ICUFormatUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenInUK_whenAliceSendsNothing_thenCorrectMessage() {
|
||||
assertEquals("Alice has sent you no messages.", ICUFormat.getLabel(Locale.UK, new Object[] { "Alice", "female", 0 }));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenInUK_whenAliceSendsOneMessage_thenCorrectMessage() {
|
||||
assertEquals("Alice has sent you a message.", ICUFormat.getLabel(Locale.UK, new Object[] { "Alice", "female", 1 }));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenInUK_whenBobSendsSixMessages_thenCorrectMessage() {
|
||||
assertEquals("Bob has sent you 6 messages.", ICUFormat.getLabel(Locale.UK, new Object[] { "Bob", "male", 6 }));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenInItaly_whenAliceSendsNothing_thenCorrectMessage() {
|
||||
assertEquals("Alice non ti ha inviato nessun messaggio.", ICUFormat.getLabel(Locale.ITALY, new Object[] { "Alice", "female", 0 }));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenInItaly_whenAliceSendsOneMessage_thenCorrectMessage() {
|
||||
assertEquals("Alice ti ha inviato un messaggio.", ICUFormat.getLabel(Locale.ITALY, new Object[] { "Alice", "female", 1 }));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenInItaly_whenBobSendsSixMessages_thenCorrectMessage() {
|
||||
assertEquals("Bob ti ha inviato 6 messaggi.", ICUFormat.getLabel(Locale.ITALY, new Object[] { "Bob", "male", 6 }));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenInFrance_whenAliceSendsNothing_thenCorrectMessage() {
|
||||
assertEquals("Alice ne vous a envoyé aucun message.", ICUFormat.getLabel(Locale.FRANCE, new Object[] { "Alice", "female", 0 }));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenInFrance_whenAliceSendsOneMessage_thenCorrectMessage() {
|
||||
assertEquals("Alice vous a envoyé un message.", ICUFormat.getLabel(Locale.FRANCE, new Object[] { "Alice", "female", 1 }));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenInFrance_whenBobSendsSixMessages_thenCorrectMessage() {
|
||||
assertEquals("Bob vous a envoyé 6 messages.", ICUFormat.getLabel(Locale.FRANCE, new Object[] { "Bob", "male", 6 }));
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void givenInPoland_whenAliceSendsNothing_thenCorrectMessage() {
|
||||
assertEquals("Alice nie wysłała ci żadnej wiadomości.", ICUFormat.getLabel(Locale.forLanguageTag("pl-PL"), new Object[] { "Alice", "female", 0 }));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenInPoland_whenAliceSendsOneMessage_thenCorrectMessage() {
|
||||
assertEquals("Alice wysłała ci wiadomość.", ICUFormat.getLabel(Locale.forLanguageTag("pl-PL"), new Object[] { "Alice", "female", 1 }));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenInPoland_whenBobSendsSixMessages_thenCorrectMessage() {
|
||||
assertEquals("Bob wysłał ci 6 wiadomości.", ICUFormat.getLabel(Locale.forLanguageTag("pl-PL"), new Object[] { "Bob", "male", 6 }));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,103 +0,0 @@
|
||||
package com.baeldung.randomstrings;
|
||||
|
||||
import org.apache.commons.lang3.RandomStringUtils;
|
||||
import org.junit.Test;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.nio.charset.Charset;
|
||||
import java.util.Random;
|
||||
|
||||
public class RandomStringsUnitTest {
|
||||
|
||||
private static final Logger LOG = LoggerFactory.getLogger(RandomStringsUnitTest.class);
|
||||
|
||||
@Test
|
||||
public void givenUsingPlainJava_whenGeneratingRandomStringUnbounded_thenCorrect() {
|
||||
byte[] array = new byte[7]; // length is bounded by 7
|
||||
new Random().nextBytes(array);
|
||||
String generatedString = new String(array, Charset.forName("UTF-8"));
|
||||
|
||||
LOG.debug(generatedString);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenUsingPlainJava_whenGeneratingRandomStringBounded_thenCorrect() {
|
||||
int leftLimit = 97; // letter 'a'
|
||||
int rightLimit = 122; // letter 'z'
|
||||
int targetStringLength = 10;
|
||||
Random random = new Random();
|
||||
StringBuilder buffer = new StringBuilder(targetStringLength);
|
||||
|
||||
for (int i = 0; i < targetStringLength; i++) {
|
||||
int randomLimitedInt = leftLimit + (int) (random.nextFloat() * (rightLimit - leftLimit + 1));
|
||||
buffer.append((char) randomLimitedInt);
|
||||
}
|
||||
String generatedString = buffer.toString();
|
||||
|
||||
LOG.debug(generatedString);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenUsingJava8_whenGeneratingRandomAlphabeticString_thenCorrect() {
|
||||
int leftLimit = 97; // letter 'a'
|
||||
int rightLimit = 122; // letter 'z'
|
||||
int targetStringLength = 10;
|
||||
Random random = new Random();
|
||||
|
||||
String generatedString = random.ints(leftLimit, rightLimit + 1)
|
||||
.limit(targetStringLength)
|
||||
.collect(StringBuilder::new, StringBuilder::appendCodePoint, StringBuilder::append)
|
||||
.toString();
|
||||
|
||||
LOG.debug(generatedString);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenUsingJava8_whenGeneratingRandomAlphanumericString_thenCorrect() {
|
||||
int leftLimit = 48; // numeral '0'
|
||||
int rightLimit = 122; // letter 'z'
|
||||
int targetStringLength = 10;
|
||||
Random random = new Random();
|
||||
|
||||
String generatedString = random.ints(leftLimit, rightLimit + 1)
|
||||
.filter(i -> (i <= 57 || i >= 65) && (i <= 90 || i >= 97))
|
||||
.limit(targetStringLength)
|
||||
.collect(StringBuilder::new, StringBuilder::appendCodePoint, StringBuilder::append)
|
||||
.toString();
|
||||
|
||||
LOG.debug(generatedString);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenUsingApache_whenGeneratingRandomString_thenCorrect() {
|
||||
String generatedString = RandomStringUtils.random(10);
|
||||
|
||||
LOG.debug(generatedString);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenUsingApache_whenGeneratingRandomAlphabeticString_thenCorrect() {
|
||||
String generatedString = RandomStringUtils.randomAlphabetic(10);
|
||||
|
||||
LOG.debug(generatedString);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenUsingApache_whenGeneratingRandomAlphanumericString_thenCorrect() {
|
||||
String generatedString = RandomStringUtils.randomAlphanumeric(10);
|
||||
|
||||
LOG.debug(generatedString);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenUsingApache_whenGeneratingRandomStringBounded_thenCorrect() {
|
||||
int length = 10;
|
||||
boolean useLetters = true;
|
||||
boolean useNumbers = false;
|
||||
String generatedString = RandomStringUtils.random(length, useLetters, useNumbers);
|
||||
|
||||
LOG.debug(generatedString);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,44 +0,0 @@
|
||||
package com.baeldung.stringpool;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class StringPoolUnitTest {
|
||||
|
||||
@Test
|
||||
public void whenCreatingConstantStrings_thenTheirAddressesAreEqual() {
|
||||
String constantString1 = "Baeldung";
|
||||
String constantString2 = "Baeldung";
|
||||
|
||||
assertThat(constantString1).isSameAs(constantString2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCreatingStringsWithTheNewOperator_thenTheirAddressesAreDifferent() {
|
||||
String newString1 = new String("Baeldung");
|
||||
String newString2 = new String("Baeldung");
|
||||
|
||||
assertThat(newString1).isNotSameAs(newString2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenComparingConstantAndNewStrings_thenTheirAddressesAreDifferent() {
|
||||
String constantString = "Baeldung";
|
||||
String newString = new String("Baeldung");
|
||||
|
||||
assertThat(constantString).isNotSameAs(newString);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenInterningAStringWithIdenticalValueToAnother_thenTheirAddressesAreEqual() {
|
||||
String constantString = "interned Baeldung";
|
||||
String newString = new String("interned Baeldung");
|
||||
|
||||
assertThat(constantString).isNotSameAs(newString);
|
||||
|
||||
String internedString = newString.intern();
|
||||
|
||||
assertThat(constantString).isSameAs(internedString);
|
||||
}
|
||||
}
|
||||
File diff suppressed because one or more lines are too long
Reference in New Issue
Block a user