Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Strings in Carbon
3.
Simple And Block String Literals
3.1.
Simple String Literal
3.2.
Block String Literal
4.
Escape Sequences
5.
Frequently Asked Questions 
5.1.
What is Carbon?
5.2.
Differentiate between Simple String Literals and Block String Literals.
5.3.
Explain Block String Literal in detail.
5.4.
Which rules are followed in Simple String Literal?
5.5.
When is a content line called empty?
6.
Conclusion
Last Updated: Mar 27, 2024
Easy

Strings in Carbon

Author Geetika Dua
0 upvote
Leveraging ChatGPT - GenAI as a Microsoft Data Expert
Speaker
Prerita Agarwal
Data Specialist @
23 Jul, 2024 @ 01:30 PM

Introduction

Do you know what Carbon means? Carbon-Lang is an experimental, general-purpose programming language. This project is open-source and was introduced by Google.

Are you aware of working with Strings in Carbon? Don’t worry! We got you covered.

Let us learn about strings in carbon in this blog. Keep Reading!

Introduction

Let us study Strings in Carbon in detail.

Strings in Carbon

If we talk about Strings in Carbon, this language accepts both simple and block literals. Simple literals are single-line and use one double quotation mark (“). Block Literals are multi-line and they use three single quotation marks (""").

Let us understand this concept of strings in carbon with the help of a code.

package sample api;

fn Main() -> i32 {

    // Simple string literal
    var s: String = "Coding";
    Print(s);

    // Block string literal
    var block: auto = """
        We are Coding Ninjas
        We help you grow!
        Keep learning with us.
    """;
    Print(block);

    // Block string literal with file type indicator
    var code_block: auto = """cpp
        #include <iostream>
        int main() {
            std::cout << "Let's Code!";
            return 0;
        }
    """;
    Print(code_block);

    return 1;
}


Output:

Output
Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Simple And Block String Literals

Let us look into simple and block string literals.

Simple String Literal

A simple string literal follows these rules-

  • Characters except for \ and “.
    Only space characters are accepted while printing whitespace in a string literal.
     
  • Escape Sequence 
    An escape sequence in Carbon language is a sequence of characters that doesn't represent itself when used inside a string literal or character. 
    A corresponding character sequence or code unit sequence is used in place of every escape sequence. 
     

For example, this is a simple string literal:

package sample api;

fn Main() -> i32 {
    var ABC: auto = "The proofs, my lord, are valid.";
    Print(ABC);
    return 1;
}


Output:

String output
package sample api;

fn Main() -> i32 {
    // The three adjacent simple string literals `""`, `"Code"` and `""` are not permitted.
    var block: auto = """Code""";
    Print(block);
    return 1;
}


Output:

Error output

It is important to note that the string literals with triple-double quotation marks """ are adjacent string literals. We should reject them directly.

Now let us study block string literal.

Block String Literal

A block string literal starts with '''.The lines written between the opening and closing lines (exclusive) are called content lines. The content lines do not have\ characters. They do not form part of an escape sequence. An escape sequence in Carbon language is a sequence of characters that doesn't represent itself when used inside a string literal or character. 

The content of the literal is formed according to these rules:

  • The indentation of the ending line is removed from each non-empty content line.
     
  • All trailing whitespace on every line is replaced with a (U+000A) character.
     
  • The resulting lines are concatenated.
     

A content line is called empty if it contains only whitespace characters.

Escape Sequences

The following escape sequences are accepted in string literal-

Escape

Meaning

\t This means U+0009 CHARACTER TABULATION.
\n It signifies U+000A LINE FEED.
\r This escape sequence means U+000D CARRIAGE RETURN.
\" The meaning of this escape function is U+0022 QUOTATION MARK (").
\' It means U+0027 APOSTROPHE (').
\\ This escape sequence signifies U+005C REVERSE SOLIDUS (\)
\0 The meaning here is Code unit with value 0
\0D. It conveys that it is Invalid and reserved for evolution.
\xHH It signifies a code unit with value HH16
\u{HHHH...} This means it is a Unicode code point U+HHHH...
\<newline> No string literal content produced (block literals only)

Frequently Asked Questions 

What is Carbon?

Carbon-Lang is an experimental, general-purpose programming language. This project is open-source and was introduced by Google.

Differentiate between Simple String Literals and Block String Literals.

Simple literals are single-line and use one double quotation mark (“). Block Literals are multi-line, and they use three single quotation marks (""").

Explain Block String Literal in detail.

A block string literal starts with ("""). The lines written between the opening and closing lines (exclusive) are called content lines. The content lines do not have\ characters.

Which rules are followed in Simple String Literal?

Only space characters are accepted whitespace in a string literal. In case of an escape  Sequence, a Corresponding character sequence or code unit sequence is used in place of every escape sequence.

When is a content line called empty?

A content line is called empty if it contains only whitespace characters.

Conclusion

In this article, we studied Strings in Carbon. We hope that this clears your concepts on this topic. Want to know more about Carbon? Keep an eye out for our other blogs on Carbon.

Refer to our guided paths on Coding Ninjas Studio to learn more about DSA, Competitive Programming, JavaScript, System Design, etc. Enroll in our courses and refer to the mock test and problems available. Take a look at the interview experiences and interview bundle for placement preparations.

Happy Coding!

Live masterclass