“This is the 20th day of my participation in the Gwen Challenge in November. Check out the details: The Last Gwen Challenge in 2021.”

Introduction to the

The easiest way to become familiar with a language is to become familiar with the various core libraries that DART provides. Dart provides us including dart: core, dart: async, dart: math, dart: convert, dart: HTML and dart: IO this several common library.

Today I’ll introduce you to the use of numbers and strings in DART: Core.

digital

Dart: Core defines three types of numbers: num,int, and double.

Num is the general term for all numbers. Int and double are subclasses of num.

In fact, dart: Core also has a data type called BigInt, which is a separate data type and not a subclass of num:

abstract class BigInt implements Comparable<BigInt>
Copy the code

The most common operation on numbers is to convert a string to a number. This can be done by calling the parse method.

static num parse(String input, [@deprecated num onError(String input)?] ) { num? result = tryParse(input); if (result ! = null) return result; if (onError == null) throw FormatException(input); return onError(input); }Copy the code

The input passed in can be either decimal or hexadecimal, as follows:

assert(int.parse('18') == 18); assert(int.parse('0x05') == 5); Assert (double. Parse (' 0.50 ') = = 0.5).Copy the code

Num. Parse converts the corresponding character to an int or double:

assert(num.parse('18') is int); Assert (num. Parse (' 0.50 ') is a double);Copy the code

The parse method can also pass in the cardinality of the string, such as decimal or hexadecimal:

assert(int.parse('11', radix: 16) == 17);
Copy the code

Num provides the toString() method to easily convert ints and doubles to strings.

assert(18.toString() == '18'); Assert (3.1415. The toString () = = '3.1415');Copy the code

For decimals, toStringAsFixed can be used to specify the number of digits:

Assert (3.1415 toStringAsFixed (2) = = '3.14');Copy the code

To use scientific notation, use toStringAsPrecision:

Assert (314.15 toStringAsPrecision (2) = = '3.1 e+2');Copy the code

string

All strings are encoded in UTF-16 in DART, and The String in DART defines a number of common and useful methods.

Such as a query in a string:

assert('www.flydean.com'.contains('flydean'));

assert('www.flydean.com'.startsWith('www'));

assert('www.flydean.com'.endsWith('com'));

assert('www.flydean.com'.indexOf('flydean') == 4);
Copy the code

Intercepting a substring from a string:

assert('www.flydean.com'.substring(4, 11) == 'flydean');
Copy the code

To intercept a string by a particular character:

var parts = 'www.flydean.com'.split('.');
assert(parts.length == 3);
Copy the code

What does dart support for Chinese look like? Since all characters in DART are represented in UTF-16, if a UTF-16 unit can represent the corresponding character, Chinese can be used without problem:

Assert (' How are you? '. Substring (1,2) == 'ok '); Assert (' How are you? '[1] == 'good ');Copy the code

However, some characters cannot be represented using a UTF-16 unit, so you need to use the characters package to handle specific characters.

Converting a string to uppercase or lowercase:

assert('www.flydean.com'.toUpperCase() ==
    'WWW.FLYDEAN.COM');

// Convert to lowercase.
assert('WWW.FLYDEAN.COM'.toLowerCase() ==
    'www.flydean.com');
Copy the code

Dart provides the trim() method, which intercepts whitespace at the front and back of a string:

assert('  www.flydean.com  '.trim() == 'www.flydean.com');
Copy the code

StringBuffer

Dart also provides a StringBuffer class, from which we can create strings freely:

var sb = StringBuffer(); sb .. write('www.flydean.com ') .. writeAll(['is', 'very', 'good'], ' ') .. write('.'); var fullString = sb.toString();Copy the code

“www.flydean.com is very good.”

Where writeAll() concatenates the character array passed in with a specific concatenator.

conclusion

That’s the introduction to numbers and strings in DART.

This article is available at www.flydean.com/14-dart-num…

The most popular interpretation, the most profound dry goods, the most concise tutorial, many tips you didn’t know waiting for you to discover!

Welcome to pay attention to my public number: “procedures those things”, understand technology, more understand you!