Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to convert an array of strings into one string?

In my program, I read in a file using this statement:

string[] allLines = File.ReadAllLines(dataFile);  

But I want to apply a Regex to the file as a whole (an example file is shown at the bottom) so I can eliminate certain stuff I'm not concerned with in the file. I can't use ReadAllText as I need to read it line by line for another purpose of the program (removing whitespace from each line).

Regex r = new Regex(@"CREATE TABLE [^\(]+\((.*)\) ON");  

(thanks to chiccodoro for this code)
This is the Regex that I want to apply.

Is there any way to change the array back into one text file? Or any other solution to the problem?
Things that pop into my mind is replacing the 'stuff' that I'm not concerned with with string.Empty.

example file

USE [Shelleys Other Database]
CREATE TABLE db.exmpcustomers(
    f_name varchar(100) NULL,
    l_name varchar(100) NULL,
    date_of_birth date NULL,
    house_number int NULL,
    street_name varchar(100) NULL
) ON [PRIMARY]
like image 343
New Start Avatar asked Sep 16 '10 13:09

New Start


3 Answers

You can join string[] into a single string like this

string strmessage=string.Join(",",allLines);

output :-a single , separated string.

like image 55
PrateekSaluja Avatar answered Oct 16 '22 06:10

PrateekSaluja


You can use String.Join():

string joined = String.Join(Environment.NewLine, allLines);

If you just want to write it back to the file, you can use File.WriteAllLines() and that works with an array.

like image 37
NullUserException Avatar answered Oct 16 '22 06:10

NullUserException


String.Join will concatenate all the members of your array using any specified seperator.

like image 41
rtalbot Avatar answered Oct 16 '22 05:10

rtalbot