Skip to content

docs: update str interpolation dollar escape documents #400

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 8 additions & 7 deletions docs/reference/lang/tour.md
Original file line number Diff line number Diff line change
Expand Up @@ -360,18 +360,19 @@ assert "length length".title() == "Length Length"
assert x.upper() == "LENGTH"
```

There are 2 different ways to format a string: to use the `"{}".format()` built-in function, or to specify the variable between the curly braces and use a `$` mark to tell KCL to extract its value. This is called **string interpolation** in KCL. In following example, both `a` and `b` will be assigned to string `"hello world"`.
There are 2 different ways to format a string: to use the `"{}".format()` built-in function, or to specify the variable between the curly braces and use a `${}` mark to tell KCL to extract its value. This is called **string interpolation** in KCL. In following example, both `a` and `b` will be assigned to string `"hello world"`.

Besides, the variable to serialized can be extracted in special data format, such as YAML or JSON. In this case, a `#yaml` or `#json` can be included within the curly braces.
Besides, the variable to serialized can be extracted in special data format, such as YAML or JSON. In this case, a `#yaml` or `#json` can be included within the curly braces.

Specifically, when the dollar sign `$` itself is needed in a **string interpolation**, it needs to be escaped and use `$$` instead. Or in another way, `+` can be used to concat the dollar sign with the **string interpolation** to avoid that escape. In following example, both `c` and `c2` will be assigned to string `$hello world$`
Note that if we don't want to interpolate variables, we can add the `\` character before `$`.

```python
world = "world"
a = "hello {}".format(world) # "hello world"
b = "hello ${world}" # "hello world"
c = "$$hello ${world}$$" # "$hello world$"
c2 = "$" + "hello ${world}" + "$" # "$hello world$"
a = "hello {}".format(world) # "hello world"
b = "hello ${world}" # "hello world"
c1 = "$hello ${world}$" # "$hello world$"
c2 = "$" + "hello ${world}" + "$" # "$hello world$"
c3 = "$" + "hello \${world}" + "$" # "$hello ${world}$"

myDict = {
"key1" = "value1"
Expand Down
11 changes: 6 additions & 5 deletions docs/user_docs/support/faq-kcl.md
Original file line number Diff line number Diff line change
Expand Up @@ -515,14 +515,15 @@ data: value
jsonData: '{"key": "value"}'
```

Note that if we want to use the `$` character alone in the `${}` interpolated string, we need to escape the `$` with `$$`
Note that if we don't want to interpolate variables, we can add the `\` character before `$`.

```python
world = "world"
a = "hello {}".format(world) # "hello world"
b = "hello ${world}" # "hello world"
c = "$$hello ${world}$$" # "$hello world$"
c2 = "$" + "hello ${world}" + "$" # "$hello world$"
a = "hello {}".format(world) # "hello world"
b = "hello ${world}" # "hello world"
c1 = "$hello ${world}$" # "$hello world$"
c2 = "$" + "hello ${world}" + "$" # "$hello world$"
c3 = "$" + "hello \${world}" + "$" # "$hello ${world}$"
```

The output YAML is
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -364,14 +364,15 @@ assert x.upper() == "LENGTH"

此外,要序列化的变量可以以特殊的数据格式提取,例如 YAML 或 JSON。在这种情况中,`#yaml` 或 `#json` 可以包含在花括号中。

具体来说,当 `$` 符号本身需要出现在**插值字符串**中,需要使用 `$$` 转义。或者使用 `+` 符号连接 `$` 符号和插值字符串来避免转义。在以下示例中,`c` 和 `c2` 的值都是 `$hello world$`。
注意,如果不想 `${...}` 表示字符串插值 ,我们可以在 `$` 之前添加`\` 字符表示直接以字符串的形式输出 `${...}`。

```python
world = "world"
a = "hello {}".format(world) # "hello world"
b = "hello ${world}" # "hello world"
c = "$$hello ${world}$$" # "$hello world$"
c2 = "$" + "hello ${world}" + "$" # "$hello world$"
a = "hello {}".format(world) # "hello world"
b = "hello ${world}" # "hello world"
c1 = "$hello ${world}$" # "$hello world$"
c2 = "$" + "hello ${world}" + "$" # "$hello world$"
c3 = "$" + "hello \${world}" + "$" # "$hello ${world}$"

