Skip to content

Commit

Permalink
Merge pull request #36 from onflow/sainati/string-function-docs
Browse files Browse the repository at this point in the history
add documentation for new string functions
  • Loading branch information
dsainati1 authored Dec 22, 2023
2 parents 2d3ae7f + e7404b0 commit fa20152
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions versioned_docs/version-1.0/language/values-and-types.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -889,6 +889,34 @@ Strings have multiple built-in functions you can use:
example.toLower() // is `flowers`
```

-
```cadence
view fun replaceAll(of: String, with: String): String
```

Returns a string where all occurences of `of` are replaced with `with`.
If `of` is empty, it matches at the beginning of the string and after each UTF-8 sequence, yielding k+1 replacements for a string of length k.

```cadence
let example = "abababa"
example.replaceAll(of: "a", with: "o") // is `obobobo`
```

-
```cadence
view fun split(separator: String): [String]
```

Returns the variable-sized array of strings created splitting the receiver string on the `separator`.

```cadence
let example = "hello world"
example.split(separator: " ") // is `["hello", "world"]`
```


The `String` type also provides the following functions:

-
Expand All @@ -904,6 +932,18 @@ The `String` type also provides the following functions:
String.encodeHex(data) // is `"010203cade"`
```

-
```cadence
view fun String.join(_ strings: [String], separator: String): String
```

Returns the string created by joining the array of `strings` with the provided `separator`.

```cadence
let strings = ["hello", "world"]
String.join(strings, " ") // is "hello world"
```

`String`s are also indexable, returning a `Character` value.

```cadence
Expand Down

0 comments on commit fa20152

Please sign in to comment.