Move articles out of java-strings part1
This commit is contained in:
@@ -3,10 +3,7 @@
|
||||
This module contains articles about operations on strings in Java.
|
||||
|
||||
### Relevant Articles:
|
||||
- [Convert char to String in Java](https://www.baeldung.com/java-convert-char-to-string)
|
||||
- [Convert String to int or Integer in Java](https://www.baeldung.com/java-convert-string-to-int-or-integer)
|
||||
- [Java String Conversions](https://www.baeldung.com/java-string-conversions)
|
||||
- [Check if a String is a Palindrome](https://www.baeldung.com/java-palindrome)
|
||||
- [Check If a String Is a Palindrome](https://www.baeldung.com/java-palindrome)
|
||||
- [Comparing Strings in Java](https://www.baeldung.com/java-compare-strings)
|
||||
- [Check If a String Is Numeric in Java](https://www.baeldung.com/java-check-string-number)
|
||||
- [Get Substring from String in Java](https://www.baeldung.com/java-substring)
|
||||
@@ -16,7 +13,6 @@ This module contains articles about operations on strings in Java.
|
||||
- [Guide to Java String Pool](https://www.baeldung.com/java-string-pool)
|
||||
- [Split a String in Java](https://www.baeldung.com/java-split-string)
|
||||
- [Common String Operations in Java](https://www.baeldung.com/java-string-operations)
|
||||
- [Convert String to Byte Array and Reverse in Java](https://www.baeldung.com/java-string-to-byte-array)
|
||||
- [Java toString() Method](https://www.baeldung.com/java-tostring)
|
||||
- [CharSequence vs. String in Java](https://www.baeldung.com/java-char-sequence-string)
|
||||
- [StringBuilder and StringBuffer in Java](https://www.baeldung.com/java-string-builder-string-buffer)
|
||||
|
||||
@@ -1,11 +0,0 @@
|
||||
package com.baeldung.datetime;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
public class UseLocalDateTime {
|
||||
|
||||
public LocalDateTime getLocalDateTimeUsingParseMethod(String representation) {
|
||||
return LocalDateTime.parse(representation);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,53 +0,0 @@
|
||||
package com.baeldung;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class CharToStringUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenChar_whenCallingStringValueOf_shouldConvertToString() {
|
||||
final char givenChar = 'x';
|
||||
|
||||
final String result = String.valueOf(givenChar);
|
||||
|
||||
assertThat(result).isEqualTo("x");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenChar_whenCallingToStringOnCharacter_shouldConvertToString() {
|
||||
final char givenChar = 'x';
|
||||
|
||||
final String result = Character.toString(givenChar);
|
||||
|
||||
assertThat(result).isEqualTo("x");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenChar_whenCallingCharacterConstructor_shouldConvertToString3() {
|
||||
final char givenChar = 'x';
|
||||
|
||||
final String result = new Character(givenChar).toString();
|
||||
|
||||
assertThat(result).isEqualTo("x");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenChar_whenConcatenated_shouldConvertToString4() {
|
||||
final char givenChar = 'x';
|
||||
|
||||
final String result = givenChar + "";
|
||||
|
||||
assertThat(result).isEqualTo("x");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenChar_whenFormated_shouldConvertToString5() {
|
||||
final char givenChar = 'x';
|
||||
|
||||
final String result = String.format("%c", givenChar);
|
||||
|
||||
assertThat(result).isEqualTo("x");
|
||||
}
|
||||
}
|
||||
@@ -1,2 +0,0 @@
|
||||
Relevant Articles:
|
||||
- [Java String Conversions](http://www.baeldung.com/java-string-conversions)
|
||||
@@ -1,128 +0,0 @@
|
||||
package com.baeldung.java.conversion;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import java.text.ParseException;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.Month;
|
||||
import java.util.Arrays;
|
||||
import java.util.Calendar;
|
||||
import java.util.Date;
|
||||
import java.util.GregorianCalendar;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import com.baeldung.datetime.UseLocalDateTime;
|
||||
|
||||
public class StringConversionUnitTest {
|
||||
|
||||
@Test
|
||||
public void whenConvertedToInt_thenCorrect() {
|
||||
String beforeConvStr = "1";
|
||||
int afterConvInt = 1;
|
||||
|
||||
assertEquals(Integer.parseInt(beforeConvStr), afterConvInt);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenConvertedToInteger_thenCorrect() {
|
||||
String beforeConvStr = "12";
|
||||
Integer afterConvInteger = 12;
|
||||
|
||||
assertEquals(Integer.valueOf(beforeConvStr).equals(afterConvInteger), true);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenConvertedTolong_thenCorrect() {
|
||||
String beforeConvStr = "12345";
|
||||
long afterConvLongPrimitive = 12345;
|
||||
|
||||
assertEquals(Long.parseLong(beforeConvStr), afterConvLongPrimitive);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenConvertedToLong_thenCorrect() {
|
||||
String beforeConvStr = "14567";
|
||||
Long afterConvLong = 14567l;
|
||||
|
||||
assertEquals(Long.valueOf(beforeConvStr).equals(afterConvLong), true);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenConvertedTodouble_thenCorrect() {
|
||||
String beforeConvStr = "1.4";
|
||||
double afterConvDoublePrimitive = 1.4;
|
||||
|
||||
assertEquals(Double.parseDouble(beforeConvStr), afterConvDoublePrimitive, 0.0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenConvertedToDouble_thenCorrect() {
|
||||
String beforeConvStr = "145.67";
|
||||
double afterConvDouble = 145.67d;
|
||||
|
||||
assertEquals(Double.valueOf(beforeConvStr).equals(afterConvDouble), true);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenConvertedToByteArr_thenCorrect() {
|
||||
String beforeConvStr = "abc";
|
||||
byte[] afterConvByteArr = new byte[] { 'a', 'b', 'c' };
|
||||
|
||||
assertEquals(Arrays.equals(beforeConvStr.getBytes(), afterConvByteArr), true);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenConvertedToboolean_thenCorrect() {
|
||||
String beforeConvStr = "true";
|
||||
boolean afterConvBooleanPrimitive = true;
|
||||
|
||||
assertEquals(Boolean.parseBoolean(beforeConvStr), afterConvBooleanPrimitive);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenConvertedToBoolean_thenCorrect() {
|
||||
String beforeConvStr = "true";
|
||||
Boolean afterConvBoolean = true;
|
||||
|
||||
assertEquals(Boolean.valueOf(beforeConvStr), afterConvBoolean);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenConvertedToCharArr_thenCorrect() {
|
||||
String beforeConvStr = "hello";
|
||||
char[] afterConvCharArr = { 'h', 'e', 'l', 'l', 'o' };
|
||||
|
||||
assertEquals(Arrays.equals(beforeConvStr.toCharArray(), afterConvCharArr), true);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenConvertedToDate_thenCorrect() throws ParseException {
|
||||
String beforeConvStr = "15/10/2013";
|
||||
int afterConvCalendarDay = 15;
|
||||
int afterConvCalendarMonth = 9;
|
||||
int afterConvCalendarYear = 2013;
|
||||
SimpleDateFormat formatter = new SimpleDateFormat("dd/M/yyyy");
|
||||
Date afterConvDate = formatter.parse(beforeConvStr);
|
||||
Calendar calendar = new GregorianCalendar();
|
||||
calendar.setTime(afterConvDate);
|
||||
|
||||
assertEquals(calendar.get(Calendar.DAY_OF_MONTH), afterConvCalendarDay);
|
||||
assertEquals(calendar.get(Calendar.MONTH), afterConvCalendarMonth);
|
||||
assertEquals(calendar.get(Calendar.YEAR), afterConvCalendarYear);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenConvertedToLocalDateTime_thenCorrect() {
|
||||
String str = "2007-12-03T10:15:30";
|
||||
int afterConvCalendarDay = 03;
|
||||
Month afterConvCalendarMonth = Month.DECEMBER;
|
||||
int afterConvCalendarYear = 2007;
|
||||
LocalDateTime afterConvDate = new UseLocalDateTime().getLocalDateTimeUsingParseMethod(str);
|
||||
|
||||
assertEquals(afterConvDate.getDayOfMonth(), afterConvCalendarDay);
|
||||
assertEquals(afterConvDate.getMonth(), afterConvCalendarMonth);
|
||||
assertEquals(afterConvDate.getYear(), afterConvCalendarYear);
|
||||
}
|
||||
}
|
||||
@@ -1,90 +0,0 @@
|
||||
package com.baeldung.string.conversion;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.charset.CharacterCodingException;
|
||||
import java.nio.charset.Charset;
|
||||
import java.nio.charset.CharsetDecoder;
|
||||
import java.nio.charset.CodingErrorAction;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class ByteArrayToStringUnitTest {
|
||||
|
||||
@Test
|
||||
public void whenStringConstructorWithDefaultCharset_thenOK() {
|
||||
final byte[] byteArrray = { 72, 101, 108, 108, 111, 32, 87, 111, 114,
|
||||
108, 100, 33 };
|
||||
String string = new String(byteArrray);
|
||||
System.out.println(string);
|
||||
|
||||
assertNotNull(string);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenStringConstructorWithNamedCharset_thenOK()
|
||||
throws UnsupportedEncodingException {
|
||||
final String charsetName = "IBM01140";
|
||||
final byte[] byteArrray = { -56, -123, -109, -109, -106, 64, -26, -106,
|
||||
-103, -109, -124, 90 };
|
||||
|
||||
String string = new String(byteArrray, charsetName);
|
||||
|
||||
assertEquals("Hello World!", string);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenStringConstructorWithCharSet_thenOK() {
|
||||
final Charset charset = Charset.forName("UTF-8");
|
||||
final byte[] byteArrray = { 72, 101, 108, 108, 111, 32, 87, 111, 114,
|
||||
108, 100, 33 };
|
||||
|
||||
String string = new String(byteArrray, charset);
|
||||
|
||||
assertEquals("Hello World!", string);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenStringConstructorWithStandardCharSet_thenOK() {
|
||||
final Charset charset = StandardCharsets.UTF_16;
|
||||
|
||||
final byte[] byteArrray = { -2, -1, 0, 72, 0, 101, 0, 108, 0, 108, 0,
|
||||
111, 0, 32, 0, 87, 0, 111, 0, 114, 0, 108, 0, 100, 0, 33 };
|
||||
|
||||
String string = new String(byteArrray, charset);
|
||||
|
||||
assertEquals("Hello World!", string);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenDecodeWithCharset_thenOK() {
|
||||
byte[] byteArrray = { 72, 101, 108, 108, 111, 32, -10, 111, 114, 108, -63, 33 };
|
||||
final Charset charset = StandardCharsets.US_ASCII;
|
||||
|
||||
String string = charset.decode(ByteBuffer.wrap(byteArrray)).toString();
|
||||
System.out.println(string);
|
||||
|
||||
assertEquals("Hello <20>orl<72>!", string);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenUsingCharsetDecoder_thenOK()
|
||||
throws CharacterCodingException {
|
||||
byte[] byteArrray = { 72, 101, 108, 108, 111, 32, -10, 111, 114, 108, -63, 33};
|
||||
CharsetDecoder decoder = StandardCharsets.US_ASCII.newDecoder();
|
||||
|
||||
decoder.onMalformedInput(CodingErrorAction.REPLACE)
|
||||
.onUnmappableCharacter(CodingErrorAction.REPLACE)
|
||||
.replaceWith("?");
|
||||
|
||||
String string = decoder.decode(ByteBuffer.wrap(byteArrray)).toString();
|
||||
|
||||
assertEquals("Hello ?orl?!", string);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -1,122 +0,0 @@
|
||||
package com.baeldung.string.conversion;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.nio.CharBuffer;
|
||||
import java.nio.charset.CharacterCodingException;
|
||||
import java.nio.charset.Charset;
|
||||
import java.nio.charset.CharsetEncoder;
|
||||
import java.nio.charset.CodingErrorAction;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Arrays;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class StringToByteArrayUnitTest {
|
||||
|
||||
@Test
|
||||
public void whenGetBytesWithDefaultCharset_thenOK() {
|
||||
final String inputString = "Hello World!";
|
||||
final String defaultCharSet = Charset.defaultCharset()
|
||||
.displayName();
|
||||
|
||||
byte[] byteArrray = inputString.getBytes();
|
||||
|
||||
System.out.printf(
|
||||
"Using default charset:%s, Input String:%s, Output byte array:%s\n",
|
||||
defaultCharSet, inputString, Arrays.toString(byteArrray));
|
||||
|
||||
assertNotNull(byteArrray);
|
||||
assert (byteArrray.length >= inputString.length());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenGetBytesWithNamedCharset_thenOK()
|
||||
throws UnsupportedEncodingException {
|
||||
final String inputString = "Hello World!";
|
||||
final String charsetName = "IBM01140";
|
||||
|
||||
byte[] byteArrray = inputString.getBytes("IBM01140");
|
||||
|
||||
System.out.printf(
|
||||
"Using named charset:%s, Input String:%s, Output byte array:%s\n",
|
||||
charsetName, inputString, Arrays.toString(byteArrray));
|
||||
|
||||
assertArrayEquals(new byte[] { -56, -123, -109, -109, -106, 64, -26,
|
||||
-106, -103, -109, -124, 90 },
|
||||
byteArrray);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenGetBytesWithCharset_thenOK() {
|
||||
final String inputString = "Hello ਸੰਸਾਰ!";
|
||||
final Charset charset = Charset.forName("ASCII");
|
||||
|
||||
byte[] byteArrray = inputString.getBytes(charset);
|
||||
|
||||
System.out.printf(
|
||||
"Using Charset:%s, Input String:%s, Output byte array:%s\n",
|
||||
charset, inputString, Arrays.toString(byteArrray));
|
||||
|
||||
assertArrayEquals(
|
||||
new byte[] { 72, 101, 108, 108, 111, 32, 63, 63, 63, 63, 63, 33 },
|
||||
byteArrray);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenGetBytesWithStandardCharset_thenOK() {
|
||||
final String inputString = "Hello World!";
|
||||
final Charset charset = StandardCharsets.UTF_16;
|
||||
|
||||
byte[] byteArrray = inputString.getBytes(charset);
|
||||
|
||||
System.out.printf(
|
||||
"Using Standard Charset:%s, Input String:%s, Output byte array:%s\n",
|
||||
charset, inputString, Arrays.toString(byteArrray));
|
||||
|
||||
assertArrayEquals(new byte[] { -2, -1, 0, 72, 0, 101, 0, 108, 0, 108, 0,
|
||||
111, 0, 32, 0, 87, 0, 111, 0, 114, 0, 108, 0, 100, 0, 33 },
|
||||
byteArrray);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenEncodeWithCharset_thenOK() {
|
||||
final String inputString = "Hello ਸੰਸਾਰ!";
|
||||
final Charset charset = StandardCharsets.US_ASCII;
|
||||
|
||||
byte[] byteArrray = charset.encode(inputString)
|
||||
.array();
|
||||
|
||||
System.out.printf(
|
||||
"Using encode with Charset:%s, Input String:%s, Output byte array:%s\n",
|
||||
charset, inputString, Arrays.toString(byteArrray));
|
||||
|
||||
assertArrayEquals(
|
||||
new byte[] { 72, 101, 108, 108, 111, 32, 63, 63, 63, 63, 63, 33 },
|
||||
byteArrray);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenUsingCharsetEncoder_thenOK()
|
||||
throws CharacterCodingException {
|
||||
final String inputString = "Hello ਸੰਸਾਰ!";
|
||||
CharsetEncoder encoder = StandardCharsets.US_ASCII.newEncoder();
|
||||
encoder.onMalformedInput(CodingErrorAction.IGNORE)
|
||||
.onUnmappableCharacter(CodingErrorAction.REPLACE)
|
||||
.replaceWith(new byte[] { 0 });
|
||||
|
||||
byte[] byteArrray = encoder.encode(CharBuffer.wrap(inputString))
|
||||
.array();
|
||||
|
||||
System.out.printf(
|
||||
"Using encode with CharsetEncoder:%s, Input String:%s, Output byte array:%s\n",
|
||||
encoder, inputString, Arrays.toString(byteArrray));
|
||||
|
||||
assertArrayEquals(
|
||||
new byte[] { 72, 101, 108, 108, 111, 32, 0, 0, 0, 0, 0, 33 },
|
||||
byteArrray);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user