Text
Concatenation needs Text on both sides
+ joins two Texts. "Count: " + 5 is not "Count: 5". It's the mixed-type fallback,
Integer 0. Turn numbers, Bools and handles into Text with StringFormat first.
StringFormat(format, value0, value1)
- Replaces every
{0}withvalue0and every{1}withvalue1. Either value can be any kind. There's no{2}and no format specifiers such as{0:N2}. - It always takes three arguments. Pass
""for a value you don't use:StringFormat("{0} items", n, ""). - For more than two values, nest calls or concatenate the results (see the example below).
- A Real always shows six decimals (
0.500000). Round it to an Integer first for a whole number, or scale it (MathRound(r * 100)) for fixed-point output. {0}is replaced before{1}, so ifvalue0's text itself contains{1}, that gets replaced too.
w = WindowGetActive();
s = StringFormat(StringFormat("{0} x {1} at ", WindowGetWidth(w), WindowGetHeight(w)) + "({0}, {1})",
WindowGetX(w), WindowGetY(w));
UtilityPrint(s); // e.g. 1280 x 720 at (100, 80)
Measuring, searching, slicing
StringGetLengthcounts UTF-16 code units, not characters. An emoji outside the Basic Multilingual Plane counts as 2.StringGetIndexOf(text, search)gives a 0-based index, or-1whensearchisn't found;StringGetLastIndexOfgives the last one (an example). Both are case-sensitive.StringCount(text, search)counts the matches without overlapping:"aa"occurs twice in"aaaa".StringGetSubstring(text, start, length):startis 0-based. Alengthrunning past the end is cut short, and astartat or past the end gives"". A negativestartorlengthis an error and stops the action.- There's no indexing syntax (
s[0]). UseStringGetSubstring(s, i, 1)for one character. StringContains,StringStartsWith,StringEndsWithandStringReplaceare exact and case-sensitive. For a case-insensitive test, lower-case both sides first.StringToUpper/StringToLowerfollow the user's locale casing rules, so the Turkish dotted/dotless i is handled correctly.StringTrimstrips whitespace from both ends, including the\rleft over from Windows\r\nline endings.StringReplace,StringCountandStringSplitstop the action if the search text or delimiter is empty.StringPadLeft(text, totalLength, pad)andStringPadRightaddpaduntil the Text istotalLengthlong (zero-pad a number); Text already that long comes back unchanged.StringRepeat(text, times)writestextseveral times in a row. Each stops the action rather than make Text longer than 16 million characters.
Splitting is a cache you read by index
A Value can't hold a list, so StringSplit(text, delimiter) stores the parts in the running
script's context and returns how many there are. Read each part with StringGetSplitPartAt(i); an
out-of-range index gives "". The next StringSplit in the same script replaces that cache,
so copy a part into a variable before you split something else. Every …EnumerateAll,
…FindAll… or …GetAll… builtin, paired with its …EnumeratedAt reader, works the same way.
Comparing text
== is exact and case-sensitive. </> compare ordinally by UTF-16 code unit, which isn't
dictionary order: every uppercase ASCII letter sorts before every lowercase one, and accented
letters sort after z. Don't present that ordering to a user as alphabetical.
For alphabetical order, StringCompare(a, b, ignoreCase) returns -1, 0 or 1 in the order the
user's language sorts words, from their Windows region settings: apple before Banana, and in
Swedish ö after z.
Lines
StringSortLines(text, descending, ignoreCase) sorts the lines of a Text in that same order, and
StringRemoveDuplicateLines(text, ignoreCase) drops every line that repeats an earlier one, keeping
the first. A line ends at \r\n, \n or a lone \r; both keep the Text's kind of line break and
whether it ended with one. Numbers in lines sort as text (10 before 9).
FileReadLineAt(path, index) and FileGetLineCount(path) read a file the same way, one line at a
time, without loading the whole file.
Encoding for the web
StringUrlEncode/StringUrlDecode convert to and from URL encoding (%20 for a space; a +
stays a +). StringHtmlEncode/StringHtmlDecode convert &, <, > and quotes to and from
&-style references. StringBase64Encode/StringBase64Decode convert to and from Base64, as
UTF-8. Text that doesn't decode stays as written, and Text that isn't Base64 decodes to "".