Dart series: using numbers and strings in dart

brief introduction The easiest way to become familiar with a language is to become familiar with the various core librar...

brief introduction

The easiest way to become familiar with a language is to become familiar with the various core libraries provided by dart. Dart provides us with several common libraries, including dart:core,dart:async,dart:math,dart:convert,dart:html and dart:io.

Today, let's introduce the use of numbers and strings in dart:core.

number

Dart: three types of numbers are defined in the core, namely num,int and double.

Num is the sum of all numbers. int and double inherit from num and are subclasses of num.

In fact, dart:core also has a data type called bigint. Bigint is an independent data type and is not a subclass of num:

abstract class BigInt implements Comparable<BigInt>

The most common operation in numbers is to convert a string into a number. You can call the parse method. First, see the definition of the parse method in num:

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); }

The input can be decimal or hexadecimal, as shown below:

assert(int.parse('18') == 18); assert(int.parse('0x05') == 5); assert(double.parse('0.50') == 0.5);

num.parse will convert the corresponding characters into int or double types:

assert(num.parse('18') is int); assert(num.parse('0.50') is double);

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

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

We talked about how to convert a string into a number, and the following is how to convert a number into a string. num provides the toString() method, which can easily convert int and double into a string.

assert(18.toString() == '18'); assert(3.1415.toString() == '3.1415');

For decimals, tostrinasfixed can be used to specify the number of decimal places:

assert(3.1415.toStringAsFixed(2) == '3.14');

If you want to use scientific notation, you can use toStringAsPrecision:

assert(314.15.toStringAsPrecision(2) == '3.1e+2');
character string

All strings are encoded in UTF-16 in dart. The string in dart defines many common and very useful methods.

For example, query in 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);

To intercept a substring from a string:

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

Truncate the string according to specific characters:

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

So what is the corresponding Chinese support in dart? Because all characters in dart are represented by UTF-16, if a UTF-16 unit can represent the corresponding characters, there is no problem in using Chinese:

assert('how are you?'.substring(1,2) == 'good'); assert('how are you?'[1] == 'good');

However, some characters cannot be represented by a UTF-16 unit. At this time, the characters package needs to be used to process specific characters.

Convert string to uppercase or lowercase:

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

dart provides a trim() method to intercept the spaces at the front and back of the string:

assert(' www.flydean.com '.trim() == 'www.flydean.com');
StringBuffer

In addition to the displayed string to create characters, dart also provides a StringBuffer class. Through the StringBuffer class, we can create strings freely:

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

The above code output: "www.flydean.com is very good."

writeAll() connects the incoming character array with a specific connector.

summary

The above is the introduction of numbers and strings in dart.

This article has been included in http://www.flydean.com/14-dart-number-string/

The most popular interpretation, the most profound dry goods, the most concise tutorial, and many tips you don't know are waiting for you to find!

Welcome to my official account: "those things in procedure", understand technology, know you better!

23 November 2021, 21:01 | Views: 8135

Add new comment

For adding a comment, please log in
or create account

0 comments