Re: C# Multi-line String Literals

Hi: OK, for example: If you are building a string to be passed to a Database 
for execution you would pass it one string without any line breaks in the 
string itself or use a parameterized query. If you are just creating lines of 
code to print out or display in a browser or in a textbox then you would use a 
string with line breaks so each segment of the string would be displayed on 
individual lines in the browser or TextBox. When you  use the StringBuilder or 
the plus or and signs to append text to a string you are not creating a 
multi-line string you are just making the string longer and longer but it is 
still only one string.
For Example:
MyString = "abc" + "def" the contents of the MyString variable would be 
"abcdef".
This is called string concatenation.You could use several lines of code to 
build a string variable contents:
Dim MyString As String
MyString = "abc" + _
"def" + _
"123"
and the MyString would be:
"abcdef123".
You can add special characters to the string like line breaks like:
MyString = "abc" + VbCrlf + "def".
Now the value would be:
"abc(a line break ascii code)def".
ThThat would still be one line of text in storage at the address of the 
MyString variable but would appear as 2 lines of text when displayed in a 
TextBox.
The LineBreak character would likely throw an error if you include it in the 
string and then tried to pass it to a Database Engine for execution. It is only 
for displaying the string in a TextBox or other user display control. 
You can concatenate strings like above or:
Dim MyString As String
MyString = "abc"
MyString = MyString + "def"
My String = MyString + "123"
and the result:
MyString = "abcdef123".
Are you planning your string to just be displayed in a TextBox or for a reader 
to view or you actually planning on using it as a query string to pass to a 
Database? That is what will determine if you use the line breaks in the string. 
There is no such thing as a multi-line string. You can use several lines of 
code to create a string and you can put line break characters, tabs and other 
special characters inside a string but the string itself is only one long 
grouping of characters, letters, numbers and special characters.
What are you trying to do once you have the string built? Use it in a  TextBox 
or other display object or pass it to a Database for execution?
Rick USA
 
  ----- Original Message ----- 
  From: Kerneels Roos 
  To: programmingblind@xxxxxxxxxxxxx 
  Sent: Wednesday, August 11, 2010 5:03 AM
  Subject: Re: C# Multi-line String Literals


  Thanks for all the info Rick. I don't mind long emails if they are 
interesting and convey good info ha ha!

  Oh yeah, I totally agree with you about actual values which one will do via 
parameterised queries. What I meant with multi line strings is simply that, for 
a big query you don't want to put it all on one line -- it becomes very hard to 
read it like that. Splitting it across lines is a much better approach. 
Previously I thought that the "+" string concatenation operator was very slow, 
but now it looks like it's not really a problem if the string is a constant, 
meaning, the value is totally known at compile time.

  Some guy inspected the actual byte code and saw that the compiler is smart 
enough to make one long string if it's just a constant string value, even if 
you use "+" operators. The "@" operator for denoting very literal string is 
definately also treated like that by the compiler with the exception that you 
introduce a glut of white space and new line chars since the compiler can 
surely not remove those, that would attack the integrity of your code itself or 
data rather.

  StringBuilder is for sure by far the best way to go if you are going to 
append to a string in a loop or something -- append a variable number of times, 
but my question is just what about if you are appending a fixed number of 
times, like when you want to display some sql string literal more reader 
friendly, is the StringBuilder approach still more eficient? It cant' b.

  Regards


  On Wed, Aug 11, 2010 at 10:43 AM, RicksPlace <ofbgmail@xxxxxxxxx> wrote:

    Hi: OK, first a StringBuilder creates one variable to hold the string. No 
matter how many times you change that string you will have one variable and 
that will save space and perform faster than using a standard string variable 
if you change the value of the StringBuilder object more than a few hundred 
times or so. A standard String variable is created new each time you change the 
contents of the string so you can get a bunch of variables created if you 
change the value held in the string. Now, it looks like you are trying to build 
a Database Query String in a StringBuilder or String Variable. In your example 
you include the where clause and the value to test for "5" if I remember in the 
string itself. This is not how you would creat4e a Query String if you are 
going to have diferent values for a where clause or other clauses that might 
have values that would change. You can build a query string and use it by 
passing the string and a value to the DB Engine but the correct method would be 
to use a Parameterized query. In your example the query might execute ok but 
what if you wanted to use a text value in the where clause? Then you would have 
to use single quotes or, depending on the DB Engine settings use Single quotes 
for the query string and double quotes for the value. You want to use a 
Parameterized query if you are learning to work with a Sql Server or Sql CE 
Database, likely an Access DB or other DB if Parameterized querys are supported 
for non MS Databases as well. 
    So, you can use either StringBuilder or a standard string to build a 
string. By the way, I am not sure about Multi-Line strings, a string is one 
long line of text as far as I know. You can include line breaks in a string but 
the string itself is one long line of text. If you want multi-lines you would 
use a list object or a table or array object to hold each line of text. A list 
might look like:
    List.( Of String). Then you can use the list of strings by using the 
methods and properties of the List Object like the Append, For Each, Remove and 
all that jazz. But, it sounds like you just want to build either a standard 
query string or want to use Parameterized queries for the code you are working 
on. PS, my list object code is from vb.net but I believe it is a Framework 
object so should be the same in C# or other VS Languages. Well, sorry this is 
sort of long but a simple answer is that there is actually no such thing as a 
multi-line string. You can code several lines to build a string and variious 
objects that hold strings like an array or list but each string is one long 
group of characters so I don't understand the question exactly.
    In the MS Docs the StringBuilder AppendLine just addes a line break in the 
long string you are building:
    According to the MS Docs:
    StringBuilder.AppendLine Method
    Appends the default line terminator to the end of the current
    StringBuilder
    Well, my answer is to use the simple string object unless you will be 
changing the string variable more than say 50 or 100 times without destroying 
it by leaving the class or method where it is defined so you don't build 
thousands of variables, one each time you change the value of the string.
    Rick USA
     ----- Original Message ----- 
      From: Kerneels Roos 
      To: programmingblind 
      Sent: Wednesday, August 11, 2010 3:57 AM
      Subject: C# Multi-line String Literals


      Hi List,

      Three ways to do this, which one is best:

      1.
      StringBuilder sb = new StringBuilder();
      sb.AppendLine("SELECT id ");
      sb.AppendLine("FROM my_table ");
      sb.AppendLine("WHERE id > 5");

      2.
      string myMultiLine = "SELECT id "
         + "FROM my_table"
         + "WHERE id >5";

      3.
      string myMultiLine = @"SELECT id
         FROM my_table
         WHERE id > 5";

      Method 1 is the usual way, 2 introduces extra whitespace and lime breaks, 
and 3 apparently does not result in slower code if the whole string is known at 
compile time -- the compiler is smart enough to make it into one string (see: 
http://jameskovacs.com/2007/02/12/multiline-strings-in-c/ ).

      If you know that you are going to have to tweak the string and have a few 
round trips to the query analyzer I would say that 2 would actually be best 
since you only have to remove the first @" and last "; and the literal string 
will run. Once you know the string is perfect I would say method 3 is the best 
since it looks like it might be faster than method 1 and does not have the 
extra, maybe unwanted additional whitespace and new line chars that method 2 
introduces.

      What do you guys think?

      Regards,

      -- 
      Kerneels Roos
      Cell/SMS: +27 (0)82 309 1998
      Skype: cornelis.roos

      The early bird may get the worm, but the second mouse gets the cheese!






  -- 
  Kerneels Roos
  Cell/SMS: +27 (0)82 309 1998
  Skype: cornelis.roos

  The early bird may get the worm, but the second mouse gets the cheese!


Other related posts: