用Java生成字符串标记

时间:2012-12-11 14:51:27

标签: java

我想在Java中使用正向遍历生成可能的令牌。例如,如果我有一个字符串“这是我的车”。我需要生成令牌

  • “这是我的车”
  • “这是我的”
  • “这是”
  • “这”
  • “是我的车”
  • “是我的”
  • “是”
  • “我的车”
  • “我的”
  • “汽车”

最好的方法是什么?任何例子?感谢。

4 个答案:

答案 0 :(得分:5)

以下是拆分和嵌套循环的另一种解决方案:

public static void main(String[] args) {
    String original = "this is my car";

    String[] singleWords = original.split(" ");  // split the String to get the single words
    ArrayList<String> results = new ArrayList<String>();  // a container for all the possible sentences
    for (int startWord = 0; startWord < singleWords.length; startWord++) {  // starWords start with 0 and increment just until they reach the last word
        for (int lastWord = singleWords.length; lastWord > startWord; lastWord--) { // last words start at the end and decrement just until they reached the first word 
            String next = "";
            for (int i = startWord; i != lastWord; i++) { // put all words in one String (starting with the startWord and ending with the lastWord)
                next += singleWords[i] + " "; 
            }
            results.add(next);  // add the next result to your result list
        }
    }

    // this is just to check the results. All your sentences are now stored in the ArrayList results
    for (String string : results) {
        System.out.println("" + string);
    }
}

这是我测试方法时的结果:

this is my car 
this is my 
this is 
this 
is my car 
is my 
is 
my car 
my 
car 

答案 1 :(得分:2)

使用Guava

String yourOriginalString = "This is my car";
final Set<String> originalWords = 
        Sets.newLinkedHashSet(
                Splitter.on(CharMatcher.WHITESPACE).trimResults().split(yourOriginalString));
final Set<Set<String>> variations = Sets.powerSet(originalWords);
for (Set<String> variation : variations) {
    System.out.println(Joiner.on(' ').join(variation));
}

输出:

This
is
This is
my
This my
is my
This is my
car
This car
is car
This is car
my car
This my car
is my car
This is my car

答案 2 :(得分:1)

这是一种可行的方法:

//Just a method that seperates your String into an array of words based on the spaces
//I'll leave that for you to figure out how to make
String[] array = getSeperatedWords(<yourword>);
List<StringBuffer> bufferArray = new ArrayList<StringBuffer>();

for(int i = 0; i < array.length; i++){
    StringBuffer nowWord = array[i];
    for(int j = i; j < array.length; j++{
        nowWord.append(array[j]);
    }
    bufferArray.add(nowWord);
 }
 for(int i = 0; i < bufferArray.length; i++){
    System.out.print(bufferArray.get(i));
 }

答案 3 :(得分:1)

import java.util.Arrays;

public class Test {

    public static void main(String[] args) {

        String var = "This is my car";
        permute(var);

    }

    public static void permute(String var) {

        if(var.isEmpty())
            return;

        String[] arr = var.split(" ");  

        while(arr.length > 0) {
            for(String str : arr) {
                System.out.print(str + " ");
            }
            arr = (String[]) Arrays.copyOfRange(arr, 0, arr.length - 1);
            System.out.println();               
        }       

        String[] original = var.split(" ");
        permute(implodeArray((String[]) Arrays.copyOfRange(original, 1, original.length), " "));

    }

    public static String implodeArray(String[] inputArray, String glueString) {

        String output = "";

        if (inputArray.length > 0) {
            StringBuilder sb = new StringBuilder();
            sb.append(inputArray[0]);

            for (int i=1; i<inputArray.length; i++) {
                sb.append(glueString);
                sb.append(inputArray[i]);
            }

            output = sb.toString();

        }

        return output;

    }        

}

阅读本书,您将成为递归的主人:http://mitpress.mit.edu/sicp/