文字列関数

文字列関数

名前 説明
BYTE_LENGTH STRING 値または BYTES 値に含まれる BYTES の数を返します。
CHAR_LENGTH STRING 値に含まれる Unicode 文字数を返します。
STARTS_WITH STRING が指定された接頭辞で始まる場合、TRUE を返します。
ENDS_WITH STRING が指定された接尾辞で終わる場合、TRUE を返します。
LIKE STRING がパターンと一致する場合、TRUE を返します。
REGEX_CONTAINS 値が正規表現に対して部分一致または完全一致である場合、TRUE を返します。
REGEX_MATCH 値の一部が正規表現と一致する場合、TRUE を返します。
STRING_CONCAT 複数の STRING を 1 つの STRING に連結します。
STRING_CONTAINS 値に STRING が含まれている場合、TRUE を返します。
TO_UPPER STRING 値または BYTES 値を大文字に変換します。
TO_LOWER STRING 値または BYTES 値を小文字に変換します。
SUBSTRING STRING 値または BYTES 値の部分文字列を取得します。
STRING_REVERSE STRING 値または BYTES 値を元に戻します。
TRIM STRING 値または BYTES 値から先頭と末尾の文字を削除します。
SPLIT STRING 値または BYTES 値を配列に分割します。

BYTE_LENGTH

構文:

byte_length[T <: STRING | BYTES](value: T) -> INT64

説明:

STRING 値または BYTES 値に含まれる BYTES の数を返します。

例:

byte_length(value)
"abc" 3
"xyzabc" 6
b"abc" 3

Web

const result = await execute(db.pipeline()
  .collection("books")
  .select(
    field("title").byteLength().as("titleByteLength")
  )
);
Swift
let result = try await db.pipeline()
  .collection("books")
  .select([
    Field("title").byteLength().as("titleByteLength")
  ])
  .execute()

Kotlin

val result = db.pipeline()
    .collection("books")
    .select(
        field("title").byteLength().alias("titleByteLength")
    )
    .execute()

Java

Task<Pipeline.Snapshot> result = db.pipeline()
    .collection("books")
    .select(
        field("title").byteLength().alias("titleByteLength")
    )
    .execute();
Python
from google.cloud.firestore_v1.pipeline_expressions import Field

result = (
    client.pipeline()
    .collection("books")
    .select(Field.of("title").byte_length().as_("titleByteLength"))
    .execute()
)

CHAR_LENGTH

構文:

char_length(value: STRING) -> INT64

説明:

STRING 値に含まれる Unicode コードポイントの数を返します。

例:

char_length(value)
"abc" 3
"hello" 5
"world" 5

Web

const result = await execute(db.pipeline()
  .collection("books")
  .select(
    field("title").charLength().as("titleCharLength")
  )
);
Swift
let result = try await db.pipeline()
  .collection("books")
  .select([
    Field("title").charLength().as("titleCharLength")
  ])
  .execute()

Kotlin

val result = db.pipeline()
    .collection("books")
    .select(
        field("title").charLength().alias("titleCharLength")
    )
    .execute()

Java

Task<Pipeline.Snapshot> result = db.pipeline()
    .collection("books")
    .select(
        field("title").charLength().alias("titleCharLength")
    )
    .execute();
Python
from google.cloud.firestore_v1.pipeline_expressions import Field

result = (
    client.pipeline()
    .collection("books")
    .select(Field.of("title").char_length().as_("titleCharLength"))
    .execute()
)

STARTS_WITH

構文:

starts_with(value: STRING, prefix: STRING) -> BOOLEAN

説明:

valueprefix で始まる場合、TRUE を返します。

例:

接頭辞 starts_with(value, prefix)
"abc" "a" true
"abc" "b" false
"abc" "" true

Web

const result = await execute(db.pipeline()
  .collection("books")
  .select(
    field("title").startsWith("The")
      .as("needsSpecialAlphabeticalSort")
  )
);
Swift
let result = try await db.pipeline()
  .collection("books")
  .select([
    Field("title").startsWith("The")
      .as("needsSpecialAlphabeticalSort")
  ])
  .execute()

Kotlin

val result = db.pipeline()
    .collection("books")
    .select(
        field("title").startsWith("The")
            .alias("needsSpecialAlphabeticalSort")
    )
    .execute()

Java

Task<Pipeline.Snapshot> result = db.pipeline()
    .collection("books")
    .select(
        field("title").startsWith("The")
            .alias("needsSpecialAlphabeticalSort")
    )
    .execute();
Python
from google.cloud.firestore_v1.pipeline_expressions import Field

