Tôi đã cố gắng giải quyết vấn đề cắt xén chuỗi khi xuất hiện lần đầu tiên của một từ cụ thể và tôi không quan tâm đến tên gốc của phương thức (deleteLastOccurrence
) là IMO gây hiểu nhầm.
Bí quyết chỉ đối sánh một từ và không phải từ con cho tôi là thêm hai dấu phẩy trước và sau câu rồi kiểm tra từ bằng dấu phẩy.
tức là ",dog,"
sẽ được kiểm tra với số ",foo,bar,dog,cat,dog,bird,"
để hiện diện.
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 + ","; }
}
và nếu bạn muốn ưa thích một số thử nghiệm tôi đã sử dụng cho TDD, ở đây chúng tôi đi:
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")));
}
}
Một refactoring dài dòng cho một lớp OO hơn cũng có thể là:
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 + ",";}
}
với cách sử dụng như:
new Sentence("dog,foozzo,foo,cat,dog,bird").trimOnFirstOccurrenceOf("foo"), is(equalTo("dog,foozzo,foo"))
là gì 'đường dẫn'? Phương thức _not_ của bạn hoạt động như thế nào? (Đầu ra cho đầu vào bạn cung cấp cái gì?) – sarnold
Tôi đang gặp khó khăn khi hiểu phương pháp này được cho là phải làm gì. Nó có xóa mọi thứ sau lần đầu tiên của đối số thứ hai không? Nếu tôi đọc ví dụ của bạn sử dụng "deleteLastOccurrance" trong mã của ai đó, tôi chắc chắn sẽ không * mong đợi nó trả về "foo, bar, dog". Thay vào đó, tôi sẽ mong đợi "foo, bar, dog, cat, bird". –
Sử dụng kết hợp String.indexOf và String.substring sẽ là đủ. – dragon66