【问题标题】:Split Email:passrwords to list of emails and list of passwords [duplicate]拆分电子邮件:密码到电子邮件列表和密码列表[重复]
【发布时间】:2021-09-26 09:46:45
【问题描述】:

假设我有一个 TextArea 包含电子邮件:密码 - 以这种方式 - 在每一行中。 我想将电子邮件和密码拆分为电子邮件列表和密码列表。 我该怎么做?

【问题讨论】:

  • 你已经提到了split,到目前为止你有没有尝试过使用?
  • 是的,但我不知道该怎么做,因为我通常会写 mail = textArea1.getText().split("\\r?\\n"); pass = textArea2.getText().split("\\r?\\n"); 来溢出每一行,并且每一行都在不同的 TextArea 中。我的土豆头不明白怎么做

标签: java split


【解决方案1】:

您可以使用split 首先在所有换行符处拆分,然后在: 处拆分每一行。当然,如果密码中包含:,您可能会遇到麻烦,但这是另一项任务。

    String text = textArea.getText();
    String[] lines = text.split("\r?\n");
    List<String> users = new ArrayList<>(lines.length);
    List<String> passwords = new ArrayList<>(lines.length);
    for(String line:lines) {
        String[] userAndPass = line.split(":");
        users.add(userAndPass[0]);
        passwords.add(userAndPass[1]);
    }

text 分配给"u1:p1\nu2:p2\r\nu3:p3" 的示例输入并在末尾添加System.out.println(users +"\n"+ passwords); 将打印:

[u1, u2, u3]
[p1, p2, p3]

【讨论】:

  • 是的,如果密码中包含:,我会让它只拆分文本一次。对吗?
  • 为什么处理:在密码中是一项不同的任务?
  • @AwniAhmed 是的,在这种情况下使用 indexOf 和子字符串。
  • @JoakimDanielson 因为要求中没有提及。此外,对于“我什么都没试过”的问题,我认为这是一个相当广泛的答案。
猜你喜欢
  • 2017-08-30
  • 2021-10-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-12-06
  • 1970-01-01
  • 2012-03-09
  • 2011-10-07
相关资源
最近更新 更多