String
type (joe
)
A String
is just a Java String
, as accessed from a Joe script.
- string.charAt(index) → String
- string.contains(target) → Boolean
- string.endsWith(suffix) → Boolean
- string.equalsIgnoreCase(other) → Boolean
- string.indent(n) → String
- string.indexOf(target, [beginIndex], [endIndex]) → Number
- string.isBlank() → Boolean
- string.isEmpty() → Boolean
- string.lastIndexOf(target, [fromIndex]) → Number
- string.length() → Double
- string.lines() → List
- string.matches(pattern) → Boolean
- string.repeat(count) → String
- string.replace(target, replacement) → String
- string.replaceAll(regex, replacement) → String
- string.replaceFirst(regex, replacement) → String
- string.split(delimiter) → List
- string.splitWithDelimiters(delimiter) → List
- string.startsWith(prefix) → Boolean
- string.strip() → String
- string.stripIndent() → String
- string.stripLeading() → String
- string.stripTrailing() → String
- string.substring(beginIndex, [endIndex]) → String
- string.toLowerCase() → String
- string.toString() → String
- string.toUpperCase() → String
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:
Conversion | Input Type | Description |
---|---|---|
b , B | Any | true if truthy, false if falsey |
h , H | Any | The value's hash code as a hex string. |
s , S | Any | The value, stringified. |
d | Number | Converted to integer and formatted. |
x , X | Number | Converted to integer, formatted as hex |
e , E | Number | Decimal, scientific notation |
f | Number | Decimal |
g , G | Number | Decimal or scientific notation, as appropriate. |
% | n/a | A literal percent sign |
n | n/a | The 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
, andX
conversions.
Flags
The supported flags are as follows, and apply only to the specified argument type.
Flag | Input Type | Description |
---|---|---|
- | Any | Result is left-justified. |
+ | Number | Result always includes a sign |
space | Number | Leading space for positive values |
0 | Number | Padded with leading zeros |
, | Number | Use local-specific grouping separators |
( | Number | Enclose 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.