summaryrefslogtreecommitdiff
path: root/devdocs/elisp/modifying-strings.html
blob: 4ea7ee3ec81602dc4d9994ce2f68a361ac8e2243 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
 <h3 class="section">Modifying Strings</h3>   <p>You can alter the contents of a mutable string via operations described in this section. See <a href="mutability">Mutability</a>. </p> <p>The most basic way to alter the contents of an existing string is with <code>aset</code> (see <a href="array-functions">Array Functions</a>). <code>(aset <var>string</var> <var>idx</var> <var>char</var>)</code> stores <var>char</var> into <var>string</var> at character index <var>idx</var>. It will automatically convert a pure-<acronym>ASCII</acronym> <var>string</var> to a multibyte string (see <a href="text-representations">Text Representations</a>) if needed, but we recommend to always make sure <var>string</var> is multibyte (e.g., by using <code>string-to-multibyte</code>, see <a href="converting-representations">Converting Representations</a>), if <var>char</var> is a non-<acronym>ASCII</acronym> character, not a raw byte. </p> <p>A more powerful function is <code>store-substring</code>: </p> <dl> <dt id="store-substring">Function: <strong>store-substring</strong> <em>string idx obj</em>
</dt> <dd>
<p>This function alters part of the contents of the specified <var>string</var>, by storing <var>obj</var> starting at character index <var>idx</var>. The argument <var>obj</var> may be either a character (in which case the function behaves exactly as <code>aset</code>) or a (smaller) string. If <var>obj</var> is a multibyte string, we recommend to make sure <var>string</var> is also multibyte, even if it’s pure-<acronym>ASCII</acronym>. </p> <p>Since it is impossible to change the number of characters in an existing string, it is en error if <var>obj</var> consists of more characters than would fit in <var>string</var> starting at character index <var>idx</var>. </p>
</dd>
</dl> <p>To clear out a string that contained a password, use <code>clear-string</code>: </p> <dl> <dt id="clear-string">Function: <strong>clear-string</strong> <em>string</em>
</dt> <dd><p>This makes <var>string</var> a unibyte string and clears its contents to zeros. It may also change <var>string</var>’s length. </p></dd>
</dl><div class="_attribution">
  <p class="_attribution-p">
    Copyright &copy; 1990-1996, 1998-2022 Free Software Foundation, Inc. <br>Licensed under the GNU GPL license.<br>
    <a href="https://www.gnu.org/software/emacs/manual/html_node/elisp/Modifying-Strings.html" class="_attribution-link">https://www.gnu.org/software/emacs/manual/html_node/elisp/Modifying-Strings.html</a>
  </p>
</div>