【问题标题】:Java , add new string object if the length of the chain exceeds a number of characterJava,如果链的长度超过字符数,则添加新的字符串对象
【发布时间】:2015-04-30 20:16:47
【问题描述】:

如果这个字符串的长度太大,我正在尝试在 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

谢谢!

【问题讨论】:

  • 能否请您发布输入/输出示例?
  • 嗯,它有一个 api,我会解释:i.imgur.com/8voYbER.png 如你所见,有 3 行文本,我想要同样的东西,但使用一种方法,因为我必须这样做: list.add("玩家拿..."); list.add("世界。然而...");等

标签: java string arraylist newline


【解决方案1】:

谢谢,但我做了这个:

 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++; 

【讨论】:

    【解决方案2】:

    希望下面的代码有所帮助

        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
         }
    

    【讨论】:

      猜你喜欢
      • 2021-05-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多