import java.util.ArrayList;
public class split {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int count=0;
ArrayList<String> template= new ArrayList<String>;
String sentence ="$name1 went to the fruit stall with $int1 pieces of $5-notes. $name1 bought $int2 $fruit1 and $int3 $fruit2. Each $fruit1 cost $$decimal10. Each $fruit2 cost $$decimal20. How much money had $name1 left? ( Leave your answer to one decimal place )";
String[] words = sentence.split(" ");
for(int i=0; i< words.length;i++) {
System.out.println(i+": "+words[i]);
if(words[i].equals("$fruit")) {
System.out.println("$fruit is in this template");
}
if(words[i].equals("$name1")) {
System.out.println("$name1 is in this template");
count++;
}
}
System.out.println("$name1 Appears "+ count + " times." );
}
}
我有一个如上图所示的句子模板。哪一个是
String sentence ="$name1 went to the fruit stall with $int1 pieces of $5-notes. $name1 bought $int2 $fruit1 and $int3 $fruit2. Each $fruit1 cost $$decimal10. Each $fruit2 cost $$decimal20. How much money had $name1 left? ( Leave your answer to one decimal place )";
我只想处理句子模板并提取所有文本模板变量,如 $name1、$int1 等。它们将被替换为合适的文本,在其中我制作了一个随机生成器并将它们组合回句子以制作一个完整的句子。
我已经尝试了上面的拆分来提取文本变量模板(例如:$name1、$int1 等)。它有效,但我正在寻找一种更简单和更短的方法。因为如果我有很长的段落,for循环处理时间会很慢。
我已阅读有关 Collections 的信息。但不知道它是否可以用于我的情况。任何人有任何意见或任何想法,你可能已经做过?