deleteLastOccurrence
特定の単語が最初に出現したときに文字列をトリミングする問題を解決しようとしましたが、 IMOの誤解を招くメソッドの元の名前()は気にしませんでした。
サブワードではなく単一の単語のみに一致する秘訣は、文の前後に2つのコンマを追加してから、コンマで単語をチェックすることです。
つまり 、存在",dog,"
するかどうかがチェックされます",foo,bar,dog,cat,dog,bird,"
。
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 + ","; }
}
そして、私がTDDに使用したテストが必要な場合は、次のようにします。
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")));
}
}
より多くのOOクラスのための言葉のリファクタリングも次のようになります。
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 + ",";}
}
次のような使用法で:
new Sentence("dog,foozzo,foo,cat,dog,bird").trimOnFirstOccurrenceOf("foo"), is(equalTo("dog,foozzo,foo"))