Embedding a newline in a vb.net string constant

  • Thread starter Thread starter Andrew Backer
  • Start date Start date
A

Andrew Backer

Is it possible to embed a newline constant in a string in the same way
as c#? I really don't want to do something like this:
++ String.Format("Line 1 : {0} " & vbcrlf & " Line 2 : {1}", o, t)
when I should be able to do
++ String.Format("Line 1 : {0}\nLine2 : {2}", o, t)

Not the best example, I am sure, but it does get worse in many cases?
So, is there a way to do this in vb, or am I stuck? I have tried
googling but I can't find anything.

Thanks,
Andrew
 
Is it possible to embed a newline constant in a string in the same way
as c#? I really don't want to do something like this:
++ String.Format("Line 1 : {0} " & vbcrlf & " Line 2 : {1}", o, t)
when I should be able to do
++ String.Format("Line 1 : {0}\nLine2 : {2}", o, t)

Not the best example, I am sure, but it does get worse in many cases?
So, is there a way to do this in vb, or am I stuck? I have tried
googling but I can't find anything.

Thanks,
Andrew

Instead of vbcrlf use Environment.NewLine
 
The question is how do I embed it in the string.

If there is some magic regarding this the env constant that will make
this happen, I think I still need some illumination.

// Andrew
 
Andrew said:
Is it possible to embed a newline constant in a string in the same way
as c#? I really don't want to do something like this:
++ String.Format("Line 1 : {0} " & vbcrlf & " Line 2 : {1}", o, t)
when I should be able to do
++ String.Format("Line 1 : {0}\nLine2 : {2}", o, t)

Not the best example, I am sure, but it does get worse in many cases?
So, is there a way to do this in vb, or am I stuck? I have tried
googling but I can't find anything.

Thanks,
Andrew

The only way I can think of is:

String.Format("Line 1 : {0}{1}Line2 : {2}", o, vbcrlf, t)
 
The only way I can think of is:

String.Format("Line 1 : {0}{1}Line2 : {2}", o, vbcrlf, t)

I was hoping that wouldn't be the answer ;) Why add so much extra stuff
to the language but the leave off this kind of nicety?
 
I was hoping that wouldn't be the answer ;) Why add so much extra stuff
to the language but the leave off this kind of nicety?

Aren't you supposed to use a string builder for such tasks?
 
Rad said:
Instead of vbcrlf use Environment.NewLine

Yes and no. If you want to use a system-specific newline character
sequence, use 'Environment.NewLine', otherwise use an appropriate constant
in 'ControlChars' to enable the compiler to perform the concatenation at
compile time.
 
Homer said:
Aren't you supposed to use a string builder for such tasks?

Um, sure. Just ignore the code I wrote, and in any case that was not
the question. Is embedding a control character possible, in some way,
as in c#?

Also, look to stringbuilder vs. string performance for single use,
small cases.

// Andrew
You might be taken more seriously if you used your name.
 
The only way I can think of is:

String.Format("Line 1 : {0}{1}Line2 : {2}", o, vbcrlf, t)

This solution is even worse because one additional concatenation needs to be
performed at runtime!
 
Andrew Backer said:
Is it possible to embed a newline constant in a string in the same way
as c#? I really don't want to do something like this:
++ String.Format("Line 1 : {0} " & vbcrlf & " Line 2 : {1}", o, t)

What's the problem? The code is functionally completely equivalent to the
C# code if "\r\n" is embedded into the literal in the source code. Note
that the concatenation can be performed by the compiler because 'vbCrLf' is
a constant!
 
Herfried said:
This solution is even worse because one additional concatenation needs to be
performed at runtime!

I agree, I was just trying to show the OP the futility in his request.
 
I agree, I was just trying to show the OP the futility in his request.

Sorry I am futile, but in reality this is just a simple Yes (and here
is how) or No question. It got answered a while back that it is
probably not possible. To explain my frustration I will do this again
:

In c++/c/c#/php/just about everything you can embed control chars in a
string using a \.

