Skip to content

String operation examples

For the complete documentation index see: llms.txt

All documentation pages available in markdown.

Aerospike string operations run on the server, so text is normalized, searched, and reshaped without a fetch-modify-write round trip. Each operation has a detailed description with its own code examples in the String operations reference.

The patterns below combine several of them. To filter or project string values inside a query rather than on one record, see String expressions.

Examples

Normalizing an email address

Starting from an email bin holding a value with stray whitespace and mixed case:

" Ana@Corp.IO "

Trim and lowercase it in one atomic operate call, replacing a read-modify-write loop in the application. Modify operations return no value, so the same call carries a read of the bin to get the normalized string back.

try (RecordStream rs = session.upsert(key)
.bin("email").trim()
.bin("email").lower()
.bin("email").get()
.execute()) {
Record rec = rs.next().recordOrThrow();
String normalized = rec.operationResult(2).getString();
// normalized: "ana@corp.io"
}

Extracting the domain from an email address

Starting from an email bin holding:

"ana@corp.io"

find returns the codepoint index of the first @, or -1 when the needle is absent.

try (RecordStream rs = session.query(key)
.bin("email").find("@")
.execute()) {
int at = (int) rs.next().recordOrThrow().operationResult(0).getLong();
// at: 3
}

Then take the rest of the string from the codepoint after it. The single-argument form of substr runs to the end of the value, so no length is needed.

try (RecordStream rs = session.query(key)
.bin("email").substr(at + 1)
.execute()) {
String domain = rs.next().recordOrThrow().operationResult(0).getString();
// domain: "corp.io"
}

Both round trips collapse into one if the result feeds an expression instead: compose find and substr in an expression_read projection. See String expressions.

Classifying a value with a regular expression

Starting from a sku bin holding:

"A-1024"

regex_compare returns a boolean rather than the match, so it classifies a record without moving the value to the client.

try (RecordStream rs = session.query(key)
.bin("sku").regexCompare("^A-[0-9]+$")
.execute()) {
boolean matched = rs.next().recordOrThrow().operationResult(0).getBoolean();
// matched: true
}

Prefer regex_compare over the legacy cmp_regex comparison, which uses POSIX regex and is not Unicode-aware. See regex_compare.

Modifying a bin whose type is uncertain

A string modify against a non-String bin fails with AS_ERR_INCOMPATIBLE_TYPE (12), and NO_FAIL does not suppress it. Starting from a maybe_string bin that holds an integer:

42

Gate the write with a bin-type filter, so the server skips records whose bin is not a String. The two SDKs take the test as AEL text; the other clients build the same comparison from expression calls. Clients differ in how they report the skip — some raise, some return an empty result, some say nothing — so the reliable signal is that the bin is unchanged.

// Returns an empty stream when the filter skips the record.
try (RecordStream rs = session.upsert(key)
.where("$.maybe_string.type() == STRING")
.bin("maybe_string").upper()
.execute()) {
RecordResult result = rs.next(); // null: the record was skipped
}

Or send the modify ungated and handle the type error, which costs a round trip on every record with the wrong type but needs no expression. Ask for error detail at verbosity 2 and the server says which operation wanted what — string_upper requires string bin, got integer — rather than leaving the handler to interpret code 12 on its own.

Behavior verbose = Behavior.DEFAULT.deriveWithChanges("verbose", b ->
b.on(Selectors.all(), ops ->
ops.errorDetailVerbosity(ErrorDetailVerbosity.MESSAGE)));
Session session = cluster.createSession(verbose);
try (RecordStream rs = session.upsert(key)
.bin("maybe_string").upper()
.execute()) {
rs.next().recordOrThrow();
} catch (AerospikeException e) {
String reason = e.getBaseMessage();
// reason: "string_upper requires string bin, got integer"
}