docs-v2/content/flux/v0/stdlib/strings/strlen.md

4.6 KiB

title description menu weight
strings.strlen() function `strings.strlen()` returns the length of a string. String length is determined by the number of UTF code points a string contains.
flux_v0_ref
name parent identifier
strings.strlen strings strings/strlen
101

strings.strlen() returns the length of a string. String length is determined by the number of UTF code points a string contains.

Function type signature
(v: string) => int

{{% caption %}} For more information, see Function type signatures. {{% /caption %}}

Parameters

v

({{< req >}}) String value to measure.

Examples

Filter based on string value length

import "strings"

data
    |> filter(fn: (r) => strings.strlen(v: r._value) <= 6)

{{< expand-wrapper >}} {{% expand "View example input and output" %}}

Input data

_time _value *tag
2021-01-01T00:00:00Z pl_gqcz t1
2021-01-01T00:00:10Z pl_gvn t1
2021-01-01T00:00:20Z pl_phw t1
2021-01-01T00:00:30Z pl_guvzy t1
2021-01-01T00:00:40Z pl_vcce t1
2021-01-01T00:00:50Z pl_fgy t1
_time _value *tag
2021-01-01T00:00:00Z pl_beida t2
2021-01-01T00:00:10Z pl_euoxp t2
2021-01-01T00:00:20Z pl_gtz t2
2021-01-01T00:00:30Z pl_oxut t2
2021-01-01T00:00:40Z pl_wf t2
2021-01-01T00:00:50Z pl_dtnbv t2

Output data

_time _value *tag
2021-01-01T00:00:10Z pl_gvn t1
2021-01-01T00:00:20Z pl_phw t1
2021-01-01T00:00:50Z pl_fgy t1
_time _value *tag
2021-01-01T00:00:20Z pl_gtz t2
2021-01-01T00:00:40Z pl_wf t2

{{% /expand %}} {{< /expand-wrapper >}}

Store the length of string values

import "strings"

data
    |> map(fn: (r) => ({r with length: strings.strlen(v: r._value)}))

{{< expand-wrapper >}} {{% expand "View example input and output" %}}

Input data

_time _value *tag
2021-01-01T00:00:00Z pl_gqcz t1
2021-01-01T00:00:10Z pl_gvn t1
2021-01-01T00:00:20Z pl_phw t1
2021-01-01T00:00:30Z pl_guvzy t1
2021-01-01T00:00:40Z pl_vcce t1
2021-01-01T00:00:50Z pl_fgy t1
_time _value *tag
2021-01-01T00:00:00Z pl_beida t2
2021-01-01T00:00:10Z pl_euoxp t2
2021-01-01T00:00:20Z pl_gtz t2
2021-01-01T00:00:30Z pl_oxut t2
2021-01-01T00:00:40Z pl_wf t2
2021-01-01T00:00:50Z pl_dtnbv t2

Output data

_time _value length *tag
2021-01-01T00:00:00Z pl_gqcz 7 t1
2021-01-01T00:00:10Z pl_gvn 6 t1
2021-01-01T00:00:20Z pl_phw 6 t1
2021-01-01T00:00:30Z pl_guvzy 8 t1
2021-01-01T00:00:40Z pl_vcce 7 t1
2021-01-01T00:00:50Z pl_fgy 6 t1
_time _value length *tag
2021-01-01T00:00:00Z pl_beida 8 t2
2021-01-01T00:00:10Z pl_euoxp 8 t2
2021-01-01T00:00:20Z pl_gtz 6 t2
2021-01-01T00:00:30Z pl_oxut 7 t2
2021-01-01T00:00:40Z pl_wf 5 t2
2021-01-01T00:00:50Z pl_dtnbv 8 t2

{{% /expand %}} {{< /expand-wrapper >}}