Javatpoint Logo
Javatpoint Logo

Raw string literal in C++

What is a string literal?

An anonymous string[1] or string literal is a literal for a string value in a computer program's source code. As in x = "foo," where "foo" is a string literal with the value foo, modern computer languages frequently use a quoted series of characters, formally known as "bracketed delimiters." The issue of delimiter collision (problems with brackets) can be avoided by using techniques like escape sequences, which allow the delimiters to be contained in a string. There are numerous alternative notations available for defining string literals, particularly in complex situations. Depending on the computer language in the issue, the specific notation will vary. But most contemporary programming languages adhere to some broad rules.

What is a raw string literal?

Literals are variables whose values remain constant over the course of the program. Alternatively, a raw string literal is a string that does not handle C++ escape characters like'n, t or " '. As a result, a raw string literal that begins with R"(and ends in)" is created.

The syntax for Raw string Literal:

In this case, the delimiter is optional and can be any character other than the backslash (/), whitespace (), and parentheses ().

These raw string literals enable a succession of characters by precisely encoding their contents as raw character sequences.

Example:

Ordinary String Literal

A Raw String Literal differs from an Ordinary String Literal in the following ways:

Ordinary string literal Raw string literal
Nothing needs to be defined about it. To begin with the prefix R, it needs a defined line enclosed in parentheses ().
It excludes/does not permit nested characters. It allows/incorporates the implementation of nested characters.
It incorporates each character's unique attribute without disregarding their specific meaning. It handles all special characters like n and t like regular text and ignores them all.

Example of Raw String Literal:

What is paired delimiters in string literal?

Multiple languages allow for paired delimiters, where the opening and closing delimiters are distinct. Additionally, as they frequently permit nested strings, delimiters may be inserted as long as they are paired, although inserting an unpaired closing delimiter will still cause a delimiter collision. Examples are m4, which employs the apostrophe (') as the terminating delimiter, and PostScript, which uses parentheses, as in (The swift (brown fox)).Tcl supports both quotes (for interpolated strings) and braces (for raw strings), as in "The quick brown fox" or The quick brown fox. This is because blocks of code in Tcl are syntactically equivalent to string literals, and the paired nature of the delimiters is crucial to making this possible.

Single and double quotation marks are available in the Unicode character set in paired (different opening and closing) versions:

What is delimiter collision in string literal?

When employing quoting, the delimiter collision issue arises if you want to represent the delimiter itself in a string literal. For instance, if a double quote serves as the delimiter, one cannot simply represent a double quote by its literal "The second quote is interpreted as the end of the string literal rather than the string's value, so you cannot write "" or "This is "in quotes", but invalid." because the middle quote is interpreted as being outside of quotes. There are several options, the most universal of which is the use of escape sequences, like """ or "This is "in quotes" and properly escaped "however, there are other additional options.

The problem of delimiter collision is not resolved by paired quotes, such as braces in Tcl, because an imbalanced closing delimiter cannot simply be added, as in. Paired quotes do, however, allow nested strings, such as foo bar zork.

Escape sequences

Escape sequences, which have a long history, are a general method for representing characters that are otherwise difficult to represent directly, such as delimiters, nonprinting characters (like backspaces), newlines, and whitespace characters (which are otherwise impossible to distinguish visually). As a result, they are frequently employed in string literals. Adding an escape sequence (to a single character or to the entire string) is referred to as escaping.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA