Java,如果链的长度超过一个字符,则添加新的字符串对象

时间:2015-02-28 00:25:02

标签: java string arraylist newline

如果这个字符串的长度太大,我试图在arraylist中添加一个新字符串。

像那样;

private void createLore(String s) {
ArrayList<String> a = new ArrayList<String>();
if(s.lenght > maximumLenght) {
a.add(s /TODO  new object with same string
//it's like a new line .. possible to detect a space ? 
//This avoids the cut of a text

谢谢!

2 个答案:

答案 0 :(得分:0)

希望以下代码有帮助

    private void createLore(String s,int maximumLenght) {

       ArrayList<String> a = new ArrayList<String>();
        if(s.trim().length() > maximumLenght) { //s.trim is to trim the additional spaces
             a.add(s); //Add the bigger string to the arraylist
     }

答案 1 :(得分:0)

谢谢,但我做了这个:

 List<String> lores = new ArrayList<String>();
    int i = 0;

    String current = "";

    while(i < str.length()){

        current = current + str.charAt(i);

        if((current.length() >= 25 && str.charAt(i) == ' ' && current != "") || i + 1 == str.length()){
            lores.add(current);
            current = "";
        }
        i++;