Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: eugenp/tutorials
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: master
Choose a base ref
...
head repository: mohanadayman/tutorials
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: master
Choose a head ref

There isn’t anything to compare.

eugenp:master and mohanadayman:master are entirely different commit histories.

Original file line number Diff line number Diff line change
@@ -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];
}
}
Original file line number Diff line number Diff line change
@@ -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"));
}
}