# react-native: decodeVars URL: https://generaltranslation.com/zh/docs/react-native/api/strings/decode-vars.mdx --- title: decodeVars description: decodeVars() 字符串函数 API 参考 --- {/* 自动生成:请勿直接编辑。请改为编辑 content/docs-templates/ 中的 template。 */} ## 概述 由于 `declareVar` 会在源文本中添加与 ICU 兼容的标记,因此可能会影响现有的字符串处理逻辑。 `decodeVars` 函数用于从包含 `declareVar` 标记的字符串中提取原始值。 ```jsx function getGreeting({ name }) { const greeting = "Hello, " + declareVar(name); // "Hello, {_gt_, select, other {Brian}}" const decodedGreeting = decodeVars(greeting); // "Hello, Brian" <- 该字符串与从未调用过 `declareVar` 时的结果相同 return decodedGreeting; } ``` 由于 `declareVar` 只会影响已编码的字符串,因此它仅用于源字符串,对译文字符串不起作用。 **字符串处理:** 当你需要用现有的字符串处理逻辑处理包含 `declareVar` 标记的字符串时,请使用 `decodeVars`。 ## 参考 ### 参数 | 名称 | 类型 | 说明 | | ----------- | -------- | ----------------------------------- | | `icuString` | `string` | 包含通过 `declareVar` 调用插入的 ICU 标记的字符串。 | ### 返回值 一个已移除 ICU 标记并保留原始变量值的字符串。 *** ## 示例 ### 基本用法 从包含变量标记的字符串中提取原始值。 ```jsx copy import { declareVar, decodeVars } from 'gt-react-native'; const encodedVar = declareVar(name); // "{_gt_, select, other {Alice}}" const decodedVar = decodeVars(encodedVar); // "Alice" ``` ### 多个变量 `decodeVars` 可用于处理多个变量。 ```jsx copy import { declareVar, decodeVars } from 'gt-react-native'; const name1 = "Alice"; const name2 = "Bob"; const encodedVar = "Hello, " + declareVar(name1) + "! My name is " + declareVar(name2); // "Hello, {_gt_, select, other {Alice}}! My name is {_gt_, select, other {Bob}}" const decodedVar = decodeVars(encodedVar); // "Hello, Alice! My name is Bob" ``` *** ## 说明 * 仅当你需要处理包含 `declareVar` 标记的字符串时,才使用 `decodeVars` * 该函数专门处理由 `declareVar` 创建的 ICU MessageFormat 模式 * 原始变量值会严格保留为编码前的状态 * 不会影响翻译处理,只会为字符串操作移除标记 * `decodeVars` 不是通用的 ICU MessageFormat 解析器,不应用于任意 ICU 字符串 ## 后续步骤 * 请参阅 [`declareVar`](/docs/react-native/api/strings/declare-var),了解如何标记动态内容 * 请参阅 [`derive`](/docs/react-native/api/strings/derive),了解如何创建静态函数调用 * 请参阅 [`msg`](/docs/react-native/api/strings/msg),了解字符串编码和解码模式