Examples of this include, but are not limited to, \n \r \t.
An example : "This is the first line \n and this is the second \n\t and
this is tabbed in"

*** My question was, and still is, this :
Can I do this in VB.Net somehow. That is all.

----------------------------

In response I have gotten a lot of OT and other comments that really
don't answer the actual question. I know it is difficult to read the
OP, but even with performance in mind I am not sure how relevant the
remarks are. I will take the hit of an extra concatenation for easier
to read code.

Perhaps if this were in a loop of 10K + allocations I might ask a
question and phrase it as such : "What is the fastest way to so xxxx".
In this case, unless this is seriously the choke point in my
application and I am sure the rest of the damn framework is perfectly
optimized for this (And I am sure all the perf guys have analyzed the
IL) then I'll worry about the extra cycle. But it wouldn't matter
because it's just in an exception formatter, and is rarely called. I
know, I didn't say where it was. But that's because it wasn't the
question.
 
Okay, this is what everybody meant: No.

;-)

Robin S.
-------------------------------------
 
Andrew Backer wrote:
In c++/c/c#/php/just about everything you can embed control chars in a
string using a \.

Examples of this include, but are not limited to, \n \r \t.
An example : "This is the first line \n and this is the second \n\t and
this is tabbed in"

*** My question was, and still is, this :
Can I do this in VB.Net somehow. That is all.
<snip>

No, unfortunatelly it's not possible.

Just as it isn't possible to have multi-line strings, multi-line
comments and multiple results from a function...

=P

B.
 
David,

Unfortuataly?.

I wished that legacy from even before C was not in C#.

Cor
 
Andrew said:
I was hoping that wouldn't be the answer ;) Why add so much extra stuff
to the language but the leave off this kind of nicety?

But if you added the "nicety" of "\n", you suddenly have to deal with
all the mucking about that "\\" and all the other escape sequences
entail. Suddenly, all your [UNC] paths to files becomes aPITA:

sPath = "\\\\server\\share\\dir\\dir\\file.ext"

Or would we need some "escaping" character that tells VB we're using
this sort of this "C String"? How about a leading '@' sign?

sPath = @"\\\\server\\share\\dir1\\dir2\\file.ext"

sPath = "\\server\share\dir1\dir2\file.ext"

Oh, but that would confuse the C# programmers. We'd have to do it the
other way around and, in doing so, break every string literal we've ever
coded... :-(

Maybe Our Friends in Redmond will give us a CString class in the next
Framework (or, at least make String inheritable!)... ;-)

Regards,
Phill W.
 
David said:
Really? I find it is a very intuitive wysiwyg feature.

string myString = @"this
string extends over many lines and
has line breaks exactly as you see them here";

Which is fine for 'C'-like languages, where the end of statement marker
is an explicit ";", but not so well in VB, where end-of-line =
end-of-statement. :-)

Regards,
Phill W.
 
David Anton said:
Right - I can see why it's not done in VB, but the reason is purely
technical, not because anyone should find it confusing.

It's not even technical. It would be possible to extend the compiler to
support "continued" string literals.
 
Phil,

I agree, and know the annoyances that come with doing this kind of
escaping in C/etc. For fun I want you to try it in TSQL where you have
to use quotes, and then you need to embed other calls that also use
quotes. This is real issue where the procedure compiler won't let you
use variables for the parameter to openrowset(), so you embed and embed
and end up with 8 quotes in a row. It'll put hair on your chest an
take it off your head.

C# semi-fixed this with the @"c:\no\escape\chars\here" format, which is
really quite handy.

I don't expect VB to do anything exactly like this, or even do it by
default. It is technically (as H said) possible to do this, but I
didn't know if it had been done. Perhaps there was a way (and I tried
this) to prefix the string with @ so that @"in vb this \n is now a
newline" would work.

It was just a dream :) Back to the old ampersands, or pluses, I never
can remember which.

//Andrew
 
Back
Top