BAEL-2392_Java_String_interview_questions second commit (#6064)

This commit is contained in:
ramansahasi 2019-01-03 20:19:51 +05:30 committed by Josh Cummings
parent 829b11e1f3
commit bdcc961542
13 changed files with 275 additions and 0 deletions

View File

@ -0,0 +1,19 @@
package com.baeldung.string.interview;
import java.math.BigDecimal;
import java.text.NumberFormat;
import java.util.Locale;
import static org.junit.Assert.assertEquals;
import org.junit.Test;
public class LocaleUnitTest {
@Test
public void whenUsingLocal_thenCorrectResultsForDifferentLocale() {
Locale usLocale = Locale.US;
BigDecimal number = new BigDecimal(102_300.456d);
NumberFormat usNumberFormat = NumberFormat.getCurrencyInstance(usLocale);
assertEquals(usNumberFormat.format(number), "$102,300.46");
}
}

View File

@ -0,0 +1,29 @@
package com.baeldung.string.interview;
import static org.assertj.core.api.Assertions.assertThat;
import java.util.Arrays;
import org.junit.Test;
public class StringAnagramUnitTest {
public boolean isAnagram(String s1, String s2) {
if(s1.length() != s2.length())
return false;
char[] arr1 = s1.toCharArray();
char[] arr2 = s2.toCharArray();
Arrays.sort(arr1);
Arrays.sort(arr2);
return Arrays.equals(arr1, arr2);
}
@Test
public void whenTestAnagrams_thenTestingCorrectly() {
assertThat(isAnagram("car", "arc")).isTrue();
assertThat(isAnagram("west", "stew")).isTrue();
assertThat(isAnagram("west", "east")).isFalse();
}
}

View File

@ -0,0 +1,20 @@
package com.baeldung.string.interview;
import static org.junit.Assert.assertEquals;
import org.junit.Test;
public class StringChangeCaseUnitTest {
@Test
public void givenString_whenChangingToUppercase_thenCaseChanged() {
String s = "Welcome to Baeldung!";
assertEquals("WELCOME TO BAELDUNG!", s.toUpperCase());
}
@Test
public void givenString_whenChangingToLowerrcase_thenCaseChanged() {
String s = "Welcome to Baeldung!";
assertEquals("welcome to baeldung!", s.toLowerCase());
}
}

View File

@ -0,0 +1,28 @@
package com.baeldung.string.interview;
import static org.junit.Assert.assertEquals;
import org.junit.Test;
public class StringCountOccurrencesUnitTest {
public int countOccurrences(String s, char c) {
int count = 0;
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) == c) {
count++;
}
}
return count;
}
@Test
public void givenString_whenCountingFrequencyOfChar_thenCountCorrect() {
assertEquals(3, countOccurrences("united states", 't'));
}
public void givenString_whenUsingJava8_thenCountingOfCharCorrect() {
String str = "united states";
long count = str.chars().filter(ch -> (char)ch == 't').count();
assertEquals(3, count);
}
}

View File

@ -0,0 +1,14 @@
package com.baeldung.string.interview;
import static org.junit.Assert.assertEquals;
import org.junit.Test;
public class StringFormatUnitTest {
@Test
public void givenString_whenUsingStringFormat_thenStringFormatted() {
String title = "Baeldung";
String formatted = String.format("Title is %s", title);
assertEquals(formatted, "Title is Baeldung");
}
}

View File

@ -0,0 +1,17 @@
package com.baeldung.string.interview;
import static org.assertj.core.api.Assertions.assertThat;
import org.junit.Test;
public class StringInternUnitTest {
@Test
public void whenCallingStringIntern_thenStringsInterned() {
String s1 = "Baeldung";
String s2 = new String("Baeldung");
String s3 = new String("Baeldung").intern();
assertThat(s1 == s2).isFalse();
assertThat(s1 == s3).isTrue();
}
}

View File

