SubString

SubString( value, start, [ length ] )
SubString( value, start, [ length ] )
SubString( value, start, [ length ] )
SubString( value, start, [ length ] )
substring( value, start, [ length ] )
SubString( value, start, [ length ] )

Description

The SubString function extracts a substring out of the value string, beginning at the character start position, and including length characters (or all remaining characters if length is not specified).

For the set of letters in the alphabet, setting start to 3 and length to 3 results in the substring DEF:

The substring of 'DEF' in the set of alphabet characters

Although we mention characters, technically, we are talking about code points. In Unicode, a code point is usually, but not always, a character. Code points can sometimes represent single characters, but can also represent formatting and other non-alphanumeric characters.

For example, the character é can be the single character "e" that includes an acute accent (U+00E9), or it could be displayed as a single character but be composed of an "e" (U+0065) and a "combining" acute accent (U+0301). Visually, you cannot distinguish between the two.

Parameters

Argument Type Definition and Requirements

value

The String to extract the substring from.

start

The index of the first character in value to return. The first character in value is at index 0.

When start is 0 or positive, the index is measured from the left side of the value string. If start is negative, the index is measured from the right side of the string. For example, when start is -1, the index is positioned at the last character in the value string.

When start is equal to the string length, the substring is an empty string. When start is greater than the string length, an error is returned. This behavior is identical to the Java String library’s substring function.

length

Optional - The number of characters to extract. When length is not specified, the substring returned includes all of the characters in value from the start position to the end.

Since Go does not have function overloading or optional arguments, you must use the function f.StrLength() instead of a plain integer.

Returns

A string which is a substring of value, according to the start and length values.

Examples

The following query executes an array of independent SubString operations and returns the results in an array. The result array position matches the execution array position. The first operation takes a source string containing "ABCDEFGHIJK" and extracts a string starting at the 2 position for 3 characters. The resultant string "CDE" is placed in the top position of the result array. The second operation uses the same input string, but starts at the 1 position ("B") and extracts 6 characters("BCDEFG"). The third operations extracts the last 4 characters from the end of the source string.

try
{
    Value result = await client.Query(
        Arr(
            SubString("ABCDEFGHIJK", 2, 3),
            SubString("ABCDEFGHIJK", 1, 6),
            SubString("ABCDEFGHIJK", -4),
            SubString("ABCDEFGHIJK", -3, 2)
        )
    );

    Console.WriteLine(result);
}
catch (Exception e)
{
    Console.WriteLine($"ERROR: {e.Message}");
}
Arr(StringV(CDE), StringV(BCDEFG), StringV(HIJK), StringV(IJ))
result, err := client.Query(
	f.Arr{
		f.SubString("ABCDEFGHIJK", 2, f.StrLength(3)),
		f.SubString("ABCDEFGHIJK", 1, f.StrLength(6)),
		f.SubString("ABCDEFGHIJK", -4),
		f.SubString("ABCDEFGHIJK", -3, f.StrLength(2)),
	})

if err != nil {
	fmt.Fprintln(os.Stderr, err)
} else {
	fmt.Println(result)
}
[CDE BCDEFG HIJK IJ]
System.out.println(
    client.query(
        Arr(
            SubString("ABCDEFGHIJK", 2, 3),
            SubString("ABCDEFGHIJK", 1, 6),
            SubString("ABCDEFGHIJK", -4),
            SubString("ABCDEFGHIJK", -3, 2)
        )
    ).get());
["CDE", "BCDEFG", "HIJK", "IJ"]
client.query(
  [
    q.SubString('ABCDEFGHIJK', 2, 3),
    q.SubString('ABCDEFGHIJK', 1, 6),
    q.SubString('ABCDEFGHIJK', -4),
    q.SubString('ABCDEFGHIJK', -3, 2),
  ]
)
.then((ret) => console.log(ret))
.catch((err) => console.error('Error: %s', err))
[ 'CDE', 'BCDEFG', 'HIJK', 'IJ' ]
result = client.query(
  [
    q.substring("ABCDEFGHIJK", 2, 3),
    q.substring("ABCDEFGHIJK", 1, 6),
    q.substring("ABCDEFGHIJK", -4),
    q.substring("ABCDEFGHIJK", -3, 2),
  ]
)
print(result)
['CDE', 'BCDEFG', 'HIJK', 'IJ']
try {
  println(Await.result(
    client.query(
        Arr(
            SubString("ABCDEFGHIJK", 2, 3),
            SubString("ABCDEFGHIJK", 1, 6),
            SubString("ABCDEFGHIJK", -4),
            SubString("ABCDEFGHIJK", -3, 2)
        )
    ),
    5.seconds
))} catch {
  case unknown: Throwable => println("Error: " + unknown.getMessage())
}
["CDE", "BCDEFG", "HIJK", "IJ"]

Was this article helpful?

We're sorry to hear that.
Tell us how we can improve!
Visit Fauna's Discourse forums or email docs@fauna.com

Thank you for your feedback!