有没有办法将两个多行字符串加在一起?

时间:2019-05-20 20:43:44

标签: c#

string usernamesfile = File.ReadAllText(openusername.FileName);
string passwordsfile = File.ReadAllText(openpassword.FileName);

我想从两个文本文件中读取内容,并将所有行用“:”组合起来,将它们分开:

基本上:

line:line
line2:line2

谢谢=)

1 个答案:

答案 0 :(得分:0)

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

        // 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();