<html><head><title>Strings</title><linkrel="stylesheet"type="text/css"href="/static/style.css"></head><body><divclass="sidebar"><divclass="header"><divclass="logo"><ahref="/"><divclass="image"></div><divclass="name">ethers</div><divclass="version">v5.0-beta</div></a></div></div><divclass="toc"><div><divclass="link title"><ahref="/">Documentation</a></div><divclass="base show link depth-1"><ahref="/getting-started/">Getting Started</a></div><divclass="hide link depth-2"><ahref="/getting-started/#installing">Installing</a></div><divclass="hide link depth-2"><ahref="/getting-started/#importing">Importing</a></div><divclass="base show link depth-1"><ahref="/concepts/">Concepts</a></div><divclass="hide link depth-2"><ahref="/concepts/events/">Events</a></div><divclass="hide link depth-3"><ahref="/concepts/events/#solidity-topics">Solidity Topics</a></div><divclass="hide link depth-2"><ahref="/concepts/gas/">Gas</a></div><divclass="hide link depth-3"><ahref="/concepts/gas/#gas-price">Gas Price</a></div><divclass="hide link depth-3"><ahref="/concepts/gas/#gas-limit">Gas Limit</a></div><divclass="base ancestor show link depth-1"><ahref="/api/">Application Programming Interface</a></div><divclass="show link depth-2"><ahref="/api/contract/">Contracts</a></div><divclass="hide link depth-3"><ahref="/api/contract/#buckets">Buckets</a></div><divclass="show link depth-2"><ahref="/api/signer/">Signers</a></div><divclass="hide link depth-3"><ahref="/api/signer/#signer">Signer</a></div><divclass="hide link depth-3"><ahref="/api/signer/#wallet-inherits-signer">Wallet inherits Signer</a></div><divclass="show link depth-2"><ahref="/api/providers/">Providers</a></div><divclass="hide link depth-3"><ahref="/api/providers/provider/">Provider</a></div><divclass="hide link depth-4"><ahref="/api/providers/provider/#accounts-methods">Accounts Methods</a></div><divclass="hide link depth-4"><ahref="/api/providers/provider/#blocks-methods">Blocks Methods</a></div><divclass="hide link depth-4"><ahref="/api/providers/provider/#ethereum-naming-service-(ens)-methods">Ethereum Naming Service (ENS) Methods</a></div><divclass="hide link depth-4"><ahref="/api/providers/provider/#logs-methods">Logs Methods</a></div><divclass="hide link depth-4"><ahref="/api/providers/provider/#network-status-methods">Network Status Methods</a></div><divclass="hide link depth-4"><ahref="/api/providers/provider/#transactions-methods">Transactions Methods</a></div><divclass="hide link depth-4"><ahref="/api/providers/provider/#event-emitter-methods">Event Emitter Methods</a></div><divclass="hide link depth-4"><ahref="/api/providers/provider/#inspection-methods">Inspection Methods</a></div><divclass="hide link depth-3"><ahref="/api/providers/jsonrpc-provider/">JSON-RPC Provider</a></div><divclass="hide link depth-4"><ahref="/api/providers/jsonrpc-provider/#jsonrpcprovider">JsonRpcProvider</a></div><divclass="hide link depth-4"><ahref="/api/providers/jsonrpc-provider/#jsonrpcsigner">JsonRpcSigner</a></div><divclass="hide link depth-4"><ahref="/api/providers/jsonrpc-provider/#jsonrpcuncheckedsigner">JsonRpcUncheckedSigner</a></div><divclass="hide link depth-3"><ahref="/api/providers/api-providers/">API Providers</a></div><divclass="hide link depth-4"><ahref="/api/providers/api-providers/#etherscanprovider">EtherscanProvider</a></div><divclass="hide link depth-4"><ahref="/api/providers/api-providers/#infuraprovider">InfuraProvider</a></div><divclass="hide link depth-4"><ahref="/api/providers/api-providers/#nodesmithprovider">NodesmithProvider</a></div><divclass="hide link depth-4"><ahref="/api/providers/api-providers/#alchemyprovider">AlchemyProvider</a></div><divclass="hide link depth-4"><ahref="/api/providers/api-providers/#cloudfrontprovider">CloudfrontProvider</a></div><divclass="hide link depth-3"><ahref="/api/providers/other/">Other Providers</a></div><divclass="hide link depth-4"><ahref="/api/providers/other/#fallbackprovider">FallbackProvider</a></div><divclass="hide link depth-4"><ahref="
<p>A string in Solidity is length prefixed with its 256-bit (32 byte) length, which means that even short strings require 2 words (64 bytes) of storage.</p><p>In many cases, we deal with short strings, so instead of prefixing the string with its length, we can null-terminate it and fit it in a single word (32 bytes). Since we need only a single byte for the null termination, we can store strings up to 31 bytes long in a word.</p>
<divclass="definition container-box note show-anchors"><divclass="term">Note</div><divclass="body"><p>Strings that are 31 <u><i>bytes</i></u> long may contain fewer than 31 <u><i>characters</i></u>, since UTF-8 requires multiple bytes to encode international characters.</p></div></div>
<aname="utils-parsebytes32"></a><divclass="property show-anchors"><divclass="signature"><spanclass="path">utils</span> . <spanclass="method">parseBytes32String</span> ( <spanclass="param">aBytesLike</span> ) <spanclass="arrow">⇒</span><spanclass="returns">string</span><divclass="anchors"><aclass="self"href="#utils-parsebytes32"></a><aclass="source"href="https://github.com/ethers-io/ethers.js/blob/ethers-v5-beta/packages/strings/src.ts/bytes32.ts#L21">source</a></div></div><divclass="body"><p>Returns the decoded string represented by the <codeclass="inline">Bytes32</code> encoded data.</p></div></div>
<aname="utils-formatbytes32"></a><divclass="property show-anchors"><divclass="signature"><spanclass="path">utils</span> . <spanclass="method">formatBytes32String</span> ( <spanclass="param">text</span> ) <spanclass="arrow">⇒</span><spanclass="returns">string</span><divclass="anchors"><aclass="self"href="#utils-formatbytes32"></a><aclass="source"href="https://github.com/ethers-io/ethers.js/blob/ethers-v5-beta/packages/strings/src.ts/bytes32.ts#L9">source</a></div></div><divclass="body"><p>Returns a <codeclass="inline">bytes32</code> string representation of <i>text</i>. If the length of <i>text</i> exceeds 31 bytes, it will throw an error.</p></div></div>
<aname="utils-toutf8bytes"></a><divclass="property show-anchors"><divclass="signature"><spanclass="path">utils</span> . <spanclass="method">toUtf8Bytes</span> ( <spanclass="param">text</span> [ , <spanclass="param">form</span> = <spanclass="default-value">c</span><spanclass="param">urrent</span> ] ) <spanclass="arrow">⇒</span><spanclass="returns">Uint8Array</span><divclass="anchors"><aclass="self"href="#utils-toutf8bytes"></a><aclass="source"href="https://github.com/ethers-io/ethers.js/blob/ethers-v5-beta/packages/strings/src.ts/utf8.ts#L123">source</a></div></div><divclass="body"><p>Returns the UTF-8 bytes of <i>text</i>, optionally normalizing it using the <ahref="/api/utils/strings/#unicode-normalization-form">UnicodeNormalizationForm</a><i>form</i>.</p></div></div>
<aname="utils-toutf8codepoints"></a><divclass="property show-anchors"><divclass="signature"><spanclass="path">utils</span> . <spanclass="method">toUtf8CodePoints</span> ( <spanclass="param">aBytesLike</span> [ , <spanclass="param">form</span> = <spanclass="default-value">c</span><spanclass="param">urrent</span> ] ) <spanclass="arrow">⇒</span><spanclass="returns">Array< number ></span><divclass="anchors"><aclass="self"href="#utils-toutf8codepoints"></a><aclass="source"href="https://github.com/ethers-io/ethers.js/blob/ethers-v5-beta/packages/strings/src.ts/utf8.ts#L214">source</a></div></div><divclass="body"><p>Returns the Array of codepoints of <i>aBytesLike</i>, optionally normalizing it using the <ahref="/api/utils/strings/#unicode-normalization-form">UnicodeNormalizationForm</a><i>form</i>.</p><p><b>Note:</b> This function correctly splits each user-perceived character into its codepoint, accounting for surrogate pairs. This should not be confused with <codeclass="inline">string.split("")</code>, which destroys surrogate pairs, spliting between each UTF-16 codeunit instead.</p></div></div>
<aname="utils-toutf8string"></a><divclass="property show-anchors"><divclass="signature"><spanclass="path">utils</span> . <spanclass="method">toUtf8String</span> ( <spanclass="param">aBytesLike</span> [ , <spanclass="param">ignoreErrors</span> = <spanclass="default-value">f</span><spanclass="param">alse</span> ] ) <spanclass="arrow">⇒</span><spanclass="returns">string</span><divclass="anchors"><aclass="self"href="#utils-toutf8string"></a><aclass="source"href="https://github.com/ethers-io/ethers.js/blob/ethers-v5-beta/packages/strings/src.ts/utf8.ts#L210">source</a></div></div><divclass="body"><p>Returns the string represented by the UTF-8 bytes of <i>aBytesLike</i>. This will throw an error for invalid surrogates, overlong sequences or other UTF-8 issues, unless <i>ignoreErrors</i> is specified.</p></div></div>
<p>There are several <ahref="https://en.wikipedia.org/wiki/Unicode_equivalence">commonly used forms</a> when normalizing UTF-8 data, which allow strings to be compared or hashed in a stable way.</p>
<divclass="property show-anchors"><divclass="signature"><spanclass="path">utils</span> . <spanclass="path">UnicodeNormalizationForm</span> . <spanclass="method">current</span><divclass="anchors"></div></div><divclass="body"><p>Maintain the current normalization form.</p></div></div>
<divclass="property show-anchors"><divclass="signature"><spanclass="path">utils</span> . <spanclass="path">UnicodeNormalizationForm</span> . <spanclass="method">NFC</span><divclass="anchors"></div></div><divclass="body"><p>The Composed Normalization Form. This form uses single codepoints which represent the fully composed character.</p><p>For example, the <b>é</b> is a single codepoint, <codeclass="inline">0x00e9</code>.</p></div></div>
<divclass="property show-anchors"><divclass="signature"><spanclass="path">utils</span> . <spanclass="path">UnicodeNormalizationForm</span> . <spanclass="method">NFD</span><divclass="anchors"></div></div><divclass="body"><p>The Decomposed Normalization Form. This form uses multiple codepoints (when necessary) to compose a character.</p><p>For example, the <b>é</b> is made up of two codepoints, <codeclass="inline">"0x0065"</code> (which is the letter <codeclass="inline">"e"</code>) and <codeclass="inline">"0x0301"</code> which is a special diacritic UTF-8 codepoint which indicates the previous character should have an acute accent.</p></div></div>
<divclass="property show-anchors"><divclass="signature"><spanclass="path">utils</span> . <spanclass="path">UnicodeNormalizationForm</span> . <spanclass="method">NFKC</span><divclass="anchors"></div></div><divclass="body"><p>The Composed Normalization Form with Canonical Equivalence. The Canonical representation folds characters which have the same syntactic representation but different semantic meaning.</p><p>For example, the Roman Numeral <b>I</b>, which has a UTF-8 codepoint <codeclass="inline">"0x2160"</code>, is folded into the capital letter I, <codeclass="inline">"0x0049"</code>.</p></div></div>
<divclass="property show-anchors"><divclass="signature"><spanclass="path">utils</span> . <spanclass="path">UnicodeNormalizationForm</span> . <spanclass="method">NFKD</span><divclass="anchors"></div></div><divclass="body"><p>The Decomposed Normalization Form with Canonical Equivalence. See NFKC for more an example.</p></div></div>
<divclass="definition container-box note show-anchors"><divclass="term">Note</div><divclass="body"><p>Only certain specified characters are folded in Canonical Equivalence, and thus it should <b>not</b> be considered a method to acheive <i>any</i> level of security from <ahref="https://en.wikipedia.org/wiki/IDN_homograph_attack">homoglyph attacks</a>.</p></div></div><divclass="footer"><divclass="nav previous"><ahref="/api/utils/hashing/"><spanclass="arrow">←</span>Hashing Algorithms</a></div><divclass="nav next"><ahref="/cookbook/">Cookbook<spanclass="arrow">→</span></a></div></div><divclass="copyright">The content of this site is licensed under the <ahref="https://choosealicense.com/licenses/cc-by-4.0/">Creative Commons Attribution 4.0 International License</a>.</div></div><scriptsrc="/script.js"type="text/javascript"></script></body></html><!-- ContentHash:515fa34697826167bf5ea59d44c306e0246564bd6ac655aabe8f8228cff91a0e -->