如何在拆分字符串中输出所有字符串数组?

我写了这段代码,将“@”和“:”分开


var Split = Texts.Split(new char[] { '@' });

var Split1 = Texts.Split(new char[] { ':' });

我想将所有字符串数组输出到一个文件中。我已经试过了,但我只得到一根绳子,而不是全部。


formatted = Split[0] + ":" + Split1[1];

File.WriteAllText(outputfile, formatted);

这是我的代码:


public void CreateUsernameList(string targetfile,string outputfile)

{

    string[] texts = File.ReadAllLines(targetfile);

    string formatted = null;

    foreach(string Texts in texts)

    {

        var Split = Texts.Split(new char[] { '@' });

        var Split1 = Texts.Split(new char[] { ':' });

        formatted = Split[0] + ":" + Split1[1];

        File.WriteAllText(outputfile, formatted);

    }

}


智慧大石
浏览 123回答 3
3回答

九州编程

您正在不断覆盖该循环中的文件。而是将结果收集在 a 中List<string>,然后将其写入文件。public void CreateUsernameList(string targetfile,string outputfile){&nbsp; &nbsp; string[] texts = File.ReadAllLines(targetfile);&nbsp; &nbsp; string formatted = null;&nbsp; &nbsp; List<string> output = new List<string>();&nbsp; &nbsp; foreach(string Texts in texts)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; var Split = Texts.Split(new char[] { '@' });&nbsp; &nbsp; &nbsp; &nbsp; var Split1 = Texts.Split(new char[] { ':' });&nbsp; &nbsp; &nbsp; &nbsp; formatted = Split[0] + ":" + Split1[1];&nbsp; &nbsp; &nbsp; &nbsp; output.Add(formatted);&nbsp; &nbsp; }&nbsp; &nbsp; File.WriteAllLines(outputfile, output)}不会使用那么多内存的替代方案是public void CreateUsernameList(string targetfile,string outputfile){&nbsp; &nbsp; File.WriteAllLines(&nbsp; &nbsp; &nbsp; &nbsp; outputfile,&nbsp; &nbsp; &nbsp; &nbsp; File.ReadLines(targetfile)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .Select(line =>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;var Split = line.Split(new char[] { '@' });&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;var Split1 = line.Split(new char[] { ':' });&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return Split[0] + ":" + Split1[1];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; &nbsp; &nbsp; )&nbsp; &nbsp; );&nbsp;}

茅侃侃

最好以惰性方式读取文件:File.ReadLines(targetfile).ForEach(line =>{&nbsp; &nbsp; File.AppendAllText("path", string.Join(":", Regex.Split(line, "@|:")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.Cast<Match>().Select(m => m.Value)));});static class ExtensionMethods{&nbsp; &nbsp; internal static void ForEach<T>(this IEnumerable<T> enumerable, Action<T> action)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; if (enumerable == null) throw new NullReferenceException($"'{nameof(enumerable)}' argument is null");&nbsp; &nbsp; &nbsp; &nbsp; using var enumerator = enumerable.GetEnumerator();&nbsp; &nbsp; &nbsp; &nbsp; while (enumerator.MoveNext()) action(enumerator.Current);&nbsp; &nbsp; }}

陪伴而非守候

由于我没有看到任何格式的数据,我只能猜测错误可能出在这里:格式化 = Split[0] + ":" + Split1[1];您只从每个字符串数组中获取单个元素。尝试遍历数组 Split 和 Split1 中的所有元素以打印它们的值
打开App,查看更多内容
随时随地看视频慕课网APP