C++ to C#

  • Thread starter Thread starter Vai2000
  • Start date Start date
V

Vai2000

In VC++ if wanted to break a string on line I can use this
\n"

What's the equivalent in C#?

TIA
 
Vai2000 said:
In VC++ if wanted to break a string on line I can use this
\n"

What's the equivalent in C#?

System.Environment.NewLine. Typically this has the value \r\n.

C# recognizes the C character escapes \r and \n and they have the same
character equivalence that they have in C. C# in general does NOT follow
the C convention that a single \n indicates a line break, and on output
(e.g. to a file or to the console), .NET will NOT convert \n to \r\n as C
typically does when running on Windows.

-cd
 
Carl Daniel [VC++ MVP]
System.Environment.NewLine. Typically this has the value \r\n.

C# recognizes the C character escapes \r and \n and they have the same
character equivalence that they have in C.
Agreed.

C# in general does NOT follow
the C convention that a single \n indicates a line break

I wouldn't say that's a C convention particuarly - it's entirely
context sensitive. When writing to the console, at least on the build
of XP I'm running, \n is enough. It's not in a TextBox - on Windows.
and on output
(e.g. to a file or to the console), .NET will NOT convert \n to \r\n as C
typically does when running on Windows.

Yes - although again, that's the behaviour of *libraries* rather than
the language itself. As far as I'm aware, the C compiler itself won't
do any conversion of \n to \r\n.

(I know I'm being picky, but it can be important to realise the
boundary between language and library.)
 
sorry for poorly explaining in vb you can break a string by _ to the next
line
the quick brown _
fox jumps over the _
lazy dog

I am looking for equivalent of _ in c#

"the quick brown fox
 
Vai2000 said:
sorry for poorly explaining in vb you can break a string by _ to the next
line
the quick brown _
fox jumps over the _
lazy dog

I am looking for equivalent of _ in c#

That isn't breaking the string, that is a convention that indicates the code
continues on the next line. This is important in VB as it has no end of
processing line character other than the end of line. The similar in C#
would be:

string x = "the quick brown " +
"fox jumps over the " +
"lazy dog";
 
Jon said:
Carl Daniel [VC++ MVP]
System.Environment.NewLine. Typically this has the value \r\n.

C# recognizes the C character escapes \r and \n and they have the
same character equivalence that they have in C.
Agreed.

C# in general does NOT follow
the C convention that a single \n indicates a line break

I wouldn't say that's a C convention particuarly - it's entirely
context sensitive. When writing to the console, at least on the build
of XP I'm running, \n is enough. It's not in a TextBox - on Windows.
and on output
(e.g. to a file or to the console), .NET will NOT convert \n to \r\n
as C typically does when running on Windows.

Yes - although again, that's the behaviour of *libraries* rather than
the language itself. As far as I'm aware, the C compiler itself won't
do any conversion of \n to \r\n.

(I know I'm being picky, but it can be important to realise the
boundary between language and library.)

Picky yes, but correct - it's the CRT that does the conversion for C/C++
programs on windows. Confuses way more programmers than it ever helped,
IMO. Preserved for decades of backwards compatibility with the earliest of
DOS-based C compilers.

-cd
 
Vai2000 said:
sorry for poorly explaining in vb you can break a string by _ to the next
line
the quick brown _
fox jumps over the _
lazy dog

I am looking for equivalent of _ in c#

Try this ("@" is the trick):

myString = @"My
Multi
Line
Pre-Formatted
Literal
String
Without
Escapes
Or
\n";
Console.Write(myString);

~Jason

--
 
Carl said:
Picky yes, but correct - it's the CRT that does the conversion for C/C++
programs on windows. Confuses way more programmers than it ever helped,
IMO. Preserved for decades of backwards compatibility with the earliest of
DOS-based C compilers.

No. Done because that is what C/C++ does.

For text files the C/C++ RTL translated between the file
systems way of representing lines and the C/C++ way
of data and \n.

For DOS/Windows \n is translated to \r\n, but more
exotic translations can happen.

A VMS variable length file with one line of AB
is stored as 0x0002 'A' 'B' but fgetc still
reads 'A' 'B' '\n'.

Arne
 
Arne said:
No. Done because that is what C/C++ does.

It's the C/C++ runtime library that we're talking about here. The reasons
for the \r\n to \n conversion are entirely historical and not the least
technical. Had the first implementations of C on DOS stored text files with
only linefeeds as separators, then that's what VC++ would do today.

-cd
 
Arne said:
Carl said:
Jon Skeet [C# MVP] wrote:
Yes - although again, that's the behaviour of *libraries* rather
than the language itself. As far as I'm aware, the C compiler itself
won't do any conversion of \n to \r\n.

(I know I'm being picky, but it can be important to realise the
boundary between language and library.)

Picky yes, but correct - it's the CRT that does the conversion for
C/C++ programs on windows. Confuses way more programmers than it
ever helped, IMO. Preserved for decades of backwards compatibility
with the earliest of DOS-based C compilers.

No. Done because that is what C/C++ does.

It's the C/C++ runtime library that we're talking about here. The reasons
for the \r\n to \n conversion are entirely historical and not the least
technical. Had the first implementations of C on DOS stored text files with
only linefeeds as separators, then that's what VC++ would do today.

It has nothing to do with DOS. Similar conversion used to take place on
MacOS (before it got UNIXied) - \r was converted to \n. This is a quote
from C standard:

"A text stream is an ordered sequence of characters composed into lines,
each line consisting of zero or more characters plus a terminating new-line
[...] Characters may have to be added, altered, or deleted on input and
output to conform to differing conventions for representing text in the
host environment."
 
It's the C/C++ runtime library that we're talking about here. The reasons
for the \r\n to \n conversion are entirely historical and not the least
technical. Had the first implementations of C on DOS stored text files with
only linefeeds as separators, then that's what VC++ would do today.

It is specified so in the C and C++ standards.

And that convention is older than DOS.

And it is not even that big a problem on DOS/Windows (\r\n) and
MacOS (\r) - the real problem are on file systems where lines are
not delimited at all

Arne
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top