Java代码将字符串分段为有意义的字典单词,获取StringIndexOutOfBoundException

时间:2015-01-28 09:31:41

标签: java string dictionary

//这是将字符串分割成有意义的字典单词的Java代码。    获取StringIndexOutOfBoundException!

import java.util.*;
public class SeparateStringWords {
    public static void main(String[] args) {
        //segment a word into meaning full word eg. iamstudent => i am student

        String str = "iamstudent";
        Set < String > dict = new HashSet < String > ();
        dict.add("i");
        dict.add("am");
        dict.add("student");

        //dict is our lookup dictionary
        String separated = segmentString(str, dict);
        System.out.println("separated string is:" + separated);

    }
    static String segmentString(String str, Set < String > dict) {
        if (dict.contains(str)) return str;
        int len = str.length();
        System.out.println(len);
        for (int i = 1; i < len; i++) {
            String prefix = str.substring(0, i);
            if (dict.contains(prefix)) {
                String suffix = prefix.substring(i, len); //StringIndexOutOfBoundException 
                String subSuffix = segmentString(suffix, dict);
                if (subSuffix != null) {
                    return prefix + " " + subSuffix;

                }
            }
        }
        return null;
    }

}

//解决了:感谢帮助人们:) ..... suffix = str.substring(i,len);

2 个答案:

答案 0 :(得分:2)

前缀是一个比len短的子字符串,因为lenstr的长度,而prefix是从o到i的子字符串,所以你必须使用{ {1}};

答案 1 :(得分:0)

您可能想要获取原始字符串的后缀,而不是前缀:

String suffix=str.substring(i,len); 

尝试获取前缀的后缀是没有意义的,因为前缀只有i个字符,因此ilen都将超出范围。