Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Line breaks not counted as 2 characters towards the length of a string

Tags:

c#

I am wondering why \n is not counted as 2 characters when determining the length of a string in C#

EX: This is a test comment\nSample text test 123456\nadssssssssss\n\nasdasdasda\nasdadadadad\nasadad\nasada\n\n\nLast paragraph\n12345\nTest

The above string contains 136 characters, but the Length property of the String object I store the string in counts \n as 1 chartacter, and reports the string as 124 characters long.

I want to extract a substring based on pre calculated start and lenght markers. The markers were created counting \n as two characters. How could I do the equivilent of the following in C#

select SUBSTRING('This is a test comment\nSample text test 123456\nadssssssssss\n\nasdasdasda\nasdadadadad\nasadad\nasada\n\n\nLast paragraph\n12345\nTest',1,136);

I can't use substring since my lenght offset of 136 is outside the boundaries of the c# representation of the string.

like image 517
TGH Avatar asked Nov 20 '12 23:11

TGH


People also ask

Does a line break count as a character?

As described in issue #1264 the length calculation counts a line break as one character. Javascript returns / stores a line break as \r instead of the often needed \r\n (e.g. in a windows environment).

How many characters is a line break?

In Windows and DOS, the line break code is two characters: a carriage return followed by a line feed (CR/LF).

What is the character for line break?

CR and LF are control characters or bytecode that can be used to mark a line break in a text file.

How do you represent a line break in a string?

In Windows, a new line is denoted using “\r\n”, sometimes called a Carriage Return and Line Feed, or CRLF. Adding a new line in Java is as simple as including “\n” , “\r”, or “\r\n” at the end of our string.


1 Answers

String.Length does include newlines:

string test = "1234\n6789\n\nC";

Console.WriteLine(test);
Console.WriteLine("Length: {0}", test.Length);

Output:

1234
6789

C    
Length: 12

What you may be missing is that '\n' is one character. It represents the newline character (LF). The backslash in a string literal indicates it is an Escape Sequence.

So even though you count 136 characters, the \n is replaced with a single newline character when it is compiled.


Note, depending on where this string is being used, you may want to consider using Environment.NewLine instead of \n. The true newline on Windows is "\r\n", or CR LF. While many controls, etc. will handle the \n fine, files and other things may expect \r\n.

string test = "Welcome to" + Environment.NewLine + "StackOverflow!";
like image 170
Jonathon Reinhart Avatar answered Sep 29 '22 07:09

Jonathon Reinhart