myDict = {
"key1" = "value1"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -517,14 +517,15 @@ data: value
jsonData: '{"key": "value"}'
```

注意,如果想在 `${}` 插值字符串中单独使用 `$` 字符,则需要使用 `$$` 对 `$` 进行转义
注意,如果不想 `${...}` 表示字符串插值 ,我们可以在 `$` 之前添加`\` 字符表示直接以字符串的形式输出 `${...}`。

```python
world = "world"
a = "hello {}".format(world) # "hello world"
b = "hello ${world}" # "hello world"
c = "$$hello ${world}$$" # "$hello world$"
c2 = "$" + "hello ${world}" + "$" # "$hello world$"
a = "hello {}".format(world) # "hello world"
b = "hello ${world}" # "hello world"
c1 = "$hello ${world}$" # "$hello world$"
c2 = "$" + "hello ${world}" + "$" # "$hello world$"
c3 = "$" + "hello \${world}" + "$" # "$hello ${world}$"
```

输出 YAML 为:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -364,14 +364,15 @@ assert x.upper() == "LENGTH"

此外,要序列化的变量可以以特殊的数据格式提取,例如 YAML 或 JSON。在这种情况中,`#yaml` 或 `#json` 可以包含在花括号中。

具体来说,当 `$` 符号本身需要出现在**插值字符串**中,需要使用 `$$` 转义。或者使用 `+` 符号连接 `$` 符号和插值字符串来避免转义。在以下示例中,`c` 和 `c2` 的值都是 `$hello world$`。
注意,如果不想 `${...}` 表示字符串插值 ,我们可以在 `$` 之前添加`\` 字符表示直接以字符串的形式输出 `${...}`。

```python
world = "world"
a = "hello {}".format(world) # "hello world"
b = "hello ${world}" # "hello world"
c = "$$hello ${world}$$" # "$hello world$"
c2 = "$" + "hello ${world}" + "$" # "$hello world$"
a = "hello {}".format(world) # "hello world"
b = "hello ${world}" # "hello world"
c1 = "$hello ${world}$" # "$hello world$"
c2 = "$" + "hello ${world}" + "$" # "$hello world$"
c3 = "$" + "hello \${world}" + "$" # "$hello ${world}$"

myDict = {
"key1" = "value1"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -517,14 +517,15 @@ data: value
jsonData: '{"key": "value"}'
```

注意,如果想在 `${}` 插值字符串中单独使用 `$` 字符,则需要使用 `$$` 对 `$` 进行转义
注意,如果不想 `${...}` 表示字符串插值 ,我们可以在 `$` 之前添加`\` 字符表示直接以字符串的形式输出 `${...}`。

```python
world = "world"
a = "hello {}".format(world) # "hello world"
b = "hello ${world}" # "hello world"
c = "$$hello ${world}$$" # "$hello world$"
c2 = "$" + "hello ${world}" + "$" # "$hello world$"
a = "hello {}".format(world) # "hello world"
b = "hello ${world}" # "hello world"
c1 = "$hello ${world}$" # "$hello world$"
c2 = "$" + "hello ${world}" + "$" # "$hello world$"
c3 = "$" + "hello \${world}" + "$" # "$hello ${world}$"
```

输出 YAML 为:
Expand Down Expand Up @@ -2642,4 +2643,4 @@ another_module = "0.1.1"
import another_module
```

`another-module = "0.1.1"` 和 `another_module = "0.1.1"` 是等价的, 如果同时在 `kcl.mod` 中使用这两种写法会得到一个错误。
`another-module = "0.1.1"` 和 `another_module = "0.1.1"` 是等价的, 如果同时在 `kcl.mod` 中使用这两种写法会得到一个错误。
15 changes: 8 additions & 7 deletions versioned_docs/version-0.9/reference/lang/tour.md
Original file line number Diff line number Diff line change
Expand Up @@ -360,18 +360,19 @@ assert "length length".title() == "Length Length"
assert x.upper() == "LENGTH"
```

There are 2 different ways to format a string: to use the `"{}".format()` built-in function, or to specify the variable between the curly braces and use a `$` mark to tell KCL to extract its value. This is called **string interpolation** in KCL. In following example, both `a` and `b` will be assigned to string `"hello world"`.
There are 2 different ways to format a string: to use the `"{}".format()` built-in function, or to specify the variable between the curly braces and use a `${}` mark to tell KCL to extract its value. This is called **string interpolation** in KCL. In following example, both `a` and `b` will be assigned to string `"hello world"`.

Besides, the variable to serialized can be extracted in special data format, such as YAML or JSON. In this case, a `#yaml` or `#json` can be included within the curly braces.
Besides, the variable to serialized can be extracted in special data format, such as YAML or JSON. In this case, a `#yaml` or `#json` can be included within the curly braces.

Specifically, when the dollar sign `$` itself is needed in a **string interpolation**, it needs to be escaped and use `$$` instead. Or in another way, `+` can be used to concat the dollar sign with the **string interpolation** to avoid that escape. In following example, both `c` and `c2` will be assigned to string `$hello world$`
Note that if we don't want to interpolate variables, we can add the `\` character before `$`.

```python
world = "world"
a = "hello {}".format(world) # "hello world"
b = "hello ${world}" # "hello world"
c = "$$hello ${world}$$" # "$hello world$"
c2 = "$" + "hello ${world}" + "$" # "$hello world$"
a = "hello {}".format(world) # "hello world"
b = "hello ${world}" # "hello world"
c1 = "$hello ${world}$" # "$hello world$"
c2 = "$" + "hello ${world}" + "$" # "$hello world$"
c3 = "$" + "hello \${world}" + "$" # "$hello ${world}$"

myDict = {
"key1" = "value1"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Getting Started

Get started include a quick overview of the KCL programming language.
Getting Started include a quick overview of the KCL programming language.

import DocCardList from '@theme/DocCardList';

Expand Down
11 changes: 6 additions & 5 deletions versioned_docs/version-0.9/user_docs/support/faq-kcl.md
Original file line number Diff line number Diff line change
Expand Up @@ -515,14 +515,15 @@ data: value
jsonData: '{"key": "value"}'
```

Note that if we want to use the `$` character alone in the `${}` interpolated string, we need to escape the `$` with `$$`
Note that if we don't want to interpolate variables, we can add the `\` character before `$`.

```python
world = "world"
a = "hello {}".format(world) # "hello world"
b = "hello ${world}" # "hello world"
c = "$$hello ${world}$$" # "$hello world$"
c2 = "$" + "hello ${world}" + "$" # "$hello world$"
a = "hello {}".format(world) # "hello world"
b = "hello ${world}" # "hello world"
c1 = "$hello ${world}$" # "$hello world$"
c2 = "$" + "hello ${world}" + "$" # "$hello world$"
c3 = "$" + "hello \${world}" + "$" # "$hello ${world}$"
```

The output YAML is
Expand Down
Loading