【試讀版】All In One Solidity
KrypltoCampChiHaoLu
  • Welcome!
  • | Chapter 0 | Intro
    • 【推薦序】
    • 【前言】
  • Part I Basic
    • | Chapter 1 | 基本介紹 Introduction
      • 【環境建置 Remix IDE】
      • 【版本控制】
      • 【Hello World & First Contract】
      • 【Practice】
      • 【Answer】
    • | Chapter 2 | 型別 Types & 變數 Variables
      • 【Integer】
      • 【Bool】
      • 【Address】
      • 【Contract & This】
      • 【String】
      • 【Scope】
      • 【Practice】
      • 【Answer】
    • | Chapter 3 | 單位 Unit & 運算子 Operators
      • 【單位】
      • 【Time】
      • 【運算子】
      • 【Practice】
      • 【Answer】
    • | Chapter 4 | 流程控制 Selection and Repetition
      • 【If-Else】
      • 【For】
      • 【While】
      • 【Practice】
      • 【Answer】
    • | Chapter 5 | 函式 Function
      • 【Returns】
      • 【Visibility】
      • 【stateMutability】
      • 【Constructor】
      • 【Function Overloading】
      • 【Fallback】
      • 【Useful Function】
      • 【Practice】
      • 【Answer】
    • | Chapter 6 | 資料結構 Data Structures
      • 【Array】
      • 【Mapping】
      • 【Structs】
      • 【Enum】
      • 【Practice】
      • 【Answer】
    • | Chapter 7 | 角色和全局訊息 Global Variables
      • 【Msg】
      • 【Block】
      • 【ABI】
      • 【TX】
      • 【Practice】
      • 【Answer】
  • Part II Medium
    • | Chapter 8 | 記憶體配置 Memory Allocation
    • | Chapter 9 | 異常處理 Handling Exception
      • 【Require】
      • 【Assert】
      • 【Revert】
      • 【Try/Catch】
      • 【Practice】
      • 【Answer】
    • | Chapter 10 | 事件 Event
      • 【emit】
      • 【Indexed】
      • 【web3.eth.subscribe()】
      • 【Logs with Data & Topics】
      • 【anonymous】
      • 【Practice】
      • 【Answer】
    • | Chapter 11 | 繼承 Inheritance
      • 【Inheritance】
      • 【Modifier】
      • 【合約互動】
      • 【Function Overriding】
      • 【Polymorphism】
      • 【多重繼承與 super】
      • 【Practice】
      • 【Answer】
    • | Chapter 12 | 介面 Interface
    • | Chapter 13 | 引用 Imports & 函式庫 Libraries
      • 【Library】
      • 【Import】
      • 【OpenZeppelin】
      • 【Practice】
      • 【Answer】
    • | Chapter 14 | ERC & Token
  • PART III Advanced
    • | Chapter 15 | 佈署 Deploy & 編譯 Compiler
    • | Chapter 16 | 開發工具 Dev. Tools
    • | Chapter 17 | 最佳化合約 Contract Optimization
  • | OTHERS | Information & Reference
    • 【結語】
    • 【參考資料】
Powered by GitBook
On this page

Was this helpful?

  1. Part I Basic
  2. | Chapter 2 | 型別 Types & 變數 Variables

【String】

在 Solidity 中和 python 一樣可以使用雙引號 "" 或者單引號 '' 來表示字串。也同樣可以用反斜槓來代表跳脫字元 "\"。

理想的情況下我們在宣告字串的時候可以使用 bytes 或者 uint<bytes> 來節省更多的 gas:

pragma solidity ^0.8.11;

contract SolidityTest {
    string data_moreGas = "test";
   bytes32 data_lessGas = "test";
}

常見用法

String 沒辦法直接比較,所以字串比較要用 keccak256 和 abi.encodePacked。

pragma solidity ^0.8.11;

contract String {

    function compare(string memory _name) public view returns(bool){
        if (keccak256(abi.encodePacked(_name)) == keccak256(abi.encodePacked("Vitalik"))){
            return true;
        }
        else{
            return false;
        }
    }
}

把兩個 String 連接起來:

pragma solidity ^0.8.11;

contract SolidityTest {
    function concat(string memory a, string memory b) public view returns(string memory){
        return string(abi.encodePacked(a, b));
    }
}

調用一個字串的長度可以使用以下方法:

pragma solidity ^0.8.11;

contract String {
    function len(string memory _name) public view returns(uint){
        return bytes(_name).length;
    }
}

String 如何在 Solidity 運作是一個非常深奧的事情,從 bytes 到 string 不僅僅只是 string() 這樣 casting 這麼簡單,之後我們在加密函數的時候再來詳述這個過程!

String vs. Bytes

  1. 兩者都屬於特殊的陣列型態

  2. String 某種程度上等於Bytes,但不具有長度和陣列取值的運算子([])

  3. String 較為昂貴

Previous【Contract & This】Next【Scope】

Last updated 3 years ago

Was this helpful?