String type (joe)


A String is just a Java String, as accessed from a Joe script.

Topics

Static Methods

Initializer

Methods

Note: Joe provides a number of methods that Java does not.

Static Methods


String.format()


String.format(fmt, [values...]) → String

Formats the values into a string given the format string. See String Formatting, below, for the format string syntax.

String.join()


String.join(delimiter, list) → String

Given a delimiter and a list, joins the string representations of the list items into a single string, delimited by the given delimiter.

String Initializer


String(value) → String

Converts the value into its string representation.

Methods


string.charAt()


string.charAt(index) → String

Returns the character at the index as a string.

string.contains()


string.contains(target) → Boolean

Returns true if this contains the target, and false otherwise.

string.endsWith()


string.endsWith(suffix) → Boolean

Returns true if this string ends with the suffix, and false otherwise.

string.equalsIgnoreCase()


string.equalsIgnoreCase(other) → Boolean

Returns true if this string is equal to the string representation of the other value, ignoring case, and false otherwise.

string.indent()


string.indent(n) → String

Indents or outdents the string by n characters.

Note: Java's String::indent returns the result with a trailing newline; this is easier to add than to remove, and is often unwanted, so Joe trims it.

string.indexOf()


string.indexOf(target, [beginIndex], [endIndex]) → Number

Returns the index of the first occurrence of the target string in this string, or -1 if the target is not found. The search starts at beginIndex, which defaults to 0, and ends at endIndex, which defaults to the end of the string.

string.isBlank()


string.isBlank() → Boolean

Returns true if this string is empty or contains only whitespace, and false otherwise.

string.isEmpty()


string.isEmpty() → Boolean

Returns true if this string is the empty string, and false otherwise.

string.lastIndexOf()


string.lastIndexOf(target, [fromIndex]) → Number

Returns the index of the last occurrence of the target string in this string, or -1 if the target is not found. The search starts at fromIndex, which defaults to 0, and proceeds towards the start of the string.

string.length()


string.length() → Double

Gets the string's length.

string.lines()


string.lines() → List

Returns a list consisting of the lines of text in the string.

string.matches()


string.matches(pattern) → Boolean

Returns true if this string matches the regex pattern, and false otherwise.

string.repeat()


string.repeat(count) → String

Creates a string containing this string repeated count times.

string.replace()


string.replace(target, replacement) → String

Returns the string created by replacing every occurrence of the target string in this string with the replacement string.

string.replaceAll()


string.replaceAll(regex, replacement) → String

Returns the string created by replacing each substring of this string that matches the regex with the replacement string.

string.replaceFirst()


string.replaceFirst(regex, replacement) → String

Returns the string created by replacing the first substring of this string that matches the regex with the replacement string.

string.split()


string.split(delimiter) → List

Returns a list of the tokens formed by splitting the string on each of the substrings that match the delimiter regular expression pattern. The delimiter substrings are not included in the list.

string.splitWithDelimiters()


string.splitWithDelimiters(delimiter) → List

Returns a list of the tokens formed by splitting the string on each of the substrings that match the delimiter regular expression pattern. The delimiter substrings are included as tokens in the list.

string.startsWith()


string.startsWith(prefix) → Boolean

Returns true if this string starts with the prefix, and false otherwise.

string.strip()


string.strip() → String

Returns this string, stripped of all leading and trailing whitespace.

string.stripIndent()


string.stripIndent() → String

Strips the indent from each line of the string, preserving relative indentation.

string.stripLeading()


string.stripLeading() → String

Returns this string, stripped of all leading whitespace.

string.stripTrailing()


string.stripTrailing() → String

Returns this string, stripped of all trailing whitespace.

string.substring()


string.substring(beginIndex, [endIndex]) → String

Returns the substring of this string that starts at the beginIndex and ends at the endIndex; endIndex defaults to the end of the string.

string.toLowerCase()


string.toLowerCase() → String

Returns this string converted to lowercase.

string.toString()


string.toString() → String

Returns this string.

string.toUpperCase()


string.toUpperCase() → String

Returns this string converted to uppercase.

String Formatting


The Joe String.format() method formats strings similarly to the Java method of the same name, supporting a subset of Java's format string syntax.

This section describes the supported subset.

As with Java, the basic syntax is:

%[flags][width][.precision][conversion]

Conversions

The conversion codes are as follows:

ConversionInput TypeDescription
b, BAnytrue if truthy, false if falsey
h, HAnyThe value's hash code as a hex string.
s, SAnyThe value, stringified.
dNumberConverted to integer and formatted.
x, XNumberConverted to integer, formatted as hex
e, ENumberDecimal, scientific notation
fNumberDecimal
g, GNumberDecimal or scientific notation, as appropriate.
%n/aA literal percent sign
nn/aThe platform-specific line separator
  • Note: the uppercase variant of the conversion is the same as the lowercase variant, but converts the output to uppercase.

Precision

  • For decimal conversions, the precision is the number of digits after the decimal place.

  • For the conversions with input type "Any" in the above table, the natural length of the output might be longer than the desired width. In this case, the precision field specifies the maximum width of the output.

  • The precision field cannot be used with the d, x, and X conversions.

Flags

The supported flags are as follows, and apply only to the specified argument type.

FlagInput TypeDescription
-AnyResult is left-justified.
+NumberResult always includes a sign
spaceNumberLeading space for positive values
0NumberPadded with leading zeros
,NumberUse local-specific grouping separators
(NumberEnclose negative numbers in parentheses

What's Not Supported

Joe's String.format() method does not support the following:

  • Argument indices, using $ syntax.
  • Date/time formatting.

I don't use argument indices with Java's String.format(), as I find them to be fragile; and I've always found it weird to combine simple type formatting and complex date/time formatting in what's meant to be a type-safe clone of Java's printf() syntax. I'd much prefer to provide a distinct API for using Java's java.time package.