@ -0,0 +1,18 @@
package com.baeldung.string.interview;
import java.util.StringJoiner;
import static org.junit.Assert.assertEquals;
import org.junit.Test;
public class StringJoinerUnitTest {
@Test
public void whenUsingStringJoiner_thenStringsJoined() {
StringJoiner joiner = new StringJoiner(",", "[", "]");
joiner.add("Red")
.add("Green")
.add("Blue");
assertEquals(joiner.toString(), "[Red,Green,Blue]");
}
}

View File

@ -0,0 +1,29 @@
package com.baeldung.string.interview;
import static org.assertj.core.api.Assertions.assertThat;
import org.junit.Test;
public class StringPalindromeUnitTest {
public boolean isPalindrome(String text) {
int forward = 0;
int backward = text.length() - 1;
while (backward > forward) {
char forwardChar = text.charAt(forward++);
char backwardChar = text.charAt(backward--);
if (forwardChar != backwardChar)
return false;
}
return true;
}
@Test
public void givenIsPalindromeMethod_whenCheckingString_thenFindIfPalindrome() {
assertThat(isPalindrome("madam")).isTrue();
assertThat(isPalindrome("radar")).isTrue();
assertThat(isPalindrome("level")).isTrue();
assertThat(isPalindrome("baeldung")).isFalse();
}
}

View File

@ -0,0 +1,13 @@
package com.baeldung.string.interview;
import static org.junit.Assert.assertEquals;
import org.junit.Test;
public class StringReverseUnitTest {
@Test
public void whenUsingInbuildMethods_thenStringReversed() {
String reversed = new StringBuilder("baeldung").reverse().toString();
assertEquals("gnudleab", reversed);
}
}

View File

@ -0,0 +1,32 @@
package com.baeldung.string.interview;
import org.apache.commons.lang3.StringUtils;
import org.junit.Test;
import static org.junit.Assert.assertArrayEquals;
public class StringSplitUnitTest {
@Test
public void givenCoreJava_whenSplittingStrings_thenSplitted() {
String expected[] = {
"john",
"peter",
"mary"
};
String[] splitted = "john,peter,mary".split(",");
assertArrayEquals( expected, splitted );
}
@Test
public void givenApacheCommons_whenSplittingStrings_thenSplitted() {
String expected[] = {
"john",
"peter",
"mary"
};
String[] splitted = StringUtils.split("john peter mary");
assertArrayEquals( expected, splitted );
}
}

View File

@ -0,0 +1,24 @@
package com.baeldung.string.interview;
import static org.junit.Assert.assertArrayEquals;
import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import org.junit.Test;
public class StringToByteArrayUnitTest {
@Test
public void whenGetBytes_thenCorrect() throws UnsupportedEncodingException {
byte[] byteArray1 = "abcd".getBytes();
byte[] byteArray2 = "efgh".getBytes(StandardCharsets.US_ASCII);
byte[] byteArray3 = "ijkl".getBytes("UTF-8");
byte[] expected1 = new byte[] { 97, 98, 99, 100 };
byte[] expected2 = new byte[] { 101, 102, 103, 104 };
byte[] expected3 = new byte[] { 105, 106, 107, 108 };
assertArrayEquals(expected1, byteArray1);
assertArrayEquals(expected2, byteArray2);
assertArrayEquals(expected3, byteArray3);
}
}

View File

@ -0,0 +1,17 @@
package com.baeldung.string.interview;
import static org.junit.Assert.assertEquals;
import java.util.Arrays;
import org.junit.Test;
public class StringToCharArrayUnitTest {
@Test
public void whenConvertingStringToCharArray_thenConversionSuccessful() {
String beforeConvStr = "hello";
char[] afterConvCharArr = { 'h', 'e', 'l', 'l', 'o' };
assertEquals(Arrays.equals(beforeConvStr.toCharArray(), afterConvCharArr), true);
}
}

View File

@ -0,0 +1,15 @@
package com.baeldung.string.interview;
import org.junit.Test;
import static org.assertj.core.api.Assertions.assertThat;
public class StringToIntegerUnitTest {
@Test
public void givenString_whenParsingInt_shouldConvertToInt() {
String givenString = "42";
int result = Integer.parseInt(givenString);
assertThat(result).isEqualTo(42);
}
}