result = (
    client.pipeline()
    .collection("books")
    .select(
        Field.of("title").starts_with("The").as_("needsSpecialAlphabeticalSort")
    )
    .execute()
)

ENDS_WITH

構文:

ends_with(value: STRING, postfix: STRING) -> BOOLEAN

説明:

valuepostfix で終わる場合、TRUE を返します。

例:

postfix ends_with(value, postfix)
"abc" "c" true
"abc" "b" false
"abc" "" true
Swift
let result = try await db.pipeline()
  .collection("inventory/devices/laptops")
  .select([
    Field("name").endsWith("16 inch")
      .as("16InLaptops")
  ])
  .execute()

Kotlin

val result = db.pipeline()
    .collection("inventory/devices/laptops")
    .select(
        field("name").endsWith("16 inch")
            .alias("16InLaptops")
    )
    .execute()

Java

Task<Pipeline.Snapshot> result = db.pipeline()
    .collection("inventory/devices/laptops")
    .select(
        field("name").endsWith("16 inch")
            .alias("16InLaptops")
    )
    .execute();
Python
from google.cloud.firestore_v1.pipeline_expressions import Field

result = (
    client.pipeline()
    .collection("inventory/devices/laptops")
    .select(Field.of("name").ends_with("16 inch").as_("16InLaptops"))
    .execute()
)

LIKE

構文:

like(value: STRING, pattern: STRING) -> BOOLEAN

説明:

valuepattern に合致する場合、TRUE を返します。

例:

パターン like(value, pattern)
"Firestore" "Fire%" true
"Firestore" "%store" true
"Datastore" "Data_tore" true
"100%" "100\%" true

Web

const result = await execute(db.pipeline()
  .collection("books")
  .select(
    field("genre").like("%Fiction")
      .as("anyFiction")
  )
);
Swift
let result = try await db.pipeline()
  .collection("books")
  .select([
    Field("genre").like("%Fiction")
      .as("anyFiction")
  ])
  .execute()

Kotlin

val result = db.pipeline()
    .collection("books")
    .select(
        field("genre").like("%Fiction")
            .alias("anyFiction")
    )
    .execute()

Java

Task<Pipeline.Snapshot> result = db.pipeline()
    .collection("books")
    .select(
        field("genre").like("%Fiction")
            .alias("anyFiction")
    )
    .execute();
Python
from google.cloud.firestore_v1.pipeline_expressions import Field

result = (
    client.pipeline()
    .collection("books")
    .select(Field.of("genre").like("%Fiction").as_("anyFiction"))
    .execute()
)

REGEX_CONTAINS

構文:

regex_contains(value: STRING, pattern: STRING) -> BOOLEAN

説明:

value の一部が pattern に合致する場合、TRUE を返します。pattern が有効な正規表現でない場合、この関数は error を返します。

正規表現は re2 ライブラリの構文に従います。

例:

パターン regex_contains(value, pattern)
"Firestore" "Fire" true
"Firestore" "store$" true
"Firestore" "data" false

Web

const result = await execute(db.pipeline()
  .collection("documents")
  .select(
    field("title").regexContains("Firestore (Enterprise|Standard)")
      .as("isFirestoreRelated")
  )
);
Swift
let result = try await db.pipeline()
  .collection("documents")
  .select([
    Field("title").regexContains("Firestore (Enterprise|Standard)")
      .as("isFirestoreRelated")
  ])
  .execute()

Kotlin

val result = db.pipeline()
    .collection("documents")
    .select(
        field("title").regexContains("Firestore (Enterprise|Standard)")
            .alias("isFirestoreRelated")
    )
    .execute()

Java

Task<Pipeline.Snapshot> result = db.pipeline()
    .collection("documents")
    .select(
        field("title").regexContains("Firestore (Enterprise|Standard)")
            .alias("isFirestoreRelated")
    )
    .execute();
Python
from google.cloud.firestore_v1.pipeline_expressions import Field

result = (
    client.pipeline()
    .collection("documents")
    .select(
        Field.of("title")
        .regex_contains("Firestore (Enterprise|Standard)")
        .as_("isFirestoreRelated")
    )
    .execute()
)

REGEX_MATCH

構文:

regex_match(value: STRING, pattern: STRING) -> BOOLEAN

説明:

valuepattern に完全に合致する場合、TRUE を返します。pattern が有効な正規表現でない場合、この関数は error を返します。

正規表現は re2 ライブラリの構文に従います。

例:

