Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I tried to solve the problem of trimming a string on the first occurrence of a specific word and I didn't care about the original name of the method (<code>deleteLastOccurrence</code>) that is IMO misleading.</p> <p>The trick to match only single word and not subwords for me is to add two commas before and after the sentence and then check the word with commas. </p> <p>i.e. <code>",dog,"</code> will be checked against <code>",foo,bar,dog,cat,dog,bird,"</code> for presence.</p> <pre><code>package gicappa; public class So { public static String trimSentenceOnFirstOccurrenceOf(String sentence, String word) { if (word.isEmpty()) return sentence; if (!addCommasAround(sentence).contains(addCommasAround(word))) return sentence; return trimAddedCommasOf(substringOfSentenceUntilEndOfWord(addCommasAround(sentence), addCommasAround(word))); } public static String substringOfSentenceUntilEndOfWord(String string, String word) { return string.substring(0, string.indexOf(word) + word.length()); } public static String trimAddedCommasOf(String string) {return string.substring(1,string.length()-1);} public static String addCommasAround(String s) {return "," + s + ","; } } </code></pre> <p>and if you'd fancy some testing I used for TDD, here we go:</p> <pre><code>package gicappa; import org.junit.Test; import static gicappa.So.trimSentenceOnFirstOccurrenceOf; import static org.hamcrest.core.Is.is; import static org.hamcrest.core.IsEqual.equalTo; import static org.junit.Assert.assertThat; public class SoTest { @Test public void it_returns_the_same_sentence_for_empty_word() { assertThat(trimSentenceOnFirstOccurrenceOf("foo,bar,dog,cat,dog,bird", ""), is(equalTo("foo,bar,dog,cat,dog,bird"))); } @Test public void it_returns_the_same_sentence_for_not_contained_word() { assertThat(trimSentenceOnFirstOccurrenceOf("foo,bar,dog,cat,dog,bird", "s"), is(equalTo("foo,bar,dog,cat,dog,bird"))); } @Test public void it_returns_the_first_word() { assertThat(trimSentenceOnFirstOccurrenceOf("foo,bar,dog,cat,dog,bird", "foo"), is(equalTo("foo"))); } @Test public void it_returns_the_same_sentence_if_is_matched_the_last_word() { assertThat(trimSentenceOnFirstOccurrenceOf("foo,bar,dog,cat,dog,bird", "bird"), is(equalTo("foo,bar,dog,cat,dog,bird"))); } @Test public void it_trims_after_the_end_of_the_first_matched_word() { assertThat(trimSentenceOnFirstOccurrenceOf("foo,bar,dog,cat,dog,bird", "dog"), is(equalTo("foo,bar,dog"))); } @Test public void it_does_not_trim_for_a_subword_of_a_contained_word() { assertThat(trimSentenceOnFirstOccurrenceOf("foo,bar,dog,cat,dog,bird", "do"), is(equalTo("foo,bar,dog,cat,dog,bird"))); } @Test public void it_does_not_trim_for_a_subword_of_an_already_contained_word() { assertThat(trimSentenceOnFirstOccurrenceOf("dog,foozzo,foo,cat,dog,bird", "foo"), is(equalTo("dog,foozzo,foo"))); } } </code></pre> <p>A wordy refactoring for a more OO class could also be:</p> <pre><code>package gicappa; public class Sentence { private String s; public Sentence(String sentence) { this.s = sentence; } public String trimOnFirstOccurrenceOf(String word) { if (word.isEmpty() || csvSentenceContainsWord(word)) return s; return substringSentenceToEndOf(word); } private String substringSentenceToEndOf(String word) { return addCommasTo(s).substring(1, addCommasTo(s).indexOf(addCommasTo(word)) + addCommasTo(word).length()-1); } private boolean csvSentenceContainsWord(String word) { return !addCommasTo(s).contains(addCommasTo(word)); } public static String addCommasTo(String s) {return "," + s + ",";} } </code></pre> <p>with usage like:</p> <pre><code>new Sentence("dog,foozzo,foo,cat,dog,bird").trimOnFirstOccurrenceOf("foo"), is(equalTo("dog,foozzo,foo")) </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload