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

Dart语言实现字符串中数字与字母间添加空格

字符串数字与字母间添加空格的解决方案

核心思路是利用正则表达式匹配字母与数字相邻的边界位置,在这些位置插入空格。以下是常用编程语言的实现示例:

Python 实现

import re

original_str = "KL11AB2432"
processed_str = re.sub(r'(?<=[A-Za-z])(?=\d)|(?<=\d)(?=[A-Za-z])', ' ', original_str)
print(processed_str)  # 输出结果: KL 11 AB 2432

JavaScript 实现

const originalStr = "KL11AB2432";
const processedStr = originalStr.replace(/(?<=[A-Za-z])(?=\d)|(?<=\d)(?=[A-Za-z])/g, ' ');
console.log(processedStr); // 输出结果: KL 11 AB 2432

正则表达式说明

  • (?<=[A-Za-z])(?=\d):匹配字母后、数字前的空白位置
  • (?<=\d)(?=[A-Za-z]):匹配数字后、字母前的空白位置
  • 用空格替换这些位置,即可实现数字与字母的分隔。

内容的提问来源于stack exchange,提问作者Fuad Saneen

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.16 22:55:48