パターン regex_match(value, pattern)
"Firestore" "F.*store" true
"Firestore" "Fire" false
"Firestore" "^F.*e$" true

Web

const result = await execute(db.pipeline()
  .collection("documents")
  .select(
    field("title").regexMatch("Firestore (Enterprise|Standard)")
      .as("isFirestoreExactly")
  )
);
Swift
let result = try await db.pipeline()
  .collection("documents")
  .select([
    Field("title").regexMatch("Firestore (Enterprise|Standard)")
      .as("isFirestoreExactly")
  ])
  .execute()

Kotlin

val result = db.pipeline()
    .collection("documents")
    .select(
        field("title").regexMatch("Firestore (Enterprise|Standard)")
            .alias("isFirestoreExactly")
    )
    .execute()

Java

Task<Pipeline.Snapshot> result = db.pipeline()
    .collection("documents")
    .select(
        field("title").regexMatch("Firestore (Enterprise|Standard)")
            .alias("isFirestoreExactly")
    )
    .execute();
Python
from google.cloud.firestore_v1.pipeline_expressions import Field

result = (
    client.pipeline()
    .collection("documents")
    .select(
        Field.of("title")
        .regex_match("Firestore (Enterprise|Standard)")
        .as_("isFirestoreExactly")
    )
    .execute()
)

STRING_CONCAT

構文:

string_concat(values: STRING...) -> STRING

説明:

1 つ以上の STRING 値を 1 つに連結します。

例:

引数 string_concat(values...)
() エラー
("a") "a"
("abc", "def") "abcdef"
("a", "", "c") "ac"

Web

const result = await execute(db.pipeline()
  .collection("books")
  .select(
    field("title").stringConcat(" by ", field("author"))
      .as("fullyQualifiedTitle")
  )
);
Swift
let result = try await db.pipeline()
  .collection("books")
  .select([
    Field("title").concat([" by ", Field("author")])
      .as("fullyQualifiedTitle")
  ])
  .execute()

Kotlin

val result = db.pipeline()
    .collection("books")
    .select(
        field("title").concat(" by ", field("author"))
            .alias("fullyQualifiedTitle")
    )
    .execute()

Java

Task<Pipeline.Snapshot> result = db.pipeline()
    .collection("books")
    .select(
        field("title").concat(" by ", field("author"))
            .alias("fullyQualifiedTitle")
    )
    .execute();
Python
from google.cloud.firestore_v1.pipeline_expressions import Field

result = (
    client.pipeline()
    .collection("books")
    .select(
        Field.of("title")
        .concat(" by ", Field.of("author"))
        .as_("fullyQualifiedTitle")
    )
    .execute()
)

STRING_CONTAINS

構文:

string_contains(value: STRING, substring: STRING) -> BOOLEAN

説明:

value にリテラル文字列 substring が含まれているかどうかをチェックします。

例:

substring string_contains(value, substring)
"abc" "b" true
"abc" "d" false
"abc" "" true
"a.c" "." true
"☃☃☃" "☃" true

Web

const result = await execute(db.pipeline()
  .collection("articles")
  .select(
    field("body").stringContains("Firestore")
      .as("isFirestoreRelated")
  )
);
Swift
let result = try await db.pipeline()
  .collection("articles")
  .select([
    Field("body").stringContains("Firestore")
      .as("isFirestoreRelated")
  ])
  .execute()

Kotlin

val result = db.pipeline()
    .collection("articles")
    .select(
        field("body").stringContains("Firestore")
            .alias("isFirestoreRelated")
    )
    .execute()

Java

Task<Pipeline.Snapshot> result = db.pipeline()
    .collection("articles")
    .select(
        field("body").stringContains("Firestore")
            .alias("isFirestoreRelated")
    )
    .execute();
Python
from google.cloud.firestore_v1.pipeline_expressions import Field

result = (
    client.pipeline()
    .collection("articles")
    .select(Field.of("body").string_contains("Firestore").as_("isFirestoreRelated"))
    .execute()
)

TO_UPPER

構文:

to_upper[T <: STRING | BYTES](value: T) -> T

説明:

STRING 値または BYTES 値を大文字に変換します。

バイトまたは文字が UTF-8 小文字アルファベットに対応していない場合は、変換せずに渡されます。

例:

to_upper(value)
"abc" "ABC"
"AbC" "ABC"
b"abc" b"ABC"
b"a1c" b"A1C"

Web

const result = await execute(db.pipeline()
  .collection("authors")
  .select(
    field("name").toUpper()
      .as("uppercaseName")
  )
);
Swift
let result = try await db.pipeline()
  .collection("authors")
  .select([
    Field("name").toUpper()
      .as("uppercaseName")
  ])
  .execute()

