added examples about how to reverse a string in java (#7219)

This commit is contained in:
Catalin Burcea 2019-07-01 14:16:19 +03:00 committed by maibin
parent 1318804764
commit c79bc22668
2 changed files with 126 additions and 0 deletions

View File

@ -0,0 +1,56 @@
package com.baeldung.string.reverse;
import org.apache.commons.lang3.StringUtils;
public class ReverseStringExamples {
public static String reverse(String input) {
if (input == null) {
return null;
}
String output = "";
for (int i = input.length() - 1; i >= 0; i--) {
output = output + input.charAt(i);
}
return output;
}
public static String reverseUsingStringBuilder(String input) {
if (input == null) {
return null;
}
StringBuilder output = new StringBuilder(input).reverse();
return output.toString();
}
public static String reverseUsingApacheCommons(String input) {
return StringUtils.reverse(input);
}
public static String reverseTheOrderOfWords(String sentence) {
if (sentence == null) {
return null;
}
StringBuilder output = new StringBuilder();
String[] words = sentence.split(" ");
for (int i = words.length - 1; i >= 0; i--) {
output.append(words[i]);
output.append(" ");
}
return output.toString()
.trim();
}
public static String reverseTheOrderOfWordsUsingApacheCommons(String sentence) {
return StringUtils.reverseDelimited(sentence, ' ');
}
}

View File

@ -0,0 +1,70 @@
package com.baeldung.string.reverse;
import org.apache.commons.lang3.StringUtils;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
public class ReverseStringExamplesUnitTest {
private static final String STRING_INPUT = "cat";
private static final String STRING_INPUT_REVERSED = "tac";
private static final String SENTENCE = "The quick brown fox jumps over the lazy dog";
private static final String REVERSED_WORDS_SENTENCE = "dog lazy the over jumps fox brown quick The";
@Test
public void whenReverseIsCalled_ThenCorrectStringIsReturned() {
String reversed = ReverseStringExamples.reverse(STRING_INPUT);
String reversedNull = ReverseStringExamples.reverse(null);
String reversedEmpty = ReverseStringExamples.reverse(StringUtils.EMPTY);
assertEquals(STRING_INPUT_REVERSED, reversed);
assertEquals(null, reversedNull);
assertEquals(StringUtils.EMPTY, reversedEmpty);
}
@Test
public void whenReverseUsingStringBuilderIsCalled_ThenCorrectStringIsReturned() throws Exception {
String reversed = ReverseStringExamples.reverseUsingStringBuilder(STRING_INPUT);
String reversedNull = ReverseStringExamples.reverseUsingStringBuilder(null);
String reversedEmpty = ReverseStringExamples.reverseUsingStringBuilder(StringUtils.EMPTY);
assertEquals(STRING_INPUT_REVERSED, reversed);
assertEquals(null, reversedNull);
assertEquals(StringUtils.EMPTY, reversedEmpty);
}
@Test
public void whenReverseUsingApacheCommonsIsCalled_ThenCorrectStringIsReturned() throws Exception {
String reversed = ReverseStringExamples.reverseUsingApacheCommons(STRING_INPUT);
String reversedNull = ReverseStringExamples.reverseUsingApacheCommons(null);
String reversedEmpty = ReverseStringExamples.reverseUsingApacheCommons(StringUtils.EMPTY);
assertEquals(STRING_INPUT_REVERSED, reversed);
assertEquals(null, reversedNull);
assertEquals(StringUtils.EMPTY, reversedEmpty);
}
@Test
public void whenReverseTheOrderOfWordsIsCalled_ThenCorrectStringIsReturned() {
String reversed = ReverseStringExamples.reverseTheOrderOfWords(SENTENCE);
String reversedNull = ReverseStringExamples.reverseTheOrderOfWords(null);
String reversedEmpty = ReverseStringExamples.reverseTheOrderOfWords(StringUtils.EMPTY);
assertEquals(REVERSED_WORDS_SENTENCE, reversed);
assertEquals(null, reversedNull);
assertEquals(StringUtils.EMPTY, reversedEmpty);
}
@Test
public void whenReverseTheOrderOfWordsUsingApacheCommonsIsCalled_ThenCorrectStringIsReturned() {
String reversed = ReverseStringExamples.reverseTheOrderOfWordsUsingApacheCommons(SENTENCE);
String reversedNull = ReverseStringExamples.reverseTheOrderOfWordsUsingApacheCommons(null);
String reversedEmpty = ReverseStringExamples.reverseTheOrderOfWordsUsingApacheCommons(StringUtils.EMPTY);
assertEquals(REVERSED_WORDS_SENTENCE, reversed);
assertEquals(null, reversedNull);
assertEquals(StringUtils.EMPTY, reversedEmpty);
}
}