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

117 lines
3.2 KiB
Markdown

---
title: strings.countStr() function
description: >
`strings.countStr()` counts the number of non-overlapping instances of a substring appears in a string.
menu:
flux_v0_ref:
name: strings.countStr
parent: strings
identifier: strings/countStr
weight: 101
---
<!------------------------------------------------------------------------------
IMPORTANT: This page was generated from comments in the Flux source code. Any
edits made directly to this page will be overwritten the next time the
documentation is generated.
To make updates to this documentation, update the function comments above the
function definition in the Flux source code:
https://github.com/influxdata/flux/blob/master/stdlib/strings/strings.flux#L409-L409
Contributing to Flux: https://github.com/influxdata/flux#contributing
Fluxdoc syntax: https://github.com/influxdata/flux/blob/master/docs/fluxdoc.md
------------------------------------------------------------------------------->
`strings.countStr()` counts the number of non-overlapping instances of a substring appears in a string.
##### Function type signature
```js
(substr: string, v: string) => int
```
{{% caption %}}
For more information, see [Function type signatures](/flux/v0/function-type-signatures/).
{{% /caption %}}
## Parameters
### v
({{< req >}})
String value to search.
### substr
({{< req >}})
Substring to count occurrences of.
The function counts only non-overlapping instances of `substr`.
## Examples
### Count instances of a substring within a string
```js
import "sampledata"
import "strings"
sampledata.string()
|> map(fn: (r) => ({r with _value: strings.countStr(v: r._value, substr: "p")}))
```
{{< expand-wrapper >}}
{{% expand "View example input and output" %}}
#### Input data
| _time | *tag | _value |
| -------------------- | ---- | ----------- |
| 2021-01-01T00:00:00Z | t1 | smpl_g9qczs |
| 2021-01-01T00:00:10Z | t1 | smpl_0mgv9n |
| 2021-01-01T00:00:20Z | t1 | smpl_phw664 |
| 2021-01-01T00:00:30Z | t1 | smpl_guvzy4 |
| 2021-01-01T00:00:40Z | t1 | smpl_5v3cce |
| 2021-01-01T00:00:50Z | t1 | smpl_s9fmgy |
| _time | *tag | _value |
| -------------------- | ---- | ----------- |
| 2021-01-01T00:00:00Z | t2 | smpl_b5eida |
| 2021-01-01T00:00:10Z | t2 | smpl_eu4oxp |
| 2021-01-01T00:00:20Z | t2 | smpl_5g7tz4 |
| 2021-01-01T00:00:30Z | t2 | smpl_sox1ut |
| 2021-01-01T00:00:40Z | t2 | smpl_wfm757 |
| 2021-01-01T00:00:50Z | t2 | smpl_dtn2bv |
#### Output data
| _time | _value | *tag |
| -------------------- | ------- | ---- |
| 2021-01-01T00:00:00Z | 1 | t1 |
| 2021-01-01T00:00:10Z | 1 | t1 |
| 2021-01-01T00:00:20Z | 2 | t1 |
| 2021-01-01T00:00:30Z | 1 | t1 |
| 2021-01-01T00:00:40Z | 1 | t1 |
| 2021-01-01T00:00:50Z | 1 | t1 |
| _time | _value | *tag |
| -------------------- | ------- | ---- |
| 2021-01-01T00:00:00Z | 1 | t2 |
| 2021-01-01T00:00:10Z | 2 | t2 |
| 2021-01-01T00:00:20Z | 1 | t2 |
| 2021-01-01T00:00:30Z | 1 | t2 |
| 2021-01-01T00:00:40Z | 1 | t2 |
| 2021-01-01T00:00:50Z | 1 | t2 |
{{% /expand %}}
{{< /expand-wrapper >}}