Home All Groups Group Topic Archive Search About

Embedding a newline in a vb.net string constant

Author
6 Dec 2006 6:08 PM
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

Author
6 Dec 2006 6:24 PM
Rad [Visual C# MVP]
On 6 Dec 2006 10:08:48 -0800, Andrew Backer wrote:

> 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
Author
6 Dec 2006 6:32 PM
Andrew Backer
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


Rad [Visual C# MVP] wrote:
Show quoteHide quote
> On 6 Dec 2006 10:08:48 -0800, Andrew Backer wrote:
>
> > 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
> --
> Bits.Bytes
> http://bytes.thinkersroom.com
Author
6 Dec 2006 9:23 PM
Herfried K. Wagner [MVP]
"Rad [Visual C# MVP]" <nospam@nospam.com> schrieb:
>> 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.
>
> 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.

--
M S   Herfried K. Wagner
M V P  <URL:http://dotnet.mvps.org/>
V B   <URL:http://dotnet.mvps.org/dotnet/faqs/>
Author
6 Dec 2006 6:45 PM
David Anton
Unfortunately, VB does not have a feature comparable to the verbatim string
literals of C#.  You are stuck with the syntax you mentioned.
--
David Anton
www.tangiblesoftwaresolutions.com
Instant C#: VB to C# converter
Instant VB: C# to VB converter
Instant C++: C#/VB to C++ converter
Instant Python: VB to Python converter


Show quoteHide quote
"Andrew Backer" wrote:

> 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
>
>
Author
7 Dec 2006 5:30 AM
Cor Ligthert [MVP]
David,

Unfortuataly?.

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

Cor

Show quoteHide quote
"David Anton" <DavidAn***@discussions.microsoft.com> schreef in bericht
news:C4BAC573-32A4-48C1-ACC9-4C0D999AD6A4@microsoft.com...
> Unfortunately, VB does not have a feature comparable to the verbatim
> string
> literals of C#.  You are stuck with the syntax you mentioned.
> --
> David Anton
> www.tangiblesoftwaresolutions.com
> Instant C#: VB to C# converter
> Instant VB: C# to VB converter
> Instant C++: C#/VB to C++ converter
> Instant Python: VB to Python converter
>
>
> "Andrew Backer" wrote:
>
>> 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
>>
>>
Author
7 Dec 2006 4:35 PM
David Anton
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";

--
David Anton
www.tangiblesoftwaresolutions.com
Instant C#: VB to C# converter
Instant VB: C# to VB converter
Instant C++: C#/VB to C++ converter
Instant Python: VB to Python converter


Show quoteHide quote
"Cor Ligthert [MVP]" wrote:

> David,
>
> Unfortuataly?.
>
> I wished that legacy from even before C was not in C#.
>
> Cor
>
> "David Anton" <DavidAn***@discussions.microsoft.com> schreef in bericht
> news:C4BAC573-32A4-48C1-ACC9-4C0D999AD6A4@microsoft.com...
> > Unfortunately, VB does not have a feature comparable to the verbatim
> > string
> > literals of C#.  You are stuck with the syntax you mentioned.
> > --
> > David Anton
> > www.tangiblesoftwaresolutions.com
> > Instant C#: VB to C# converter
> > Instant VB: C# to VB converter
> > Instant C++: C#/VB to C++ converter
> > Instant Python: VB to Python converter
> >
> >
> > "Andrew Backer" wrote:
> >
> >> 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
> >>
> >>
>
>
>
Author
7 Dec 2006 5:06 PM
Phill W.
David Anton wrote:
> 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.
Author
7 Dec 2006 5:19 PM
David Anton
Right - I can see why it's not done in VB, but the reason is purely
technical, not because anyone should find it confusing.
--
David Anton
www.tangiblesoftwaresolutions.com
Instant C#: VB to C# converter
Instant VB: C# to VB converter
Instant C++: C#/VB to C++ converter
Instant Python: VB to Python converter


Show quoteHide quote
"Phill W." wrote:

> David Anton wrote:
> > 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.
>
Author
7 Dec 2006 11:10 PM
Herfried K. Wagner [MVP]
"David Anton" <DavidAn***@discussions.microsoft.com> schrieb:
> 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.

--
M S   Herfried K. Wagner
M V P  <URL:http://dotnet.mvps.org/>
V B   <URL:http://dotnet.mvps.org/dotnet/faqs/>
Author
8 Dec 2006 12:25 AM
Andrew Backer
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

Herfried K. Wagner [MVP] wrote:
Show quoteHide quote
> "David Anton" <DavidAn***@discussions.microsoft.com> schrieb:
> > 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.
>
> --
>  M S   Herfried K. Wagner
> M V P  <URL:http://dotnet.mvps.org/>
>  V B   <URL:http://dotnet.mvps.org/dotnet/faqs/>
Author
6 Dec 2006 7:03 PM
zacks
Andrew Backer wrote:
> 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)
Author
6 Dec 2006 7:11 PM
Andrew Backer
za***@construction-imaging.com wrote:
Show quoteHide quote
> Andrew Backer wrote:
> > 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)

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?
Author
6 Dec 2006 8:35 PM
Homer J Simpson
"Andrew Backer" <awbac***@gmail.com> wrote in message
news:1165432302.604369.227830@79g2000cws.googlegroups.com...

> 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?
Author
6 Dec 2006 9:24 PM
Andrew Backer
Homer J Simpson wrote:
> "Andrew Backer" <awbac***@gmail.com> wrote in message
> news:1165432302.604369.227830@79g2000cws.googlegroups.com...
>
> > 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?

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.
Author
7 Dec 2006 11:31 AM
Phill W.
Andrew Backer wrote:
> za***@construction-imaging.com wrote:
>> Andrew Backer wrote:
>>> 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)
>>>
> 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.
Author
6 Dec 2006 9:24 PM
Herfried K. Wagner [MVP]
<za***@construction-imaging.com> schrieb:
>> 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.
>
> 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!

--
M S   Herfried K. Wagner
M V P  <URL:http://dotnet.mvps.org/>
V B   <URL:http://dotnet.mvps.org/dotnet/faqs/>
Author
6 Dec 2006 9:34 PM
zacks
Herfried K. Wagner [MVP] wrote:
Show quoteHide quote
> <za***@construction-imaging.com> schrieb:
> >> 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.
> >
> > 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!

I agree, I was just trying to show the OP the futility in his request.
Author
6 Dec 2006 10:26 PM
Andrew Backer
>
> 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.
Author
6 Dec 2006 11:03 PM
RobinS
Okay, this is what everybody meant: No.

;-)

Robin S.
-------------------------------------
Show quoteHide quote
"Andrew Backer" <awbac***@gmail.com> wrote in message
news:1165443983.886094.174140@l12g2000cwl.googlegroups.com...
> >
>> 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.
>
Author
7 Dec 2006 2:17 AM
Branco Medeiros
Andrew Backer wrote:
<snip>
> 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.
Author
6 Dec 2006 9:25 PM
Herfried K. Wagner [MVP]
"Andrew Backer" <awbac***@gmail.com> schrieb:
> 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!

--
M S   Herfried K. Wagner
M V P  <URL:http://dotnet.mvps.org/>
V B   <URL:http://dotnet.mvps.org/dotnet/faqs/>