You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Dart中去除字符串中的引号

Dart中去除字符串引号的几种实现方式

以下是几种在Dart中去除字符串首尾双引号的实用方法,可根据需求场景选择:

方法1:直接替换所有双引号

如果确定字符串中仅首尾存在双引号,无其他需要保留的引号,可使用replaceAll直接替换所有双引号:

String original = '"26f8076e-8808-4f1e-9056-589bc92bfd81"';
String processed = original.replaceAll('"', '');
print(processed); // 输出: 26f8076e-8808-4f1e-9056-589bc92bfd81

方法2:精准截取首尾引号内的内容

若需要确保只移除首尾的引号(避免误删字符串中间的引号),可先判断字符串首尾是否为双引号,再截取中间部分:

String original = '"26f8076e-8808-4f1e-9056-589bc92bfd81"';
String processed;

if (original.startsWith('"') && original.endsWith('"')) {
  processed = original.substring(1, original.length - 1);
} else {
  processed = original; // 若没有首尾引号,直接返回原字符串
}

print(processed);

方法3:使用正则表达式匹配首尾引号

通过正则表达式精准匹配字符串开头和结尾的双引号并替换,不会影响字符串中间的引号:

String original = '"26f8076e-8808-4f1e-9056-589bc92bfd81"';
RegExp quotePattern = RegExp(r'^"|"$');
String processed = original.replaceAll(quotePattern, '');
print(processed);

内容的提问来源于stack exchange,提问作者BC TUBE

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.21 15:27:21