0

I need to add a regex string to an array but I get an error like "string can't implement in string[]".

This is the code:

foreach (string ss in fileLines)
{
    filelinesclean = System.Text.RegularExpressions.Regex.Replace(ss, @"\s+", " ");
    MessageBox.Show("ok");
    System.IO.File.WriteAllLines(@"C:\Users\Public\WriteLines.txt", filelinesclean);
}
3
  • filelinesclean is a string but File.WriteAllLines takes an array of strings for its second parameter. Perhaps you meant to use File.WriteLine. Commented Mar 9, 2016 at 20:30
  • filelinesclean is an array private string[] filelinesclean; Commented Mar 9, 2016 at 20:32
  • So that no-one proposes an answer which could result in running out of memory, how large is the (presumably) file from which you get fileLines? Commented Mar 9, 2016 at 20:38

2 Answers 2

4

First add the following usings so you don't need to type out the entire namespaces for everything.

using System.Text.RegularExpressions;
using System.IO;

The reason it doesn't work is because File.WriteAllLines expects a IEnumerabl<string> or a string[], but you're passing a string. Either use File.AppendAllText or just call File.WriteAllLines once after the foreach like this.

var lines = new List<string>();
foreach (string ss in fileLines)
{
    lines.Add(Regex.Replace(ss, @"\s+", " "));
}
MessageBox.Show("ok");
File.WriteAllLines(@"C:\Users\Public\WriteLines.txt", lines);

Or like this using Linq

File.WriteAllLines(
    @"C:\Users\Public\WriteLines.txt",
    fileLines.Select(ss => Regex.Replace(ss, @"\s+", " "));

But based on some of your comments you might want to do the following

File.WriteAllLines(
    @"C:\Users\Public\WriteLines.txt",
    File.ReadLines(@"InputPath")
        .Select(ss => Regex.Replace(ss, @"\s+", " "));

Because of lazy initialization that will read in each line of your input file one at a time, do the regular expression replacement then write the line out before moving to the next line. That will be scale-able if you need to run this on a very large file as it does not require the entire file to be in memory at one time.

Sign up to request clarification or add additional context in comments.

Comments

0

I am not sure about your requirement but it seems you are trying to use string in place of collection. This code may help.

List<string> lines = new List<string>();
foreach (string ss in fileLines)
{
   string filelinesclean = System.Text.RegularExpressions.Regex.Replace(ss, @"\s+", " ");
   lines.Add(filelinesclean);
}
System.IO.File.WriteAllLines(@"C:\Users\Public\WriteLines.txt", lines);

I have keep your code as it is and added/updated few lines.

1 Comment

There's no point in overwriting the file on each iteration of the loop.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.