【问题标题】:Is there a way to add two multi line strings together? [duplicate]有没有办法将两个多行字符串添加在一起? [复制]
【发布时间】:2019-05-20 20:43:43
【问题描述】:
string usernamesfile = File.ReadAllText(openusername.FileName);
string passwordsfile = File.ReadAllText(openpassword.FileName);

我想从两个文本文件中读取并将所有行组合在一起,用“:”分隔它们:

基本上:

line:line
line2:line2

谢谢 =)

【问题讨论】:

  • 你试过什么?你试过连接吗?

标签: c#


【解决方案1】:

如果两个文件的行数相同,以下将起作用

        // assumed file strings
        var usernames = "Username1\n" + 
                        "Username2\n" +
                        "username3";
        var passwords = "Password1\n" +
                        "Password2\n" +
                        "Password3";
        // Split it for each line
        var usernamesfile = usernames.Split("\n"); 
        var passwordsfile = passwords.Split("\n");
        var sb = new StringBuilder();
        for (int i = 0; i < usernamesfile.Length; i++)
        {
            sb.AppendLine($"{usernamesfile[i]}:{passwordsfile[i]}");
        }

        // Username1:Password1
        // Username2:Password2
        // username3:Password3
        var bothLines = sb.ToString();
猜你喜欢
  • 2018-11-21
  • 1970-01-01
  • 1970-01-01
  • 2016-12-23
  • 1970-01-01
  • 2011-06-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多