How to assign a block of text to a string in C#

  • Thread starter Thread starter Richard Frenkel
  • Start date Start date
R

Richard Frenkel

I'd like to embed a bunch of text in a c# source file and assign it to
a string.
something like:

----------------------------
string x = <<eof

This is a bunch of text
that is assigned to string x
until the "eof" on a line
by itself is encountered.

eof
-------------------------------

of course anyone using Perl will recongize this very convenient
syntax. Otherwise one has to go thru:

x = 'blah blah';
x += 'more blah blah";
etc which is tedious. Just adding all the quotes is tedious. And if
there are quotes in the text block it's even MORE tedious, and tedium
is something I don't care for as a programer. So is there a way to do
this in the C# compiler?
 
string x =
@"


some
text


";

If you need to embed quote marks, then double them up using ""
 
I am not sure if this is exactly correct but try.

string myString = null;
TextReader tr = File.OpenText(@"C:\test\sometext.txt");
myString = tr.ReadToEnd();


clintonG said:
The StringBuilder Class [1] is the only practical class offering the
functionality you need to achieve the objective.
--
<%= Clinton Gallagher
A/E/C Consulting, Web Design, e-Commerce Software Development
Wauwatosa, Milwaukee County, Wisconsin USA
NET (e-mail address removed)
URL http://www.metromilwaukee.com/clintongallagher/

[1]
http://msdn.microsoft.com/library/d...ml/frlrfsystemtextstringbuilderclasstopic.asp






Richard Frenkel said:
I'd like to embed a bunch of text in a c# source file and assign it to
a string.
something like:

----------------------------
string x = <<eof

This is a bunch of text
that is assigned to string x
until the "eof" on a line
by itself is encountered.

eof
-------------------------------

of course anyone using Perl will recongize this very convenient
syntax. Otherwise one has to go thru:

x = 'blah blah';
x += 'more blah blah";
etc which is tedious. Just adding all the quotes is tedious. And if
there are quotes in the text block it's even MORE tedious, and tedium
is something I don't care for as a programer. So is there a way to do
this in the C# compiler?
 
One idiea that I've had that haven't followed up on is to have an imbedded
text file. Then just read the text file when needed. I know there's a way.
Also if it comes down to it you could right a very basic program that auto
wrote all those "x += " for you. Just two textboxes. Probably not what
you're wanting though.
 
Back
Top