Reputation: 887
Greetings All;
I have a LinkedList of type String which contain some words like [from, to, in, then, however] and I have a text file which contain long sentences. what i want to do is to split those sentences in shorter sentences when one of the above words is found.
what I have did so far that I made a linked list that contains the words, and another linked list that contain the long sentences in the file. I don't know how to split the long sentence?
I have tried this:
int indexofsw = 0;
for (int k = 0; k < LongSentence.size(); k++) {
for (int j = 0; j < SWords.size(); j++) {
if (LongSentence.get(k).contains(SWords.get(j))== true) {
indexofsw = LongSentence.get(k).indexOf(SWords.get(j));
System.out.println(LongSentence.get(k).substring(k,indexofsw));
break;
}
}
}
but it doesn't returns a shorter sentence.
Any Ideas please?
Upvotes: 0
Views: 943
Reputation: 3994
Try using replace:
public class test {
public static void main(String[] args){
String[] splitWords = {"to", "in", "from", "then"};
String string = "this from that";
for (String splitWord : splitWords) {
string = string.replace(" " + splitWord + " ", System.getProperty("line.separator"));
}
System.out.println(string);
}
}
output:
this
that
Upvotes: 0
Reputation: 569
test.java, to get you started:
public class test{
public static void main(String[] args){
String[] splitWords = {"to", "in", "from", "then"};
String str = "this from that";
String[] tmp;
for (String splitTkn : splitWords){
tmp = str.split(splitTkn);
if (tmp.length > 1){
System.out.println(tmp[0].trim());
System.out.println(tmp[1].trim());
}
}
}
}
output:
this
that
Upvotes: 1
Reputation: 533890
Do you mean like?
Set<String> wordsToRemove =
String sentence =
List<String> words = Arrays.asList(sentence.split(" "));
words.removeAll(wordsToRemove);
Upvotes: 0