Skip to main content

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} with value0 and every {1} with value1. 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 if value0'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​

  • StringGetLength counts 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 -1 when search isn't found; StringGetLastIndexOf gives 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): start is 0-based. A length running past the end is cut short, and a start at or past the end gives "". A negative start or length is an error and stops the action.
  • There's no indexing syntax (s[0]). Use StringGetSubstring(s, i, 1) for one character.
  • StringContains, StringStartsWith, StringEndsWith and StringReplace are exact and case-sensitive. For a case-insensitive test, lower-case both sides first.
  • StringToUpper/StringToLower follow the user's locale casing rules, so the Turkish dotted/dotless i is handled correctly.
  • StringTrim strips whitespace from both ends, including the \r left over from Windows \r\n line endings.
  • StringReplace, StringCount and StringSplit stop the action if the search text or delimiter is empty.
  • StringPadLeft(text, totalLength, pad) and StringPadRight add pad until the Text is totalLength long (zero-pad a number); Text already that long comes back unchanged. StringRepeat(text, times) writes text several 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 &amp;-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 "".