Schlagwortarchiv für: Multiline

“JSON is a popular data-interchange format that has been widely adopted across the development community. However, JSON has strict schema and formatting tools.

JSON does not natively support multi-line strings. This can be quiebro frustrating when looking for flexibility and storing large texts.

Let us discuss a simple workaround and how we can use multiline strings.”

Using a JSON Array

A creative way of inserting a multi-line string in JSON is to use an array. We can separate each line as an individual element of the array.

For example, let us say we have a sample text as shown:

Lorem ipsum dolor sit amet. Aut eveniet sequi sit sequi dicta vel impedit architecto 33 voluptas tempore et optio libero qui molestiae possimus. Aut cupiditate animi ut commodi natus ut nesciunt mollitia ea ipsam iusto a ipsa odit a laboriosam neque et vero totam.

If we want to convert the above block of text into JSON, we can convert each line into an array element.

For example:

        «Lorem ipsum dolor sit amet.»,
        «Aut eveniet sequi sit sequi dicta vel impedit architecto,»,
        «33 voluptas tempore et optio libero qui molestiae possimus.»,
        «Aut cupiditate animi ut commodi natus ut nesciunt mollitia ea,»,
        «ipsam iusto a ipsa odit a laboriosam neque et vero totam.»

Note how each line of the above string is converted into an array element separated by a comma.

You can then use a method in your preferred programming language to construct the string back to its individual block.


This tutorial provides a simple but creative way of creating a multi-line string in JSON.

Source link

A string is a type of variable that stores data of characters, symbols, alphabets, etc. A multiline string is formed by joining single-line strings, forming a paragraph. It is unnecessary to have all the lines with the same alignment; we can use strings with different alignments to be stored in a single variable. It is the easiest way to make the long string get splitted into different small lines. We use quotes to enclose these lines. All these lines are initialized by using an alpha character along with them. Simply a multiline is created by joining single-line strings along with the spaces or newlines to form multiline strings. In this tutorial, we will use different approaches to create multiline strings in C sharp on the Linux operating system.”

Use of Multiline String

As for a single variable, there is no limit on the memory allocation like arrays or other structures, etc., so the benefit of using a single variable to store a multiline string is that it takes a small memory area instead of the memory used in declaring a single-line string in a separate string type variable separately.

Example 1

As the name indicates, a string will contain multiple lines in it. These lines can be the same or different having several characters in them. We have included a simple example here in which a string variable contains different lines. To execute the program, we first use two basic libraries here. The first one is the system library which includes all the classes and objects in the program. The second header file is used for the input and output streaming. Whenever we want to display anything or get input from the user, we use this library.

# Using System;

# Using System.IO;

Inside the main program, a string-type variable is declared. This contains three lines separately, making the strings a multiline string. Irrespective of the lines you add to the string, it is collectively a single variable.

There is no need to display the contents of the string separately. All the variables’ lines are displayed through the single variable by using its name. As through the console line, we have used only the variable’s name; all data will be accessed automatically. To apply a single-time double quote on all the string lines, we have used ‘@’ at the start of the strings.

Save the code and then execute it using the Linux terminal to display the results. Use the MCS compiler for the compilation of the C sharp code. After the compilation, Mico is used to execute the code.

$ MCS file.cs

$ mandril file.exe

On the execution, you will see that all the lines declared to the variable in the specified sequence are displayed in the same way when the code is executed. It means that whatever number of spaces you used or provide blank lines, the resultant will be the same as we used in the input variable.

Example 2

This example deals with having a long paragraph with 5 lines with symbols and characters. All the symbols and characters will be displayed because of using inverted commas. The string variable is initialized with “@” to add strings. Each line contains spaces in different places. It is unnecessary to end a single line where the sentence is finished. We have used arbitrary spaces between the words in lines and after the sentence has ended.

# String str = @»sample……».

The alpha sign will quote the whole paragraph.

A unique approach is used to display the content on the screen. This is through the variable and the textual. Literals are those contents used directly in the statement as it is. These are written with inverted quotes. So, use the sign alpha to display a new string with multiple characters and symbols.

Whereas the variable is the one that contains the content, as described above. So using the variable’s name directly in the console statement will display its contents as we did in the first example. Concatenation is the method used here to combine both the string variable and the textual. This is a built-in feature of string that is used to add two strings to one another. We will display the string with the variable using an operator ‘+’ to add two strings in the console statement.

On the execution, you will see that both the textual and the value inside the variable are displayed together. The sequence of the paragraph is maintained. The spaces and gaps between the lines are the same as when declaring the string.

Since it has been proved that we can use multiline strings to be displayed on the console terminal through concatenation.

Example 3

This example deals with displaying data individually through the console statement. All data types will be displayed collectively in a string. A date variable is used to have the DateTime.Now function that takes the current date from the system. Two double variables have float values inside them. And a constant integer type variable is used to align the text on the console screen.

At the time of execution, the first line will display the sum of two double values we have used. This addition will be done simply by using the ‘+’ operator. This resultant value will be used for the alignment in the next line. First, we have done the right alignment, and then the left alignment is used by the variable. In the last line, the string variable that contains the name is used along with the date function to display the current date from the system. The date is accompanied by the time taken from the Date Time function.

When we run the source code, you will see that all the strings directly used as Literals are added to the variables to get the content present inside them.

Example 4

Another example of the multiline string is used here in which three lines of strings are aligned differently. Each line contains strings and symbols surrounded by inverted commas. A single string variable will contain all the words. These all words are accompanied by the alpha sign.

We will use the variable in the console command to display all data.

You can see that all three lines are displayed through a single string variable.


A string is a variable that contains data in the form of characters and symbols. A multiline string is just like a simple string but with more than one line. There are several ways to create a multiline string. We have used different approaches in this aspect. A multiline string can be formed by using different lines collectively in a single variable. And also, by using a string statement at the time of display and variable, using the concatenation process to join variable and literals together. All the examples are implemented in Ubuntu operating system 20.04.

Source link