提示:本教程存在过时问题,更详细教程请阅读 小蓝书Typst 中文社区导航 FAQ
注意:该中文文档为社区驱动的非官网文档,可能存在错译、漏译或过时等问题,请以官网文档 Documentation 为准,如发现错漏,也欢迎 您的贡献镜像)。Typst 非官方中文交流 QQ 群:793548390
Warning: the Chinese document is a community-driven non-official document, there may be mistranslation, omission or outdated problems, please refer to the official website documentation.
Typst 中文文档

str

A sequence of Unicode codepoints.

You can iterate over the grapheme clusters of the string using a for loop. Grapheme clusters are basically characters but keep together things that belong together, e.g. multiple codepoints that together form a flag emoji. Strings can be added with the + operator, joined together and multiplied with integers.

Typst provides utility methods for string manipulation. Many of these methods (e.g., split, trim and replace) operate on patterns: A pattern can be either a string or a regular expression. This makes the methods quite versatile.

All lengths and indices are expressed in terms of UTF-8 bytes. Indices are zero-based and negative indices wrap around to the end of the string.

You can convert a value to a string with this type's constructor.

Example

#"hello world!" \
#"\"hello\n  world\"!" \
#"1 2 3".split() \
#"1,2;3".split(regex("[,;]")) \
#(regex("\d+") in "ten euros") \
#(regex("\d+") in "10 euros")
Preview

Escape sequences

Just like in markup, you can escape a few symbols in strings:

构造函数
如果类型具有构造函数,可以像函数一样调用它来创建一个该类型的值。

Converts a value to a string.

If you wish to convert from and to Unicode code points, see the to-unicode and from-unicode functions.

#str(10) \
#str(4000, base: 16) \
#str(2.7) \
#str(1e8) \
#str(<intro>)
Preview

value
int float str bytes label version type
必需参数位置参数
位置参数按顺序传入,不带名称。

The value that should be converted to a string.

base
int

The base (radix) to display integers in, between 2 and 36.

默认:10

定义
函数和类型可以有与其关联的定义 (成员或方法)。可以使用 "." 操作符来访问调用它们。

len

The length of the string in UTF-8 encoded bytes.

self.len(
) -> int

first

Extracts the first grapheme cluster of the string. Fails with an error if the string is empty.

self.first(
) -> str

last

Extracts the last grapheme cluster of the string. Fails with an error if the string is empty.

self.last(
) -> str

at

Extracts the first grapheme cluster after the specified index. Returns the default value if the index is out of bounds or fails with an error if no default value was specified.

self.at() -> any

index
int
必需参数位置参数
位置参数按顺序传入,不带名称。

The byte index. If negative, indexes from the back.

default
any

A default value to return if the index is out of bounds.

slice

Extracts a substring of the string. Fails with an error if the start or end index is out of bounds.

self.slice() -> str

start
int
必需参数位置参数
位置参数按顺序传入,不带名称。

The start byte index (inclusive). If negative, indexes from the back.

end
none int
位置参数
位置参数按顺序传入,不带名称。

The end byte index (exclusive). If omitted, the whole slice until the end of the string is extracted. If negative, indexes from the back.

默认:none

count
int

The number of bytes to extract. This is equivalent to passing start + count as the end position. Mutually exclusive with end.

clusters

Returns the grapheme clusters of the string as an array of substrings.

self.clusters(
) -> array

codepoints

Returns the Unicode codepoints of the string as an array of substrings.

self.codepoints(
) -> array

to-unicode

Converts a character into its corresponding code point.

str.to-unicode() -> int
查看示例
#"a".to-unicode() \
#("a\u{0300}"
   .codepoints()
   .map(str.to-unicode))
Preview

character
str
必需参数位置参数
位置参数按顺序传入,不带名称。

The character that should be converted.

from-unicode

Converts a unicode code point into its corresponding string.

str.from-unicode() -> str
查看示例
#str.from-unicode(97)
Preview

value
int
必需参数位置参数
位置参数按顺序传入,不带名称。

The code point that should be converted.

contains

Whether the string contains the specified pattern.

This method also has dedicated syntax: You can write "bc" in "abcd" instead of "abcd".contains("bc").

self.contains() -> bool

pattern
str regex
必需参数位置参数
位置参数按顺序传入,不带名称。

The pattern to search for.

starts-with

Whether the string starts with the specified pattern.

self.starts-with() -> bool

pattern
str regex
必需参数位置参数
位置参数按顺序传入,不带名称。

The pattern the string might start with.

ends-with

Whether the string ends with the specified pattern.

self.ends-with() -> bool

pattern
str regex
必需参数位置参数
位置参数按顺序传入,不带名称。

The pattern the string might end with.

find

Searches for the specified pattern in the string and returns the first match as a string or none if there is no match.

self.find() -> nonestr

pattern
str regex
必需参数位置参数
位置参数按顺序传入,不带名称。

The pattern to search for.

position

Searches for the specified pattern in the string and returns the index of the first match as an integer or none if there is no match.

self.position() -> noneint

pattern
str regex
必需参数位置参数
位置参数按顺序传入,不带名称。

The pattern to search for.

match

Searches for the specified pattern in the string and returns a dictionary with details about the first match or none if there is no match.

The returned dictionary has the following keys:

self.match() -> nonedictionary

pattern
str regex
必需参数位置参数
位置参数按顺序传入,不带名称。

The pattern to search for.

matches

Searches for the specified pattern in the string and returns an array of dictionaries with details about all matches. For details about the returned dictionaries, see above.

self.matches() -> array

pattern
str regex
必需参数位置参数
位置参数按顺序传入,不带名称。

The pattern to search for.

replace

Replace at most count occurrences of the given pattern with a replacement string or function (beginning from the start). If no count is given, all occurrences are replaced.

self.replace() -> str

pattern
str regex
必需参数位置参数
位置参数按顺序传入,不带名称。

The pattern to search for.

replacement
str function
必需参数位置参数
位置参数按顺序传入,不带名称。

The string to replace the matches with or a function that gets a dictionary for each match and can return individual replacement strings.

count
int

If given, only the first count matches of the pattern are placed.

trim

Removes matches of a pattern from one or both sides of the string, once or repeatedly and returns the resulting string.

pattern
none str regex
位置参数
位置参数按顺序传入,不带名称。

The pattern to search for.

默认:none

at

Can be start or end to only trim the start or end of the string. If omitted, both sides are trimmed.

repeat

Whether to repeatedly removes matches of the pattern or just once. Defaults to true.

默认:true

split

Splits a string at matches of a specified pattern and returns an array of the resulting parts.

self.split() -> array

pattern
none str regex
位置参数
位置参数按顺序传入,不带名称。

The pattern to split at. Defaults to whitespace.

默认:none

rev

Reverse the string.

self.rev(
) -> str