std/string¶
module string for String
The functions of a String, in the prelude. A receiver module: each
function takes self, so s.trim(), string.trim(s) and s | trim are
one declaration. A String is a sequence of Unicode scalar values held
as UTF-8, whatever encoding its text was read from. Positions and size
count code points.
str— The textselfprints as, for a value of any type:str(some(2))is"2".fmt— The templateselfwith each{}replaced by the text of the next ofargs, in order:fmt("{} of {}", 1, some(2))is"1 of 2".length— How many charactersselfhas, counting code points:"héllo" | lengthis5.size— How many charactersselfhas, counting code points; the same aslength:"héllo" | sizeis5.upper—selfin upper case:"Straße" | upperis"STRASSE".lower—selfin lower case:"ÀB" | loweris"àb".trim—selfwithout leading and trailing whitespace:" a b " | trimis"a b".trim_start—selfwithout leading whitespace:" a " | trim_startis"a ".trim_end—selfwithout trailing whitespace:" a " | trim_endis" a".capitalize—selfwith its first character in upper case:"élan" | capitalizeis"Élan".split— The pieces ofselfbetween occurrences ofseparator, in order:"a,b,,c" | split(",")is["a", "b", "", "c"].join— The texts ofselfin order, withseparatorbetween each two:["a", "b"].join(", ")is"a, b".replace_all—selfwith every occurrence offromreplaced byto:"aaa" | replace_all("a", "b")is"bbb".replace_first—selfwith the first occurrence offromreplaced byto:"aaa" | replace_first("a", "b")is"baa".starts_with— Whetherselfbegins withpart:"hello" | starts_with("he")istrue.ends_with— Whetherselfends withpart:"hello" | ends_with("lo")istrue.contains— Whetherpartoccurs inself:"hello" | contains("ell")istrue.index_of— The position of the first occurrence ofpartinself, counting characters from 0, ornone:"héllo" | index_of("l")is2.last_index_of— The position of the last occurrence ofpartinself, counting characters from 0, ornone:"héllo" | last_index_of("l")is3.occurrences— How many non-overlapping timespartoccurs inself; 0 for an emptypart:"aaaa" | occurrences("aa")is2.strip_prefix—selfwithoutpartat its start, ornonewhen it does not begin with it:"x.hql" | strip_prefix("x.")is"hql".strip_suffix—selfwithoutpartat its end, ornonewhen it does not end with it:"x.hql" | strip_suffix(".hql")is"x".trim_prefix—selfwithoutpartat its start, orselfas it is when it does not begin with it:"x" | trim_prefix("y")is"x".trim_suffix—selfwithoutpartat its end, orselfas it is when it does not end with it:"x.hql" | trim_suffix(".hql")is"x".equals_ignore_case— Whetherselfandotherare equal in lower case:"ABC" | equals_ignore_case("abc")istrue.chars— Each character ofselfas its own text, in order:"héy" | charsis["h", "é", "y"].lines— The lines ofself, without their line breaks; a final line break opens no empty line:string.lines("a\nb\n")is["a", "b"].words— The whitespace-separated words ofself:" a b\tc " | wordsis["a", "b", "c"].reverse— The characters ofselfin the opposite order:"héllo" | reverseis"olléh".repeat—selfwrittentimestimes in a row;""fortimesbelow 1:"ab" | repeat(3)is"ababab".pad_left—selfpreceded byfill, repeated, until it iswidthcharacters long; a longerselfas it is:"7" | pad_left(3, "0")is"007".pad_right—selffollowed byfill, repeated, until it iswidthcharacters long; a longerselfas it is:"7" | pad_right(3, "0")is"700".center—selfbetweenfills until it iswidthcharacters long; an odd fill character goes after it:"ab" | center(5, "*")is"*ab**".truncate— At most the firstwidthcharacters ofself:"hello" | truncate(3)is"hel".slice— The characters ofselffromstartup to, not including,end, counting from 0;s[start:end].char_at— The character ofselfatposition, counting from 0, ornoneoutside the text:"hello" | char_at(1)is"e".to_int— The integerselfwrites, surrounding whitespace aside, ornonewhen it writes none:"-42" | to_intis-42, and"1.5" | to_intisnone.to_float— The numberselfwrites, as aFloat, ornonewhen it writes none:"1e3" | to_floatis1000.0.to_decimal— The exact decimal numberselfwrites, at its written scale, ornonewhen it writes none:"2.50" | to_decimalis2.50.to_bool— The truth valueselfwrites astrueorfalse, ornonefor any other text:"True" | to_boolisnone.is_empty— Whetherselfhas no characters:"" | is_emptyistrue.is_blank— Whetherselfhas no characters other than whitespace:" " | is_blankistrue.ord— The Unicode code point of the one-character textself, ornonefor any other text:"a" | ordis97.chr— The one-character text of the Unicode code pointself, ornonefor anIntthat is none:97 | chris"a".bytes— The UTF-8 encoding ofself, oneIntper byte:"é" | bytesis[195, 169].