Declare A String In Dart
Code snippet for how to Declare A String In Dart with sample and detail explanation
Dart, a client-optimized programming language for apps on multiple platforms, offers several ways to declare a string. This article will guide the reader through the process with simplicity and ease.
Declare a String in Dart: Code Snippet
In Dart, a string can be declared in several ways, but the most common way of declaring a string is by using single quotes or double quotes as shown in the following example:
void main() {
String str1 = 'Hello';
String str2 = "World";
print(str1);
print(str2);
}
Code Explanation for Declare a String in Dart
The program begins with a declaration of the main()
function, which happens to be the point at which our program will begin executing. Inside this function, two strings are declared - str1
and str2
.
In Dart, we use the String
keyword to signify that we are dealing with string variable types. str1
is set equal to ‘Hello’, and str2
is set equal to “World”. Note that strings in Dart can be declared either inside single quotes (’ ’) or double quotes (” ”).
The next lines of code are calls to the print()
function, which displays the string value stored in str1
and str2
.
So, when you run this program, the output in your console will be:
Hello
World
This reflects the values we had assigned to str1
and str2
respectively, proving that our string declaration was successful. Whether you opt to use single or double quotes to declare your string in Dart, the concept is just as straightforward, and your choice largely depends on your personal preference or the project requirements.