Braintique.com header
Left Navigation Bar

Verbatim Strings

In the first part of this article, I mentioned that you could use the @ symbol to use a keyword as an identifier—if you were so inclined, you could create variables named, for example, @if, @string, and @true. (To repeat what I said back in Part I, just because one can do something, doesn’t mean it is a good idea.)

In the context of strings, the @ symbol is used to create verbatim string literals. The @ symbol tells the string constructor to use the string literal that follows it “literally”—even if it includes escape characters or spans multiple lines.

This comes in handy when working with directory paths (without the @, you would have to double each backslash). For example, the following two strings are equivalent:

string noat = "\\\\BIGSERVER\\C";
string withat = @"\\BIGSERVER\C";

Here’s how you could use the @ symbol to take the place of the \r\n escape sequence of control characters, which means “carriage return, new line”:

private void btnVer_Click(object sender, System.EventArgs e) {
string str =
@"It's fun
to be
split into a number
of lines!";
txtDoIt.Text = str;
}

If you assign the verbatim string to a TextBox that has its Multiline property set to True, you’ll see that the line breaks (and white spaces) are preserved exactly as entered in the verbatim string.

TextBox

If you are curious, here’s how you’d create the same string as a non-verbatim string, using the control characters and string concatenation:

string str = "It's fun\r\n to be\r\n"+
" split into a number\r\n" +
" of lines!";
txtDoIt.Text = str;

Previous Table of Contents Next


Google
 
Web www.braintique.com
www.digitalfieldguide.com www.googleplexblog.com


Home | Barticles | Blogs | Books | Services | FAQ | Contact

© Braintique.com. All rights reserved.

Search Engine Optimization







RSS 2.0 Syndication feed

Syndication Viewer

Our Web host:
IX WebHosting



Food for Your Brain! Get a Barticle! Questions Answered Books for You What We Can Do For You Contact Us Brain Food Questions Answered Books for You What We Can Do For You Frequently Asked Questions About Us Google Research Photoshop Wi-Fi and Wireless Networking The Natural Way to Write