BAEL-5151 (#11385)
* commited initial code for hexagonal architecture * Deleting to check in again * Deleing to check in again * Push first code for Hexagonal Architecture * final code with UT for JSON to Java conversion * removed hexagonal-architecture code from last commit * BEL-5071 updated README * BAEL-5071: Undo README changes and added a nested object in the JSON example. * BAEL-5071: fixed whitespace/indentation in JsonToJavaClassConversion.java * BAEL-5151: Added code for getting the first of a String * BAEL-5151: Renamed Unit Test * BAEL-5151: Moved the files from core-java-string-operations to core-java-string-operations-3 * BAEL-5151: Replaced tabs with white spaces. Co-authored-by: Vaibhav Jain <vaibhav.ashokjain@vodafone.com>
This commit is contained in:
parent
d1cf3c02c1
commit
3ab032d095
|
@ -0,0 +1,20 @@
|
|||
package com.baeldung.firstword;
|
||||
|
||||
public class FirstWordGetter {
|
||||
|
||||
public static void main(String[] args) {
|
||||
String input = "Roberto \"I wish you a bug-free day\"";
|
||||
System.out.println("Using split: " + getFirstWordUsingSplit(input));
|
||||
System.out.println("Using subString: " + getFirstWordUsingSubString(input));
|
||||
}
|
||||
|
||||
public static String getFirstWordUsingSubString(String input) {
|
||||
int index = input.contains(" ") ? input.indexOf(" ") : 0;
|
||||
return input.substring(0, index);
|
||||
}
|
||||
|
||||
public static String getFirstWordUsingSplit(String input) {
|
||||
String[] tokens = input.split(" ", 2);
|
||||
return tokens[0];
|
||||
}
|
||||
}
|
|
@ -0,0 +1,30 @@
|
|||
package com.baeldung.firstword;
|
||||
|
||||
import static com.baeldung.firstword.FirstWordGetter.getFirstWordUsingSplit;
|
||||
import static com.baeldung.firstword.FirstWordGetter.getFirstWordUsingSubString;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class FirstWordGetterUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenString_whenSplit_thenFirstWordIsReturned() {
|
||||
assertEquals("Roberto", getFirstWordUsingSplit("Roberto \"I wish you a bug-free day\""));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenStringWithNoSpace_whenSplit_thenFirstWordIsReturned() {
|
||||
assertEquals("StringWithNoSpace", getFirstWordUsingSplit("StringWithNoSpace"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenString_whenPassedToSubstring_thenFirstWordIsReturned() {
|
||||
assertEquals("Roberto", getFirstWordUsingSubString("Roberto \"I wish you a bug-free day\""));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenStringWithNoSpace_whenPassedToSubstring_thenFirstWordIsReturned() {
|
||||
assertEquals("", getFirstWordUsingSubString("StringWithNoSpace"));
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue