Skip to content

Commit bdcc961

Browse files
ramansahasijzheaux
authored andcommitted
BAEL-2392_Java_String_interview_questions second commit (eugenp#6064)
1 parent 829b11e commit bdcc961

13 files changed

+275
-0
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.baeldung.string.interview;
2+
3+
import java.math.BigDecimal;
4+
import java.text.NumberFormat;
5+
import java.util.Locale;
6+
import static org.junit.Assert.assertEquals;
7+
8+
import org.junit.Test;
9+
10+
public class LocaleUnitTest {
11+
@Test
12+
public void whenUsingLocal_thenCorrectResultsForDifferentLocale() {
13+
Locale usLocale = Locale.US;
14+
BigDecimal number = new BigDecimal(102_300.456d);
15+
16+
NumberFormat usNumberFormat = NumberFormat.getCurrencyInstance(usLocale);
17+
assertEquals(usNumberFormat.format(number), "$102,300.46");
18+
}
19+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.baeldung.string.interview;
2+
3+
import static org.assertj.core.api.Assertions.assertThat;
4+
5+
import java.util.Arrays;
6+
7+
import org.junit.Test;
8+
9+
public class StringAnagramUnitTest {
10+
public boolean isAnagram(String s1, String s2) {
11+
if(s1.length() != s2.length())
12+
return false;
13+
14+
char[] arr1 = s1.toCharArray();
15+
char[] arr2 = s2.toCharArray();
16+
17+
Arrays.sort(arr1);
18+
Arrays.sort(arr2);
19+
20+
return Arrays.equals(arr1, arr2);
21+
}
22+
23+
@Test
24+
public void whenTestAnagrams_thenTestingCorrectly() {
25+
assertThat(isAnagram("car", "arc")).isTrue();
26+
assertThat(isAnagram("west", "stew")).isTrue();
27+
assertThat(isAnagram("west", "east")).isFalse();
28+
}
29+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.baeldung.string.interview;
2+
3+
import static org.junit.Assert.assertEquals;
4+
5+
import org.junit.Test;
6+
7+
public class StringChangeCaseUnitTest {
8+
@Test
9+
public void givenString_whenChangingToUppercase_thenCaseChanged() {
10+
String s = "Welcome to Baeldung!";
11+
assertEquals("WELCOME TO BAELDUNG!", s.toUpperCase());
12+
}
13+
14+
15+
@Test
16+
public void givenString_whenChangingToLowerrcase_thenCaseChanged() {
17+
String s = "Welcome to Baeldung!";
18+
assertEquals("welcome to baeldung!", s.toLowerCase());
19+
}
20+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.baeldung.string.interview;
2+
3+
import static org.junit.Assert.assertEquals;
4+
5+
import org.junit.Test;
6+
7+
public class StringCountOccurrencesUnitTest {
8+
public int countOccurrences(String s, char c) {
9+
int count = 0;
10+
for (int i = 0; i < s.length(); i++) {
11+
if (s.charAt(i) == c) {
12+
count++;
13+
}
14+
}
15+
return count;
16+
}
17+
18+
@Test
19+
public void givenString_whenCountingFrequencyOfChar_thenCountCorrect() {
20+
assertEquals(3, countOccurrences("united states", 't'));
21+
}
22+
23+
public void givenString_whenUsingJava8_thenCountingOfCharCorrect() {
24+
String str = "united states";
25+
long count = str.chars().filter(ch -> (char)ch == 't').count();
26+
assertEquals(3, count);
27+
}
28+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.baeldung.string.interview;
2+
3+
import static org.junit.Assert.assertEquals;
4+
5+
import org.junit.Test;
6+
7+
public class StringFormatUnitTest {
8+
@Test
9+
public void givenString_whenUsingStringFormat_thenStringFormatted() {
10+
String title = "Baeldung";
11+
String formatted = String.format("Title is %s", title);
12+
assertEquals(formatted, "Title is Baeldung");
13+
}
14+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.baeldung.string.interview;
2+
3+
import static org.assertj.core.api.Assertions.assertThat;
4+
5+
import org.junit.Test;
6+
7+
public class StringInternUnitTest {
8+
@Test
9+
public void whenCallingStringIntern_thenStringsInterned() {
10+
String s1 = "Baeldung";
11+
String s2 = new String("Baeldung");
12+
String s3 = new String("Baeldung").intern();
13+
14+
assertThat(s1 == s2).isFalse();
15+
assertThat(s1 == s3).isTrue();
16+
}
17+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.baeldung.string.interview;
2+
3+
import java.util.StringJoiner;
4+
import static org.junit.Assert.assertEquals;
5+
6+
import org.junit.Test;
7+
8+
public class StringJoinerUnitTest {
9+
@Test
10+
public void whenUsingStringJoiner_thenStringsJoined() {
11+
StringJoiner joiner = new StringJoiner(",", "[", "]");
12+
joiner.add("Red")
13+
.add("Green")
14+
.add("Blue");
15+
16+
assertEquals(joiner.toString(), "[Red,Green,Blue]");
17+
}
18+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.baeldung.string.interview;
2+
3+
import static org.assertj.core.api.Assertions.assertThat;
4+
5+
import org.junit.Test;
6+
7+
public class StringPalindromeUnitTest {
8+
9+
public boolean isPalindrome(String text) {
10+
int forward = 0;
11+
int backward = text.length() - 1;
12+
while (backward > forward) {
13+
char forwardChar = text.charAt(forward++);
14+
char backwardChar = text.charAt(backward--);
15+
if (forwardChar != backwardChar)
16+
return false;
17+
}
18+
return true;
19+
}
20+
21+
@Test
22+
public void givenIsPalindromeMethod_whenCheckingString_thenFindIfPalindrome() {
23+
assertThat(isPalindrome("madam")).isTrue();
24+
assertThat(isPalindrome("radar")).isTrue();
25+
assertThat(isPalindrome("level")).isTrue();
26+
27+
assertThat(isPalindrome("baeldung")).isFalse();
28+
}
29+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.baeldung.string.interview;
2+
3+
import static org.junit.Assert.assertEquals;
4+
5+
import org.junit.Test;
6+
7+
public class StringReverseUnitTest {
8+
@Test
9+
public void whenUsingInbuildMethods_thenStringReversed() {
10+
String reversed = new StringBuilder("baeldung").reverse().toString();
11+
assertEquals("gnudleab", reversed);
12+
}
13+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.baeldung.string.interview;
2+
3+
import org.apache.commons.lang3.StringUtils;
4+
import org.junit.Test;
5+
import static org.junit.Assert.assertArrayEquals;
6+
7+
public class StringSplitUnitTest {
8+
@Test
9+
public void givenCoreJava_whenSplittingStrings_thenSplitted() {
10+
String expected[] = {
11+
"john",
12+
"peter",
13+
"mary"
14+
};
15+
16+
String[] splitted = "john,peter,mary".split(",");
17+
assertArrayEquals( expected, splitted );
18+
}
19+
20+
@Test
21+
public void givenApacheCommons_whenSplittingStrings_thenSplitted() {
22+
String expected[] = {
23+
"john",
24+
"peter",
25+
"mary"
26+
};
27+
String[] splitted = StringUtils.split("john peter mary");
28+
assertArrayEquals( expected, splitted );
29+
}
30+
31+
32+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.baeldung.string.interview;
2+
3+
import static org.junit.Assert.assertArrayEquals;
4+
5+
import java.io.UnsupportedEncodingException;
6+
import java.nio.charset.StandardCharsets;
7+
8+
import org.junit.Test;
9+
10+
public class StringToByteArrayUnitTest {
11+
@Test
12+
public void whenGetBytes_thenCorrect() throws UnsupportedEncodingException {
13+
byte[] byteArray1 = "abcd".getBytes();
14+
byte[] byteArray2 = "efgh".getBytes(StandardCharsets.US_ASCII);
15+
byte[] byteArray3 = "ijkl".getBytes("UTF-8");
16+
byte[] expected1 = new byte[] { 97, 98, 99, 100 };
17+
byte[] expected2 = new byte[] { 101, 102, 103, 104 };
18+
byte[] expected3 = new byte[] { 105, 106, 107, 108 };
19+
20+
assertArrayEquals(expected1, byteArray1);
21+
assertArrayEquals(expected2, byteArray2);
22+
assertArrayEquals(expected3, byteArray3);
23+
}
24+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.baeldung.string.interview;
2+
3+
import static org.junit.Assert.assertEquals;
4+
5+
import java.util.Arrays;
6+
7+
import org.junit.Test;
8+
9+
public class StringToCharArrayUnitTest {
10+
@Test
11+
public void whenConvertingStringToCharArray_thenConversionSuccessful() {
12+
String beforeConvStr = "hello";
13+
char[] afterConvCharArr = { 'h', 'e', 'l', 'l', 'o' };
14+
15+
assertEquals(Arrays.equals(beforeConvStr.toCharArray(), afterConvCharArr), true);
16+
}
17+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.baeldung.string.interview;
2+
3+
import org.junit.Test;
4+
import static org.assertj.core.api.Assertions.assertThat;
5+
6+
public class StringToIntegerUnitTest {
7+
@Test
8+
public void givenString_whenParsingInt_shouldConvertToInt() {
9+
String givenString = "42";
10+
11+
int result = Integer.parseInt(givenString);
12+
13+
assertThat(result).isEqualTo(42);
14+
}
15+
}

0 commit comments

Comments
 (0)