字符串
string
Section titled “string”stringdefault: ""
由引号包围的任何 UTF-8 编码字符序列都是 string:"foo"。
export component Example inherits Text { text: "hello";}可以在字符串中嵌入转义序列,以插入否则难以插入的字符:
| 转义 | 结果 |
|---|---|
\" |
" |
\\ |
\ |
\n |
换行 |
\u{x} |
其中 x 是十六进制数,展开为该数字表示的 Unicode 码位 |
\{expression} |
求值该表达式的结果 |
未转义 \ 后面的任何其他内容都是错误。
在 JavaScript 中,string 类型的属性或结构体字段映射到 String 类型。
在 Python 中,string 类型的属性或结构体字段映射到 原生 str 类型。
字符串具有以下属性和成员函数:
is-empty
Section titled “is-empty”当字符串不包含任何内容时为 true,否则为 false。
export component LengthOfString { property<bool> empty: "".is-empty; // true property<bool> not-empty: "hello".is-empty; // false}character-count
Section titled “character-count”字符串中 字素簇 的数量。
export component CharacterCountOfString { property<int> empty: "".character-count; // 0 property<int> hello: "hello".character-count; // 5 property<int> hiragana: "あいうえお".character-count; // 5 property<int> surrogate-pair: "😊𩸽".character-count; // 2 property<int> variation-selectors: "👍🏿".character-count; // 1 property<int> combining-character: "パ".character-count; // 1 property<int> zero-width-joiner: "👨👩👧👦".character-count; // 1 property<int> region-indicator-character: "🇦🇿🇿🇦".character-count; // 2 property<int> emoji-tag-sequences: "🏴".character-count; // 1}to-lowercase() -> string
Section titled “to-lowercase() -> string”返回转换为小写的字符串, 依据 Unicode 字符属性。
export component StringToLowercase { property<string> hello: "HELLO".to-lowercase(); // "hello" property<string> odysseus: "ὈΔΥΣΣΕΎΣ".to-lowercase(); // "ὀδυσσεύς"}to-uppercase() -> string
Section titled “to-uppercase() -> string”返回转换为大写的字符串, 依据 Unicode 字符属性。
export component StringToUppercase { property<string> bye: "tschüß".to-uppercase(); // "TSCHÜSS" property<string> new-year: "农历新年".to-uppercase(); // "农历新年"}starts-with(pat: string) -> bool
Section titled “starts-with(pat: string) -> bool”如果给定模式匹配此字符串的前缀,则返回 true。
如果不匹配,则返回 false。
export component StringStartsWith { property<bool> res1: "Hello".starts-with("He"); // true property<bool> res2: "你好".starts-with("好"); // false}ends-with(pat: string) -> bool
Section titled “ends-with(pat: string) -> bool”如果给定模式匹配此字符串的后缀,则返回 true。
如果不匹配,则返回 false。
export component StringEndsWith { property<bool> res1: "Hello".ends-with("lo"); // true property<bool> res2: "你好".ends-with("你"); // false}to-float() -> float
Section titled “to-float() -> float”返回字符串中包含的数字,如果字符串不是有效数字则返回 0。
使用 is-float() 区分保存数字 0 的字符串和不是有效数字的字符串。
字符串使用当前区域设置的小数分隔符进行解析。 参见 数字与字符串之间的转换。
is-float() -> bool
Section titled “is-float() -> bool”如果字符串包含有效数字则返回 true,否则返回 false。
与 to-float() 一样,它使用当前区域设置的小数分隔符。
export component StringToFloat { property<float> hello: "hello".to-float(); // 0 property<bool> goodbye: "goodbye".is-float(); // false property<float> value: "1.5".to-float(); // 1.5 if the decimal separator is "."}replace-all(from: string, to: string) -> string
Section titled “replace-all(from: string, to: string) -> string”返回将 from 的所有实例替换为 to 的字符串。
export component StringToReplaceAll { property<string> basic: "hello".replace-all("l", "L"); // "heLLo" property<string> repeated: "abcabcabc".replace-all("abc", "x"); // "xxx" property<string> replace-all-empty-find: "abc".replace-all("", "-"); // "-a-b-c-" property<string> empty-haystack: "".replace-all("a", "b"); // "" property<string> no-match: "hello".replace-all("z", "Z"); // "hello" property<string> hiragana: "あいうえお".replace-all("い", "亜"); // "あ亜うえお"}styled-text
Section titled “styled-text”styled-textdefault: ""
与 StyledText 元素 一起使用的属性。
使用 @markdown() 宏创建样式化文本。例如:@markdown("Hello **Bold**")。
@markdown() 支持插值:例如,@markdown("5 + 5 = *\{ 5 + 5 }*") 变为 "5 + 5 = *10*"。
作为参数传递给宏的任何文本都会被转义,例如 @markdown("Hello \{"*World*"}") 会变为 "Hello \*World\*"。
如果使用多个字符串字面量,它们会被连接起来。 不带字符串内容的调用是编译错误。
使用 <font color='...'> 应用颜色。
字面颜色名称和十六进制值可直接使用:@markdown("<font color='red'>warning</font>")。
要插入动态颜色,表达式必须是 color 类型:
@markdown("<font color='\{self.my-color}'>colored</font>")。
HTML 标签内的插值只允许在 color 属性值中使用。
在运行时使用 StyledText API 解析 markdown 以创建样式化文本:
© 2026 SixtyFPS GmbH