Dart語言學習(四)Dart字符串


一、字符串的創建

使用 單引號,雙引號 創建字符串

使用 三個引號或雙引號 創建多行字符串

使用 r 創建原始 raw 字符串

  String str1 = 'Hello';//""
  String str2 = '''Hello
                  Dart''';//"""
  print(str1);
  print(str2);

//  String str3 = 'Hello \n Dart';
  String str3 = r'Hello \n Dart';
  print(str3);

輸出

Hello
Hello
                  Dart
Hello \n Dart

二、字符串操作

 

其他常用方法如下:

 

 

 

 

  String str4 = "This is my favorite language";
  print(str4 + "New");
  print(str4 * 5);
  print(str3 == str4);
  print(str4[1]);

  int a = 1;
  int b = 2;
  print("a + b = ${a + b}");
  print("a = $a");

  print(str4.length);
  print(str4.isEmpty);

  print(str4.contains("This"));
  print(str4.substring(0,3));
  print(str4.startsWith("a"));
  print(str4.endsWith("ge"));

  var list = str4.split(" ");
  print(list);

  print(str4.replaceAll("This", "That"));

輸出:

This is my favorite languageNew
This is my favorite languageThis is my favorite languageThis is my favorite languageThis is my favorite languageThis is my favorite language
false
h
a + b = 3
a = 1
28
false
true
Thi
false
true
[This, is, my, favorite, language]
That is my favorite language

 

Dart學習系列文章:https://www.cnblogs.com/jukaiit/category/1636484.html


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM