Introduction

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

Today I will introduce you to the use of numbers and strings in dart:core.

# number

Three types of numbers are defined in dart:core, namely num, int and double.

num is the general term for all numbers. Both int and double inherit from num and are subclasses of num.

In fact, there is also a data type called BigInt in dart:core. BigInt is an independent data type, not a subclass of num:

abstract class BigInt implements Comparable<BigInt>

The most common operation in numbers is to convert a string to a number. The conversion can call the parse method. First look at 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 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 character into int or double type:

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

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

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

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

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

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

For decimals, you can use toStringAsFixed 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');

String

All strings are encoded in UTF-16 in dart. String in dart defines many commonly used and very useful methods.

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

To intercept a substring from a string:

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

To intercept 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 expressed in UTF-16, if a UTF-16 unit can represent the corresponding character, then Chinese is no problem in use:

  assert('你好吗?'.substring(1,2) == '好');
  assert('你好吗?'[1] == '好');

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

Convert the 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 the trim() method, which can intercept the spaces before and after the string:

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

StringBuffer

In addition to the displayed string to create characters, dart also provides the StringBuffer class, through which 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."

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

Summarize

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 discover!

Welcome to pay attention to my official account: "Program those things", know technology, know you better!


flydean
890 声望433 粉丝

欢迎访问我的个人网站:www.flydean.com