Kotlin

val result = db.pipeline()
    .collection("authors")
    .select(
        field("name").toUpper()
            .alias("uppercaseName")
    )
    .execute()

Java

Task<Pipeline.Snapshot> result = db.pipeline()
    .collection("authors")
    .select(
        field("name").toUpper()
            .alias("uppercaseName")
    )
    .execute();
Python
from google.cloud.firestore_v1.pipeline_expressions import Field

result = (
    client.pipeline()
    .collection("authors")
    .select(Field.of("name").to_upper().as_("uppercaseName"))
    .execute()
)

TO_LOWER

構文:

to_lower[T <: STRING | BYTES](value: T) -> T

説明:

STRING 値または BYTES 値を小文字に変換します。

バイトまたは文字が UTF-8 大文字アルファベットに対応していない場合は、変換せずに渡されます。

例:

to_lower(value)
"ABC" "abc"
"AbC" "abc"
"A1C" "a1c"
b"ABC" b"abc"

Web

const result = await execute(db.pipeline()
  .collection("authors")
  .select(
    field("genre").toLower().equal("fantasy")
      .as("isFantasy")
  )
);
Swift
let result = try await db.pipeline()
  .collection("authors")
  .select([
    Field("genre").toLower().equal("fantasy")
      .as("isFantasy")
  ])
  .execute()

Kotlin

val result = db.pipeline()
    .collection("authors")
    .select(
        field("genre").toLower().equal("fantasy")
            .alias("isFantasy")
    )
    .execute()

Java

Task<Pipeline.Snapshot> result = db.pipeline()
    .collection("authors")
    .select(
        field("genre").toLower().equal("fantasy")
            .alias("isFantasy")
    )
    .execute();
Python
from google.cloud.firestore_v1.pipeline_expressions import Field

result = (
    client.pipeline()
    .collection("authors")
    .select(Field.of("genre").to_lower().equal("fantasy").as_("isFantasy"))
    .execute()
)

SUBSTRING

構文:

substring[T <: STRING | BYTES](input: T, position: INT64) -> T
substring[T <: STRING | BYTES](input: T, position: INT64, length: INT64) -> T

説明:

position(ゼロベースのインデックス)から始まり、length エントリまでを含む input の部分文字列を返します。length が指定されていない場合、inputposition から末尾までの部分文字列を返します。

  • inputSTRING 値の場合、positionlength は Unicode コードポイント単位で数えます。BYTES 値の場合は、バイト単位で数えます。

  • positioninput の長さより大きい場合、空の部分文字列が返されます。positionlength の合計が input の長さより大きい場合、部分文字列は input の末尾まで切り捨てられます。

  • position が負の値の場合、位置は入力の末尾からカウントされます。負の値である position が入力のサイズより大きい場合、位置は 0 に設定されます。length は負の値にすることはできません。

例:

length が指定されていない場合:

入力 position substring(input, position)
"abc" 0 "abc"
"abc" 1 "bc"
"abc" 3 ""
"abc" -1 "c"
b"abc" 1 b"bc"

length が指定されている場合:

入力 position 長さ substring(input, position, length)
"abc" 0 1 "a"
"abc" 1 2 "bc"
"abc" -1 1 "c"
b"abc" 0 1 b"a"

Web

const result = await execute(db.pipeline()
  .collection("books")
  .where(field("title").startsWith("The "))
  .select(
    field("title").substring(4)
      .as("titleWithoutLeadingThe")
  )
);
Swift
let result = try await db.pipeline()
  .collection("books")
  .where(Field("title").startsWith("The "))
  .select([
    Field("title").substring(position: 4)
      .as("titleWithoutLeadingThe")
  ])
  .execute()

Kotlin

val result = db.pipeline()
    .collection("books")
    .where(field("title").startsWith("The "))
    .select(
        field("title")
          .substring(constant(4),
            field("title").charLength().subtract(4))
            .alias("titleWithoutLeadingThe")
    )
    .execute()

Java

Task<Pipeline.Snapshot> result = db.pipeline()
    .collection("books")
    .where(field("title").startsWith("The "))
    .select(
        field("title").substring(
          constant(4),
            field("title").charLength().subtract(4))
            .alias("titleWithoutLeadingThe")
    )
    .execute();
Python
from google.cloud.firestore_v1.pipeline_expressions import Field

result = (
    client.pipeline()
    .collection("books")
    .where(Field.of("title").starts_with("The "))
    .select(Field.of("title").substring(4).as_("titleWithoutLeadingThe"))
    .execute()
)

