1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023
|
<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE erlref SYSTEM "erlref.dtd">
<erlref>
<header>
<copyright>
<year>1996</year><year>2018</year>
<holder>Ericsson AB. All Rights Reserved.</holder>
</copyright>
<legalnotice>
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
</legalnotice>
<title>string</title>
<prepared>Robert Virding</prepared>
<responsible>Bjarne Däcker</responsible>
<docno>1</docno>
<approved>Bjarne Däcker</approved>
<checked></checked>
<date>1996-09-28</date>
<rev>A</rev>
<file>string.xml</file>
</header>
<module>string</module>
<modulesummary>String processing functions.</modulesummary>
<description>
<p>This module provides functions for string processing.</p>
<p>A string in this module is represented by <seealso marker="unicode#type-chardata">
<c>unicode:chardata()</c></seealso>, that is, a list of codepoints,
binaries with UTF-8-encoded codepoints
(<em>UTF-8 binaries</em>), or a mix of the two.</p>
<code>
"abcd" is a valid string
<<"abcd">> is a valid string
["abcd"] is a valid string
<<"abc..åäö"/utf8>> is a valid string
<<"abc..åäö">> is NOT a valid string,
but a binary with Latin-1-encoded codepoints
[<<"abc">>, "..åäö"] is a valid string
[atom] is NOT a valid string</code>
<p>
This module operates on grapheme clusters. A <em>grapheme cluster</em>
is a user-perceived character, which can be represented by several
codepoints.
</p>
<code>
"å" [229] or [97, 778]
"e̊" [101, 778]</code>
<p>
The string length of "ß↑e̊" is 3, even though it is represented by the
codepoints <c>[223,8593,101,778]</c> or the UTF-8 binary
<c><<195,159,226,134,145,101,204,138>></c>.
</p>
<p>
Grapheme clusters for codepoints of class <c>prepend</c>
and non-modern (or decomposed) Hangul is not handled for performance
reasons in
<seealso marker="#find/3"><c>find/3</c></seealso>,
<seealso marker="#replace/3"><c>replace/3</c></seealso>,
<seealso marker="#split/2"><c>split/2</c></seealso>,
<seealso marker="#lexemes/2"><c>split/2</c></seealso> and
<seealso marker="#trim/3"><c>trim/3</c></seealso>.
</p>
<p>
Splitting and appending strings is to be done on grapheme clusters
borders.
There is no verification that the results of appending strings are
valid or normalized.
</p>
<p>
Most of the functions expect all input to be normalized to one form,
see for example <seealso marker="unicode#characters_to_nfc_list/1">
<c>unicode:characters_to_nfc_list/1</c></seealso>.
</p>
<p>
Language or locale specific handling of input is not considered
in any function.
</p>
<p>
The functions can crash for non-valid input strings. For example,
the functions expect UTF-8 binaries but not all functions
verify that all binaries are encoded correctly.
</p>
<p>
Unless otherwise specified the return value type is the same as
the input type. That is, binary input returns binary output,
list input returns a list output, and mixed input can return a
mixed output.</p>
<code>
1> string:trim(" sarah ").
"sarah"
2> string:trim(<<" sarah ">>).
<<"sarah">>
3> string:lexemes("foo bar", " ").
["foo","bar"]
4> string:lexemes(<<"foo bar">>, " ").
[<<"foo">>,<<"bar">>]</code>
<p>This module has been reworked in Erlang/OTP 20 to
handle <seealso marker="unicode#type-chardata">
<c>unicode:chardata()</c></seealso> and operate on grapheme
clusters. The <seealso marker="#oldapi"> <c>old
functions</c></seealso> that only work on Latin-1 lists as input
are still available but should not be used, they will be
deprecated in a future release.
</p>
</description>
<datatypes>
<datatype>
<name name="direction"/>
<name name="grapheme_cluster"/>
<desc>
<p>A user-perceived character, consisting of one or more
codepoints.</p>
</desc>
</datatype>
</datatypes>
<funcs>
<func>
<name name="casefold" arity="1"/>
<fsummary>Convert a string to a comparable string.</fsummary>
<desc>
<p>
Converts <c><anno>String</anno></c> to a case-agnostic
comparable string. Function <c>casefold/1</c> is preferred
over <c>lowercase/1</c> when two strings are to be compared
for equality. See also <seealso marker="#equal/4"><c>equal/4</c></seealso>.
</p>
<p><em>Example:</em></p>
<pre>
1> <input>string:casefold("Ω and ẞ SHARP S").</input>
"ω and ss sharp s"</pre>
</desc>
</func>
<func>
<name name="chomp" arity="1"/>
<fsummary>Remove trailing end of line control characters.</fsummary>
<desc>
<p>
Returns a string where any trailing <c>\n</c> or
<c>\r\n</c> have been removed from <c><anno>String</anno></c>.
</p>
<p><em>Example:</em></p>
<pre>
182> <input>string:chomp(<<"\nHello\n\n">>).</input>
<<"\nHello">>
183> <input>string:chomp("\nHello\r\r\n").</input>
"\nHello\r"</pre>
</desc>
</func>
<func>
<name name="equal" arity="2"/>
<name name="equal" arity="3"/>
<name name="equal" arity="4"/>
<fsummary>Test string equality.</fsummary>
<desc>
<p>
Returns <c>true</c> if <c><anno>A</anno></c> and
<c><anno>B</anno></c> are equal, otherwise <c>false</c>.
</p>
<p>
If <c><anno>IgnoreCase</anno></c> is <c>true</c>
the function does <seealso marker="#casefold/1">
<c>casefold</c>ing</seealso> on the fly before the equality test.
</p>
<p>If <c><anno>Norm</anno></c> is not <c>none</c>
the function applies normalization on the fly before the equality test.
There are four available normalization forms:
<seealso marker="unicode#characters_to_nfc_list/1"> <c>nfc</c></seealso>,
<seealso marker="unicode#characters_to_nfd_list/1"> <c>nfd</c></seealso>,
<seealso marker="unicode#characters_to_nfkc_list/1"> <c>nfkc</c></seealso>, and
<seealso marker="unicode#characters_to_nfkd_list/1"> <c>nfkd</c></seealso>.
</p>
<p>By default,
<c><anno>IgnoreCase</anno></c> is <c>false</c> and
<c><anno>Norm</anno></c> is <c>none</c>.</p>
<p><em>Example:</em></p>
<pre>
1> <input>string:equal("åäö", <<"åäö"/utf8>>).</input>
true
2> <input>string:equal("åäö", unicode:characters_to_nfd_binary("åäö")).</input>
false
3> <input>string:equal("åäö", unicode:characters_to_nfd_binary("ÅÄÖ"), true, nfc).</input>
true</pre>
</desc>
</func>
<func>
<name name="find" arity="2"/>
<name name="find" arity="3"/>
<fsummary>Find start of substring.</fsummary>
<desc>
<p>
Removes anything before <c><anno>SearchPattern</anno></c> in <c><anno>String</anno></c>
and returns the remainder of the string or <c>nomatch</c> if <c><anno>SearchPattern</anno></c> is not
found.
<c><anno>Dir</anno></c>, which can be <c>leading</c> or
<c>trailing</c>, indicates from which direction characters
are to be searched.
</p>
<p>
By default, <c><anno>Dir</anno></c> is <c>leading</c>.
</p>
<p><em>Example:</em></p>
<pre>
1> <input>string:find("ab..cd..ef", ".").</input>
"..cd..ef"
2> <input>string:find(<<"ab..cd..ef">>, "..", trailing).</input>
<<"..ef">>
3> <input>string:find(<<"ab..cd..ef">>, "x", leading).</input>
nomatch
4> <input>string:find("ab..cd..ef", "x", trailing).</input>
nomatch</pre>
</desc>
</func>
<func>
<name name="is_empty" arity="1"/>
<fsummary>Check if the string is empty.</fsummary>
<desc>
<p>Returns <c>true</c> if <c><anno>String</anno></c> is the
empty string, otherwise <c>false</c>.</p>
<p><em>Example:</em></p>
<pre>
1> <input>string:is_empty("foo").</input>
false
2> <input>string:is_empty(["",<<>>]).</input>
true</pre>
</desc>
</func>
<func>
<name name="length" arity="1"/>
<fsummary>Calculate length of the string.</fsummary>
<desc>
<p>
Returns the number of grapheme clusters in <c><anno>String</anno></c>.
</p>
<p><em>Example:</em></p>
<pre>
1> <input>string:length("ß↑e̊").</input>
3
2> <input>string:length(<<195,159,226,134,145,101,204,138>>).</input>
3</pre>
</desc>
</func>
<func>
<name name="lexemes" arity="2"/>
<fsummary>Split string into lexemes.</fsummary>
<desc>
<p>
Returns a list of lexemes in <c><anno>String</anno></c>, separated
by the grapheme clusters in <c><anno>SeparatorList</anno></c>.
</p>
<p>
Notice that, as shown in this example, two or more
adjacent separator graphemes clusters in <c><anno>String</anno></c>
are treated as one. That is, there are no empty
strings in the resulting list of lexemes.
See also <seealso marker="#split/3"><c>split/3</c></seealso> which returns
empty strings.
</p>
<p>Notice that <c>[$\r,$\n]</c> is one grapheme cluster.</p>
<p><em>Example:</em></p>
<pre>
1> <input>string:lexemes("abc de̊fxxghix jkl\r\nfoo", "x e" ++ [[$\r,$\n]]).</input>
["abc","de̊f","ghi","jkl","foo"]
2> <input>string:lexemes(<<"abc de̊fxxghix jkl\r\nfoo"/utf8>>, "x e" ++ [$\r,$\n]).</input>
[<<"abc">>,<<"de̊f"/utf8>>,<<"ghi">>,<<"jkl\r\nfoo">>]</pre>
</desc>
</func>
<func>
<name name="lowercase" arity="1"/>
<fsummary>Convert a string to lowercase</fsummary>
<desc>
<p>
Converts <c><anno>String</anno></c> to lowercase.
</p>
<p>
Notice that function <seealso marker="#casefold/1"><c>casefold/1</c></seealso>
should be used when converting a string to
be tested for equality.
</p>
<p><em>Example:</em></p>
<pre>
2> <input>string:lowercase(string:uppercase("Michał")).</input>
"michał"</pre>
</desc>
</func>
<func>
<name name="next_codepoint" arity="1"/>
<fsummary>Pick the first codepoint.</fsummary>
<desc>
<p>
Returns the first codepoint in <c><anno>String</anno></c>
and the rest of <c><anno>String</anno></c> in the tail. Returns
an empty list if <c><anno>String</anno></c> is empty or an
<c>{error, String}</c> tuple if the next byte is invalid.
</p>
<p><em>Example:</em></p>
<pre>
1> <input>string:next_codepoint(unicode:characters_to_binary("e̊fg")).</input>
[101|<<"̊fg"/utf8>>]</pre>
</desc>
</func>
<func>
<name name="next_grapheme" arity="1"/>
<fsummary>Pick the first grapheme cluster.</fsummary>
<desc>
<p>
Returns the first grapheme cluster in <c><anno>String</anno></c>
and the rest of <c><anno>String</anno></c> in the tail. Returns
an empty list if <c><anno>String</anno></c> is empty or an
<c>{error, String}</c> tuple if the next byte is invalid.
</p>
<p><em>Example:</em></p>
<pre>
1> <input>string:next_grapheme(unicode:characters_to_binary("e̊fg")).</input>
["e̊"|<<"fg">>]</pre>
</desc>
</func>
<func>
<name name="nth_lexeme" arity="3"/>
<fsummary>Pick the nth lexeme.</fsummary>
<desc>
<p>Returns lexeme number <c><anno>N</anno></c> in
<c><anno>String</anno></c>, where lexemes are separated by
the grapheme clusters in <c><anno>SeparatorList</anno></c>.
</p>
<p><em>Example:</em></p>
<pre>
1> <input>string:nth_lexeme("abc.de̊f.ghiejkl", 3, ".e").</input>
"ghi"</pre>
</desc>
</func>
<func>
<name name="pad" arity="2"/>
<name name="pad" arity="3"/>
<name name="pad" arity="4"/>
<fsummary>Pad a string to given length.</fsummary>
<desc>
<p>
Pads <c><anno>String</anno></c> to <c><anno>Length</anno></c> with
grapheme cluster <c><anno>Char</anno></c>.
<c><anno>Dir</anno></c>, which can be <c>leading</c>, <c>trailing</c>,
or <c>both</c>, indicates where the padding should be added.
</p>
<p>By default, <c><anno>Char</anno></c> is <c>$\s</c> and
<c><anno>Dir</anno></c> is <c>trailing</c>.
</p>
<p><em>Example:</em></p>
<pre>
1> <input>string:pad(<<"He̊llö"/utf8>>, 8).</input>
[<<72,101,204,138,108,108,195,182>>,32,32,32]
2> <input>io:format("'~ts'~n",[string:pad("He̊llö", 8, leading)]).</input>
' He̊llö'
3> <input>io:format("'~ts'~n",[string:pad("He̊llö", 8, both)]).</input>
' He̊llö '</pre>
</desc>
</func>
<func>
<name name="prefix" arity="2"/>
<fsummary>Remove prefix from string.</fsummary>
<desc>
<p>
If <c><anno>Prefix</anno></c> is the prefix of
<c><anno>String</anno></c>, removes it and returns the
remainder of <c><anno>String</anno></c>, otherwise returns
<c>nomatch</c>.
</p>
<p><em>Example:</em></p>
<pre>
1> <input>string:prefix(<<"prefix of string">>, "pre").</input>
<<"fix of string">>
2> <input>string:prefix("pre", "prefix").</input>
nomatch</pre>
</desc>
</func>
<func>
<name name="replace" arity="3"/>
<name name="replace" arity="4"/>
<fsummary>Replace a pattern in string.</fsummary>
<desc>
<p>
Replaces <c><anno>SearchPattern</anno></c> in <c><anno>String</anno></c>
with <c><anno>Replacement</anno></c>.
<c><anno>Where</anno></c>, default <c>leading</c>, indicates whether
the <c>leading</c>, the <c>trailing</c> or <c>all</c> encounters of
<c><anno>SearchPattern</anno></c> are to be replaced.
</p>
<p>Can be implemented as:</p>
<pre>lists:join(Replacement, split(String, SearchPattern, Where)).</pre>
<p><em>Example:</em></p>
<pre>
1> <input>string:replace(<<"ab..cd..ef">>, "..", "*").</input>
[<<"ab">>,"*",<<"cd..ef">>]
2> <input>string:replace(<<"ab..cd..ef">>, "..", "*", all).</input>
[<<"ab">>,"*",<<"cd">>,"*",<<"ef">>]</pre>
</desc>
</func>
<func>
<name name="reverse" arity="1"/>
<fsummary>Reverses a string</fsummary>
<desc>
<p>
Returns the reverse list of the grapheme clusters in <c><anno>String</anno></c>.
</p>
<p><em>Example:</em></p>
<pre>
1> Reverse = <input>string:reverse(unicode:characters_to_nfd_binary("ÅÄÖ")).</input>
[[79,776],[65,776],[65,778]]
2> <input>io:format("~ts~n",[Reverse]).</input>
ÖÄÅ</pre>
</desc>
</func>
<func>
<name name="slice" arity="2"/>
<name name="slice" arity="3"/>
<fsummary>Extract a part of string</fsummary>
<desc>
<p>Returns a substring of <c><anno>String</anno></c> of
at most <c><anno>Length</anno></c> grapheme clusters, starting at position
<c><anno>Start</anno></c>.</p>
<p>By default, <c><anno>Length</anno></c> is <c>infinity</c>.</p>
<p><em>Example:</em></p>
<pre>
1> <input>string:slice(<<"He̊llö Wörld"/utf8>>, 4).</input>
<<"ö Wörld"/utf8>>
2> <input>string:slice(["He̊llö ", <<"Wörld"/utf8>>], 4,4).</input>
"ö Wö"
3> <input>string:slice(["He̊llö ", <<"Wörld"/utf8>>], 4,50).</input>
"ö Wörld"</pre>
</desc>
</func>
<func>
<name name="split" arity="2"/>
<name name="split" arity="3"/>
<fsummary>Split a string into substrings.</fsummary>
<desc>
<p>
Splits <c><anno>String</anno></c> where <c><anno>SearchPattern</anno></c>
is encountered and return the remaining parts.
<c><anno>Where</anno></c>, default <c>leading</c>, indicates whether
the <c>leading</c>, the <c>trailing</c> or <c>all</c> encounters of
<c><anno>SearchPattern</anno></c> will split <c><anno>String</anno></c>.
</p>
<p><em>Example:</em></p>
<pre>
0> <input>string:split("ab..bc..cd", "..").</input>
["ab","bc..cd"]
1> <input>string:split(<<"ab..bc..cd">>, "..", trailing).</input>
[<<"ab..bc">>,<<"cd">>]
2> <input>string:split(<<"ab..bc....cd">>, "..", all).</input>
[<<"ab">>,<<"bc">>,<<>>,<<"cd">>]</pre>
</desc>
</func>
<func>
<name name="take" arity="2"/>
<name name="take" arity="3"/>
<name name="take" arity="4"/>
<fsummary>Take leading or trailing parts.</fsummary>
<desc>
<p>Takes characters from <c><anno>String</anno></c> as long as
the characters are members of set <c><anno>Characters</anno></c>
or the complement of set <c><anno>Characters</anno></c>.
<c><anno>Dir</anno></c>,
which can be <c>leading</c> or <c>trailing</c>, indicates from
which direction characters are to be taken.
</p>
<p><em>Example:</em></p>
<pre>
5> <input>string:take("abc0z123", lists:seq($a,$z)).</input>
{"abc","0z123"}
6> <input>string:take(<<"abc0z123">>, lists:seq($0,$9), true, leading).</input>
{<<"abc">>,<<"0z123">>}
7> <input>string:take("abc0z123", lists:seq($0,$9), false, trailing).</input>
{"abc0z","123"}
8> <input>string:take(<<"abc0z123">>, lists:seq($a,$z), true, trailing).</input>
{<<"abc0z">>,<<"123">>}</pre>
</desc>
</func>
<func>
<name name="titlecase" arity="1"/>
<fsummary>Convert a string to titlecase.</fsummary>
<desc>
<p>
Converts <c><anno>String</anno></c> to titlecase.
</p>
<p><em>Example:</em></p>
<pre>
1> <input>string:titlecase("ß is a SHARP s").</input>
"Ss is a SHARP s"</pre>
</desc>
</func>
<func>
<name name="to_float" arity="1"/>
<fsummary>Return a float whose text representation is the integers
(ASCII values) of a string.</fsummary>
<desc>
<p>Argument <c><anno>String</anno></c> is expected to start with a
valid text represented float (the digits are ASCII values).
Remaining characters in the string after the float are returned in
<c><anno>Rest</anno></c>.</p>
<p><em>Example:</em></p>
<pre>
> <input>{F1,Fs} = string:to_float("1.0-1.0e-1"),</input>
> <input>{F2,[]} = string:to_float(Fs),</input>
> <input>F1+F2.</input>
0.9
> <input>string:to_float("3/2=1.5").</input>
{error,no_float}
> <input>string:to_float("-1.5eX").</input>
{-1.5,"eX"}</pre>
</desc>
</func>
<func>
<name name="to_integer" arity="1"/>
<fsummary>Return an integer whose text representation is the integers
(ASCII values) of a string.</fsummary>
<desc>
<p>Argument <c><anno>String</anno></c> is expected to start with a
valid text represented integer (the digits are ASCII values).
Remaining characters in the string after the integer are returned in
<c><anno>Rest</anno></c>.</p>
<p><em>Example:</em></p>
<pre>
> <input>{I1,Is} = string:to_integer("33+22"),</input>
> <input>{I2,[]} = string:to_integer(Is),</input>
> <input>I1-I2.</input>
11
> <input>string:to_integer("0.5").</input>
{0,".5"}
> <input>string:to_integer("x=2").</input>
{error,no_integer}</pre>
</desc>
</func>
<func>
<name name="to_graphemes" arity="1"/>
<fsummary>Convert a string to a list of grapheme clusters.</fsummary>
<desc>
<p>
Converts <c><anno>String</anno></c> to a list of grapheme clusters.
</p>
<p><em>Example:</em></p>
<pre>
1> <input>string:to_graphemes("ß↑e̊").</input>
[223,8593,[101,778]]
2> <input>string:to_graphemes(<<"ß↑e̊"/utf8>>).</input>
[223,8593,[101,778]]</pre>
</desc>
</func>
<func>
<name name="trim" arity="1"/>
<name name="trim" arity="2"/>
<name name="trim" arity="3"/>
<fsummary>Trim leading or trailing, or both, characters.</fsummary>
<desc>
<p>
Returns a string, where leading or trailing, or both,
<c><anno>Characters</anno></c> have been removed.
<c><anno>Dir</anno></c> which can be <c>leading</c>, <c>trailing</c>,
or <c>both</c>, indicates from which direction characters
are to be removed.
</p>
<p> Default <c><anno>Characters</anno></c> is the set of
nonbreakable whitespace codepoints, defined as
Pattern_White_Space in
<url href="http://unicode.org/reports/tr31/">Unicode Standard Annex #31</url>.
<c>By default, <anno>Dir</anno></c> is <c>both</c>.
</p>
<p>
Notice that <c>[$\r,$\n]</c> is one grapheme cluster according
to the Unicode Standard.
</p>
<p><em>Example:</em></p>
<pre>
1> <input>string:trim("\t Hello \n").</input>
"Hello"
2> <input>string:trim(<<"\t Hello \n">>, leading).</input>
<<"Hello \n">>
3> <input>string:trim(<<".Hello.\n">>, trailing, "\n.").</input>
<<".Hello">></pre>
</desc>
</func>
<func>
<name name="uppercase" arity="1"/>
<fsummary>Convert a string to uppercase.</fsummary>
<desc>
<p>
Converts <c><anno>String</anno></c> to uppercase.
</p>
<p>See also <seealso marker="#titlecase/1"><c>titlecase/1</c></seealso>.</p>
<p><em>Example:</em></p>
<pre>
1> <input>string:uppercase("Michał").</input>
"MICHAŁ"</pre>
</desc>
</func>
</funcs>
<section>
<marker id="oldapi"/>
<title>Obsolete API functions</title>
<p>Here follows the function of the old API.
These functions only work on a list of Latin-1 characters.
</p>
<note><p>
The functions are kept for backward compatibility, but are
not recommended.
They will be deprecated in a future release.
</p>
<p>Any undocumented functions in <c>string</c> are not to be used.</p>
</note>
</section>
<funcs>
<func>
<name name="centre" arity="2"/>
<name name="centre" arity="3"/>
<fsummary>Center a string.</fsummary>
<desc>
<p>Returns a string, where <c><anno>String</anno></c> is centered in the
string and surrounded by blanks or <c><anno>Character</anno></c>.
The resulting string has length <c><anno>Number</anno></c>.</p>
<p>This function is <seealso marker="#oldapi">obsolete</seealso>.
Use
<seealso marker="#pad/3"><c>pad/3</c></seealso>.
</p>
</desc>
</func>
<func>
<name name="chars" arity="2"/>
<name name="chars" arity="3"/>
<fsummary>Return a string consisting of numbers of characters.</fsummary>
<desc>
<p>Returns a string consisting of <c><anno>Number</anno></c> characters
<c><anno>Character</anno></c>. Optionally, the string can end with
string <c><anno>Tail</anno></c>.</p>
<p>This function is <seealso marker="#oldapi">obsolete</seealso>.
Use
<seealso marker="lists#duplicate/2"><c>lists:duplicate/2</c></seealso>.</p>
</desc>
</func>
<func>
<name name="chr" arity="2"/>
<fsummary>Return the index of the first occurrence of
a character in a string.</fsummary>
<desc>
<p>Returns the index of the first occurrence of
<c><anno>Character</anno></c> in <c><anno>String</anno></c>. Returns
<c>0</c> if <c><anno>Character</anno></c> does not occur.</p>
<p>This function is <seealso marker="#oldapi">obsolete</seealso>.
Use
<seealso marker="#find/2"><c>find/2</c></seealso>.</p>
</desc>
</func>
<func>
<name name="concat" arity="2"/>
<fsummary>Concatenate two strings.</fsummary>
<desc>
<p>Concatenates <c><anno>String1</anno></c> and
<c><anno>String2</anno></c> to form a new string
<c><anno>String3</anno></c>, which is returned.</p>
<p>
This function is <seealso marker="#oldapi">obsolete</seealso>.
Use <c>[<anno>String1</anno>, <anno>String2</anno>]</c> as
<c>Data</c> argument, and call
<seealso marker="unicode#characters_to_list/2">
<c>unicode:characters_to_list/2</c></seealso> or
<seealso marker="unicode#characters_to_binary/2">
<c>unicode:characters_to_binary/2</c></seealso>
to flatten the output.
</p>
</desc>
</func>
<func>
<name name="copies" arity="2"/>
<fsummary>Copy a string.</fsummary>
<desc>
<p>Returns a string containing <c><anno>String</anno></c> repeated
<c><anno>Number</anno></c> times.</p>
<p>This function is <seealso marker="#oldapi">obsolete</seealso>.
Use
<seealso marker="lists#duplicate/2"><c>lists:duplicate/2</c></seealso>.</p>
</desc>
</func>
<func>
<name name="cspan" arity="2"/>
<fsummary>Span characters at start of a string.</fsummary>
<desc>
<p>Returns the length of the maximum initial segment of
<c><anno>String</anno></c>, which consists entirely of characters
not from <c><anno>Chars</anno></c>.</p>
<p>This function is <seealso marker="#oldapi">obsolete</seealso>.
Use
<seealso marker="#take/3"><c>take/3</c></seealso>.</p>
<p><em>Example:</em></p>
<code type="none">
> string:cspan("\t abcdef", " \t").
0</code>
</desc>
</func>
<func>
<name name="join" arity="2"/>
<fsummary>Join a list of strings with separator.</fsummary>
<desc>
<p>Returns a string with the elements of <c><anno>StringList</anno></c>
separated by the string in <c><anno>Separator</anno></c>.</p>
<p>This function is <seealso marker="#oldapi">obsolete</seealso>.
Use
<seealso marker="lists#join/2"><c>lists:join/2</c></seealso>.</p>
<p><em>Example:</em></p>
<code type="none">
> join(["one", "two", "three"], ", ").
"one, two, three"</code>
</desc>
</func>
<func>
<name name="left" arity="2"/>
<name name="left" arity="3"/>
<fsummary>Adjust left end of a string.</fsummary>
<desc>
<p>Returns <c><anno>String</anno></c> with the length adjusted in
accordance with <c><anno>Number</anno></c>. The left margin is
fixed. If <c>length(<anno>String</anno>)</c> <
<c><anno>Number</anno></c>, then <c><anno>String</anno></c> is padded
with blanks or <c><anno>Character</anno></c>s.</p>
<p>This function is <seealso marker="#oldapi">obsolete</seealso>.
Use
<seealso marker="#pad/2"><c>pad/2</c></seealso> or
<seealso marker="#pad/3"><c>pad/3</c></seealso>.</p>
<p><em>Example:</em></p>
<code type="none">
> string:left("Hello",10,$.).
"Hello....."</code>
</desc>
</func>
<func>
<name name="len" arity="1"/>
<fsummary>Return the length of a string.</fsummary>
<desc>
<p>Returns the number of characters in <c><anno>String</anno></c>.</p>
<p>This function is <seealso marker="#oldapi">obsolete</seealso>.
Use
<seealso marker="#length/1"><c>length/1</c></seealso>.</p>
</desc>
</func>
<func>
<name name="rchr" arity="2"/>
<fsummary>Return the index of the last occurrence of
a character in a string.</fsummary>
<desc>
<p>Returns the index of the last occurrence of
<c><anno>Character</anno></c> in <c><anno>String</anno></c>. Returns
<c>0</c> if <c><anno>Character</anno></c> does not occur.</p>
<p>This function is <seealso marker="#oldapi">obsolete</seealso>.
Use
<seealso marker="#find/3"><c>find/3</c></seealso>.</p>
</desc>
</func>
<func>
<name name="right" arity="2"/>
<name name="right" arity="3"/>
<fsummary>Adjust right end of a string.</fsummary>
<desc>
<p>Returns <c><anno>String</anno></c> with the length adjusted in
accordance with <c><anno>Number</anno></c>. The right margin is
fixed. If the length of <c>(<anno>String</anno>)</c> <
<c><anno>Number</anno></c>, then <c><anno>String</anno></c> is padded
with blanks or <c><anno>Character</anno></c>s.</p>
<p>This function is <seealso marker="#oldapi">obsolete</seealso>.
Use
<seealso marker="#pad/3"><c>pad/3</c></seealso>.</p>
<p><em>Example:</em></p>
<code type="none">
> string:right("Hello", 10, $.).
".....Hello"</code>
</desc>
</func>
<func>
<name name="rstr" arity="2"/>
<fsummary>Find the index of a substring.</fsummary>
<desc>
<p>Returns the position where the last occurrence of
<c><anno>SubString</anno></c> begins in <c><anno>String</anno></c>.
Returns <c>0</c> if <c><anno>SubString</anno></c>
does not exist in <c><anno>String</anno></c>.</p>
<p>This function is <seealso marker="#oldapi">obsolete</seealso>.
Use
<seealso marker="#find/3"><c>find/3</c></seealso>.</p>
<p><em>Example:</em></p>
<code type="none">
> string:rstr(" Hello Hello World World ", "Hello World").
8</code>
</desc>
</func>
<func>
<name name="span" arity="2"/>
<fsummary>Span characters at start of a string.</fsummary>
<desc>
<p>Returns the length of the maximum initial segment of
<c><anno>String</anno></c>, which consists entirely of characters
from <c><anno>Chars</anno></c>.</p>
<p>This function is <seealso marker="#oldapi">obsolete</seealso>.
Use
<seealso marker="#take/2"><c>take/2</c></seealso>.</p>
<p><em>Example:</em></p>
<code type="none">
> string:span("\t abcdef", " \t").
5</code>
</desc>
</func>
<func>
<name name="str" arity="2"/>
<fsummary>Find the index of a substring.</fsummary>
<desc>
<p>Returns the position where the first occurrence of
<c><anno>SubString</anno></c> begins in <c><anno>String</anno></c>.
Returns <c>0</c> if <c><anno>SubString</anno></c>
does not exist in <c><anno>String</anno></c>.</p>
<p>This function is <seealso marker="#oldapi">obsolete</seealso>.
Use
<seealso marker="#find/2"><c>find/2</c></seealso>.</p>
<p><em>Example:</em></p>
<code type="none">
> string:str(" Hello Hello World World ", "Hello World").
8</code>
</desc>
</func>
<func>
<name name="strip" arity="1"/>
<name name="strip" arity="2"/>
<name name="strip" arity="3"/>
<fsummary>Strip leading or trailing characters.</fsummary>
<desc>
<p>Returns a string, where leading or trailing, or both, blanks or a
number of <c><anno>Character</anno></c> have been removed.
<c><anno>Direction</anno></c>, which can be <c>left</c>, <c>right</c>,
or <c>both</c>, indicates from which direction blanks are to be
removed. <c>strip/1</c> is equivalent to
<c>strip(String, both)</c>.</p>
<p>This function is <seealso marker="#oldapi">obsolete</seealso>.
Use
<seealso marker="#trim/3"><c>trim/3</c></seealso>.</p>
<p><em>Example:</em></p>
<code type="none">
> string:strip("...Hello.....", both, $.).
"Hello"</code>
</desc>
</func>
<func>
<name name="sub_string" arity="2"/>
<name name="sub_string" arity="3"/>
<fsummary>Extract a substring.</fsummary>
<desc>
<p>Returns a substring of <c><anno>String</anno></c>, starting at
position <c><anno>Start</anno></c> to the end of the string, or to
and including position <c><anno>Stop</anno></c>.</p>
<p>This function is <seealso marker="#oldapi">obsolete</seealso>.
Use
<seealso marker="#slice/3"><c>slice/3</c></seealso>.</p>
<p><em>Example:</em></p>
<code type="none">
sub_string("Hello World", 4, 8).
"lo Wo"</code>
</desc>
</func>
<func>
<name name="substr" arity="2"/>
<name name="substr" arity="3"/>
<fsummary>Return a substring of a string.</fsummary>
<desc>
<p>Returns a substring of <c><anno>String</anno></c>, starting at
position <c><anno>Start</anno></c>, and ending at the end of the
string or at length <c><anno>Length</anno></c>.</p>
<p>This function is <seealso marker="#oldapi">obsolete</seealso>.
Use
<seealso marker="#slice/3"><c>slice/3</c></seealso>.</p>
<p><em>Example:</em></p>
<code type="none">
> substr("Hello World", 4, 5).
"lo Wo"</code>
</desc>
</func>
<func>
<name name="sub_word" arity="2"/>
<name name="sub_word" arity="3"/>
<fsummary>Extract subword.</fsummary>
<desc>
<p>Returns the word in position <c><anno>Number</anno></c> of
<c><anno>String</anno></c>. Words are separated by blanks or
<c><anno>Character</anno></c>s.</p>
<p>This function is <seealso marker="#oldapi">obsolete</seealso>.
Use
<seealso marker="#nth_lexeme/3"><c>nth_lexeme/3</c></seealso>.</p>
<p><em>Example:</em></p>
<code type="none">
> string:sub_word(" Hello old boy !",3,$o).
"ld b"</code>
</desc>
</func>
<func>
<name name="to_lower" arity="1" clause_i="1"/>
<name name="to_lower" arity="1" clause_i="2"/>
<name name="to_upper" arity="1" clause_i="1"/>
<name name="to_upper" arity="1" clause_i="2"/>
<fsummary>Convert case of string (ISO/IEC 8859-1).</fsummary>
<type variable="String" name_i="1"/>
<type variable="Result" name_i="1"/>
<type variable="Char"/>
<type variable="CharResult"/>
<desc>
<p>The specified string or character is case-converted. Notice that
the supported character set is ISO/IEC 8859-1 (also called Latin 1);
all values outside this set are unchanged</p>
<p>This function is <seealso marker="#oldapi">obsolete</seealso> use
<seealso marker="#lowercase/1"><c>lowercase/1</c></seealso>,
<seealso marker="#uppercase/1"><c>uppercase/1</c></seealso>,
<seealso marker="#titlecase/1"><c>titlecase/1</c></seealso> or
<seealso marker="#casefold/1"><c>casefold/1</c></seealso>.</p>
</desc>
</func>
<func>
<name name="tokens" arity="2"/>
<fsummary>Split string into tokens.</fsummary>
<desc>
<p>Returns a list of tokens in <c><anno>String</anno></c>, separated
by the characters in <c><anno>SeparatorList</anno></c>.</p>
<p><em>Example:</em></p>
<code type="none">
> tokens("abc defxxghix jkl", "x ").
["abc", "def", "ghi", "jkl"]</code>
<p>Notice that, as shown in this example, two or more
adjacent separator characters in <c><anno>String</anno></c>
are treated as one. That is, there are no empty
strings in the resulting list of tokens.</p>
<p>This function is <seealso marker="#oldapi">obsolete</seealso>.
Use
<seealso marker="#lexemes/2"><c>lexemes/2</c></seealso>.</p>
</desc>
</func>
<func>
<name name="words" arity="1"/>
<name name="words" arity="2"/>
<fsummary>Count blank separated words.</fsummary>
<desc>
<p>Returns the number of words in <c><anno>String</anno></c>, separated
by blanks or <c><anno>Character</anno></c>.</p>
<p>This function is <seealso marker="#oldapi">obsolete</seealso>.
Use
<seealso marker="#lexemes/2"><c>lexemes/2</c></seealso>.</p>
<p><em>Example:</em></p>
<code type="none">
> words(" Hello old boy!", $o).
4</code>
</desc>
</func>
</funcs>
<section>
<title>Notes</title>
<p>Some of the general string functions can seem to overlap each
other. The reason is that this string package is the
combination of two earlier packages and all functions of
both packages have been retained.</p>
</section>
</erlref>
|