This is a guide to learn the Dart programming language. Dart is a general-purpose programming language developed by Google. It is primarily used for building mobile, web, and desktop applications. This guide will cover the basics of Dart, including data types, string manipulation, type conversion, operators, control flow, and more.
- Data Types
- String Concatenation and Interpolation
- String Escape and Raw
- Multiline Strings
- Converting Case
- Finding and Replacing
- Type Conversion
- Operators
- Type Inference with
var
- Dynamic Type
- Control Flow
- String Multiplication
- Enum
- Import
Dart has several built-in data types:
int
: represents integer values.bool
: represents boolean values (true or false).double
: represents double-precision floating-point values.String
: represents a sequence of characters.
Example:
var name = 'bharat'; // String
var age = 20; // int
var student = true; // bool
var marks = 50.00; // double
print(name);
print(age);
print(student);
print(marks);
In Dart, you can concatenate strings using the +
operator or using string interpolation with the $
sign. String interpolation allows you to include expressions within a string.
Example:
var str1 = 'hello';
var str2 = 'world';
print(str1 + ' ' + str2); // string concatenation
print('$str1 $str2'); // string interpolation
int num = 10;
print('${num * 10 + 20}'); // evaluating expression in string interpolation
Dart provides string escape using \
and string raw using r
. String escape allows you to include special characters like quotes within a string. String raw allows you to include backslashes within a string.
Example:
print('today I\'m feeling good'); // string escape
print(r'c:\window\desktop'); // string raw
Dart supports multiline strings, which can span across multiple lines and preserve the formatting.
Example:
String s = """
Here we are printing
a multiline string
""";
print(s);
You can convert strings to uppercase or lowercase using the toUpperCase()
and toLowerCase()
methods, respectively.
Example:
var str = 'welcome to dart course';
print(str.toUpperCase()); // converting to uppercase
var man = 'WELCOME TO DART COURSE';
print(man.toLowerCase()); // converting to lowercase
var dis = 'operating system'.toUpperCase();
print(dis);
You can check if a string contains a specific substring using the contains()
method. To replace a substring with a new value, you can use the replaceAll()
method.
Example:
var news = "i love car";
print(news.contains('car')); // true
print(news.contains('bike')); // false
print(news.contains(' ')); //