【问题标题】:How to split a String twice and automatically create a table (with n elements)?如何将字符串拆分两次并自动创建一个表(包含 n 个元素)?
【发布时间】:2019-05-14 19:01:13
【问题描述】:

(这里绝对是初学者) 我的目标是一个表格,其中一行中的分隔数组中的元素由逗号分隔,并且在空格(“”)之后是一个新行。

一个新行必须在空格之后开始(这意味着我不能用一个点来替换它)。 我认为我最大的问题是我无法将“pretty 105”(注意空格)分开。它始终算作一个元素。

现在字符串只有 8 个元素,但您必须想象字符串非常长。

一开始我想拆分String(包括“pretty 105”)

在循环中,我试图让它在“漂亮”之后“跳转”,因为它应该在这里分裂

public static void main(String[] args) {

    String str = "104,Jeans,B&B,pretty 105,Shoes,Nike,nice";
    List<String> Row = Arrays.asList(str.split(" "));
    List<String> List = Arrays.asList(str.split(","));      


    StringBuilder buf = new StringBuilder();
    buf.append("<html>" +
               "<body>" +
               "<table>" +
               "<tr>" +
               "<th>Number</th>" +
               "<th>Name</th>" +
               "<th>Maker</th>" +
               "<th>Description</th>" +
               "</tr>");

    for (int j = 0; j < Row.size(); j++) {

        int i = j*4;

        for (; i < List.size(); i++) {              

          if (i<1+i) {
              buf.append("<tr><td>")
              .append(List.get(i))
              .append("</td>");
          }
          else if (i>=1+i) {
              buf.append("<td>") 
              .append(List.get(i))
              .append("</td>");
          }
          else if (i>3+i) {
              buf.append("<td>") 
              .append(List.get(i))
              .append("</td></tr>");
              break;
          }
    }
    }



    buf.append("</table>" +
               "</body>" +
               "</html>");
    String html = buf.toString();
    System.out.println(buf);}

在这个例子中,预期的结果是:

编号 名称 制造商 说明

104,牛仔裤,民宿,漂亮
105,鞋子,耐克,不错

但在上面的示例中,每个人都有自己的行 - 除了“pretty 105”:

104

牛仔裤

民宿

漂亮的 105

。 . .

【问题讨论】:

  • 这看起来更像 Java 而不是 Javascript。
  • @apsillers 你是对的...... StringBuilder 骗我认为这是 C#

标签: java loops split


【解决方案1】:

这是你想要的吗?

public class Main {
    public static void main(String[] args) {
        String str = "104,Jeans,B&B,pretty 105,Shoes,Nike,nice";
        final String join = String.join("\n", str.split(" "));
        System.out.println(join);
    }
}

编辑:

public static void main(String[] args) {

    String str = "104,Jeans,B&B,pretty 105,Shoes,Nike,nice";
    final String[] lines = str.split(" ");

    StringBuilder buf = new StringBuilder();
    buf.append("<html>" + "<body>" + "<table>" + "<tr>" + "<th>Number</th>" + "<th>Name</th>" + "<th>Maker</th>" + "<th>Description</th>" + "</tr>");

    for (String line : lines) {
        buf.append("<tr><td>")
                .append(line)
                .append("</td><td>");
    }
    buf.append("</table>" + "</body>" + "</html>");
    System.out.println(buf);
}

【讨论】:

  • 差不多。但还有一件事:它需要在该表中。你能告诉我如何将你的线和我的桌子结合起来(循环无关紧要)吗?
  • 我真的不明白桌子的东西。你能添加预期的输出吗?如果是文件,则放入预期的内容。
  • 我想回答你的问题,但我不知道评论格式是如何工作的,所以你必须想象它----表格的名称是“buf”,我已经准备好了标题(Number, Name...) 和下面是对应的内容。从我的代码中,您可以看到我想将新数据(来自字符串)附加到表中。它将采用 HTML 格式。
  • 请看编辑后的解决方案,如果回答对你有帮助,请不要忘记接受。
猜你喜欢
  • 2011-02-17
  • 2014-01-16
  • 2015-01-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多