STRING_REVERSE

構文:

string_reverse[T <: STRING | BYTES](input: T) -> T

説明:

指定された入力を逆順で返します。

入力が STRING の場合は Unicode コードポイント単位で、入力が BYTES 値の場合はバイト単位で、文字が区切られます。

例:

入力 string_reverse(input)
"abc" "cba"
"a🌹b" "b🌹a"
"hello" "olleh"
b"abc" b"cba"

Web

const result = await execute(db.pipeline()
  .collection("books")
  .select(
    field("name").reverse().as("reversedName")
  )
);
Swift
let result = try await db.pipeline()
  .collection("books")
  .select([
    Field("name").reverse().as("reversedName")
  ])
  .execute()

Kotlin

val result = db.pipeline()
    .collection("books")
    .select(
        field("name").reverse().alias("reversedName")
    )
    .execute()

Java

Task<Pipeline.Snapshot> result = db.pipeline()
    .collection("books")
    .select(
        field("name").reverse().alias("reversedName")
    )
    .execute();
Python
from google.cloud.firestore_v1.pipeline_expressions import Field

result = (
    client.pipeline()
    .collection("books")
    .select(Field.of("name").string_reverse().as_("reversedName"))
    .execute()
)

TRIM

構文:

trim[T <: STRING | BYTES](input: T, values_to_trim: T) -> T
trim[T <: STRING | BYTES](input: T) -> T

説明:

指定された input の先頭と末尾から、指定された BYTES または CHARS のセットを削除します。

  • values_to_trim が指定されていない場合は、空白文字を削除します。

例:

values_to_trim が指定されていない場合:

入力 trim(input)
" foo " "foo"
b" foo " b"foo"
"foo" "foo"
"" ""
" " ""
"\t foo \n" "foo"
b"\t foo \n" b"foo"
"\r\f\v foo \r\f\v" "foo"
b"\r\f\v foo \r\f\v" b"foo"

values_to_trim が指定されている場合:

入力 values_to_trim trim(input, values_to_trim)
"abcbfooaacb" "abc" "foo"
"abcdaabadbac" "abc" "daabad"
b"C1C2C3" b"C1" b"C2C3"
b"C1C2" "foo" エラー
"foo" b"C1" エラー

Web

const result = await execute(db.pipeline()
  .collection("books")
  .select(
    field("name").trim().as("whitespaceTrimmedName")
  )
);
Swift
let result = try await db.pipeline()
  .collection("books")
  .select([
    Field("name").trim(" \n\t").as("whitespaceTrimmedName")
  ])
  .execute()

Kotlin

val result = db.pipeline()
    .collection("books")
    .select(
        field("name").trim().alias("whitespaceTrimmedName")
    )
    .execute()

Java

Task<Pipeline.Snapshot> result = db.pipeline()
    .collection("books")
    .select(
        field("name").trim().alias("whitespaceTrimmedName")
    )
    .execute();
Python
from google.cloud.firestore_v1.pipeline_expressions import Field

result = (
    client.pipeline()
    .collection("books")
    .select(Field.of("name").trim().as_("whitespaceTrimmedName"))
    .execute()
)

SPLIT

構文:

split(input: STRING) -> ARRAY<STRING>
split[T <: STRING | BYTES](input: T, delimiter: T) -> ARRAY<T>

説明:

STRING 値または BYTES 値を、区切り文字を使用して分割します。

  • STRING の場合、デフォルトの区切り文字はカンマ , です。区切り文字は単一の文字列として扱われます。

  • BYTES の場合は、区切り文字を指定する必要があります。

  • 空の区切り文字で分割すると、STRING 値の場合は Unicode コードポイントの配列が生成され、BYTES 値の場合は BYTES の配列が生成されます。

  • 空の STRING を分割すると、1 つの空の STRING を持つ ARRAY が返されます。

例:

delimiter が指定されていない場合:

入力 split(input)
"foo,bar,foo" ["foo", "bar", "foo"]
"foo" ["foo"]
",foo," ["", "foo", ""]
"" [""]
b"C120C2C4" エラー

delimiter が指定されている場合:

入力 区切り文字 split(input, delimiter)
"foo bar foo" " " ["foo", "bar", "foo"]
"foo bar foo" "z" ["foo bar foo"]
"abc" "" ["a", "b", "c"]
b"C1,C2,C4" b"," [b"C1", b"C2", b"C4"]
b"ABC" b"" [b"A", b"B", b"C"]
"foo" b"C1" エラー