明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 953|回复: 11

[源码] 英文标点符号自动转为中文标点符号

  [复制链接]
发表于 2025-3-20 19:50:07 | 显示全部楼层 |阅读模式
我已经使出了洪荒之力,来吧,展示
  1. ; 将字符串中的英文单、双引号替换为中文单、双引号
  2. (defun tt2(old new1 new2 str q)
  3.   (setq pos (vl-string-search old str))
  4.   (if (not pos) str
  5.     (progn
  6.       (cond ((= q "L")
  7.        (progn
  8.                (setq str (strcat (substr str 1 pos) new1 (substr str (+ pos (strlen old) 1))))
  9.          (setq q "R")
  10.        ))
  11.       ((= q "R")
  12.        (progn
  13.                (setq str (strcat (substr str 1 pos) new2 (substr str (+ pos (strlen old) 1))))
  14.          (setq q "L")
  15.        )))
  16.       (tt2 old new1 new2 str q)
  17.     )
  18.   )
  19. )
  20. ; 主程序
  21. (defun c:tt(/ i)
  22.   (vl-load-com)

  23.   ; 将字符串中的old替换为new
  24.   (defun tt1(new old)
  25.     (while (/= (vl-string-subst new old str1) str1)
  26.       (setq str1 (vl-string-subst new old str1))
  27.     )
  28.     str1
  29.   )
  30.   
  31.   (setq list1 '("," "。" "!" "?" ";" ":" "(" ")"))
  32.   (setq list2 '("," "." "!" "?" ";" ":" "(" ")"))
  33.   (setq i 0)
  34.   (while (setq ss (ssget ":s" '((0 . "TEXT,MTEXT"))))
  35.     (setq obj (ssname ss 0))
  36.     (setq str1 (cdr (assoc 1 (entget obj))))
  37.     (while (< i (length list1))
  38.       (tt1 (nth i list1) (nth i list2))
  39.       (setq i (1+ i))
  40.     )
  41.     (if (vl-string-search "\"" str1)
  42.       (progn (initget 1 "L R")
  43.   (setq q (getkword "\n双引号位置[左引号在前(L)/右引号在前(R)]:"))
  44.   (setq str1 (tt2 "\"" "“" "”" str1 q))
  45.   )
  46.     )
  47.     (if (vl-string-search "\'" str1)
  48.       (progn (initget 1 "L R")
  49.   (setq q (getkword "\n单引号位置[左引号在前(L)/右引号在前(R)]:"))
  50.   (setq str1 (tt2 "\'" "‘" "’" str1 q))
  51.       )
  52.     )
  53.     (entmod (subst (cons 1 str1) (assoc 1 (entget obj)) (entget obj)))
  54.   )
  55.   (princ)
  56. )


本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?注册

x

点评

字符数超过250的多行文本就不支持了。得改用Word+VBA来替换  发表于 2025-3-22 17:00

评分

参与人数 2明经币 +2 收起 理由
不一样地设计 + 1 神马都是浮云
xyp1964 + 1 赞一个!

查看全部评分

回复

使用道具 举报

发表于 2025-3-22 19:00:20 | 显示全部楼层
AutoCAD2024版本开始,中文由一个字节组成。
24版本之前,(vl-string->list "中")得到(214 208)
24版本开始得到(20013)
所以文字要替换的话,为了防止出错,先把文字内容拆开,然后单独替换,然后组合好。
以下是样例:
;中文替换为英文
(defun zw_yw (a1 / a1 opn ss1 ss2 tr1 tr2)
(if (cadr (vl-string->list "中")) (setq opn nil) (setq opn t) );判断中文是一个还是2个字节
(setq ss1 '()
       ss2 '(("φ" "%%c");直径
             ("“" "\"")
             ("”" "\"")
             ("】" "]")
             ("【" "[")
             ("(" "(")
             (")" ")")
             (";" ";")
             ("×" "x")
             (":" ":")
             ("" "%%130")
             ("" "%%131")
             ("" "%%132")
             ("" "%%133")
            )
)
(while (/= a1 "")
  (cond
   (opn
    (setq tr1 (substr a1 1 1) a1 (substr a1 2));中文与英文结合都是一个字节cad2024版本
   )
   (t
    (if (> (ascii (substr a1 1 1)) 128)
     (setq tr1 (substr a1 1 2) a1 (substr a1 3))
     (setq tr1 (substr a1 1 1) a1 (substr a1 2))
    )
   )
  )
  (if (setq tr2 (assoc tr1 ss2)) (setq tr1 (cadr tr2)) )
  (setq ss1 (cons tr1 ss1))
);while
(if (car ss1) (apply 'strcat (reverse ss1)) a1)
)

(defun c:tes ()
(setq a1 "中文()【“”】替换为英文")
(zw_yw a1);得到:"中文()[\"\"]替换为英文"
)

点评

?!应替换,“”替换成了\"  发表于 2025-3-23 09:56
AI回答:%%130:表示一级钢,符号为 “Ⅰ”。%%131:表示二级钢,符号为 “Ⅱ”。 %%132:表示三级钢,符号为 “Ⅲ”。%%133:表示四级钢(热处理),符号为 “Ⅳ”。  发表于 2025-3-23 07:37
这个是替换的啥字符:("□" "%%130") ("□" "%%131") ("□" "%%132") ("□" "%%133")  发表于 2025-3-23 07:30
回复 支持 1 反对 0

使用道具 举报

发表于 2025-3-21 15:26:33 | 显示全部楼层

谢谢楼主分享
回复 支持 反对

使用道具 举报

发表于 2025-3-21 22:11:37 | 显示全部楼层
谢谢楼主分享
回复 支持 反对

使用道具 举报

 楼主| 发表于 2025-3-22 10:59:13 | 显示全部楼层
本帖最后由 yjwht 于 2025-3-22 17:02 编辑


一段将英文标点转换为中文标点的 VBA 代码:
### 使用说明
1. 打开 Excel 工作簿,按下`Alt + F11 组合键,打开 VBA 编辑器。
2. 在 VBA 编辑器中,插入一个新的模块(插入 ->`模块)。
3. 将上述代码复制到新模块中。
4. 回到 Excel 工作表,选中你想要进行标点替换的单元格区域。
5. 在 VBA 编辑器中,按下F5 键运行“标点英转中”或“标点中转英”宏,即可完成英-中或中-英标点的替换。
————————————————————————————————————————————
  1. Sub 标点英转中()
  2.     Dim cell As Range
  3.     Dim strText As String
  4.     Dim i As Integer
  5.     Dim quoteCount As Integer
  6.    
  7.     ' 遍历当前选中的单元格区域
  8.     For Each cell In Selection
  9.         strText = cell.Value
  10.         quoteCount = 0
  11.         
  12.         ' 处理双引号
  13.         For i = 1 To Len(strText)
  14.             If Mid(strText, i, 1) = """" Then
  15.                 quoteCount = quoteCount + 1
  16.                 If quoteCount Mod 2 = 1 Then
  17.                     Mid(strText, i, 1) = "“"
  18.                 Else
  19.                     Mid(strText, i, 1) = "”"
  20.                 End If
  21.             End If
  22.         Next i
  23.         
  24.         quoteCount = 0
  25.         ' 处理单引号
  26.         For i = 1 To Len(strText)
  27.             If Mid(strText, i, 1) = "'" Then
  28.                 quoteCount = quoteCount + 1
  29.                 If quoteCount Mod 2 = 1 Then
  30.                     Mid(strText, i, 1) = "‘"
  31.                 Else
  32.                     Mid(strText, i, 1) = "’"
  33.                 End If
  34.             End If
  35.         Next i
  36.         
  37.         ' 替换其他英文标点符号为中文标点符号
  38.         strText = Replace(strText, ",", ",")
  39.         strText = Replace(strText, ".", "。")
  40.         strText = Replace(strText, "?", "?")
  41.         strText = Replace(strText, "!", "!")
  42.         strText = Replace(strText, ":", ":")
  43.         strText = Replace(strText, ";", ";")
  44.         
  45.         ' 将替换后的文本写回单元格
  46.         cell.Value = strText
  47.     Next cell
  48. End Sub

  49. Sub 标点中转英()
  50.     Dim cell As Range
  51.     Dim strText As String
  52.    
  53.     ' 遍历当前选中的单元格区域
  54.     For Each cell In Selection
  55.         strText = cell.Value
  56.         
  57.         ' 替换中文标点符号为英文标点符号
  58.         strText = Replace(strText, ",", ",")
  59.         strText = Replace(strText, "。", ".")
  60.         strText = Replace(strText, "?", "?")
  61.         strText = Replace(strText, "!", "!")
  62.         strText = Replace(strText, ":", ":")
  63.         strText = Replace(strText, ";", ";")
  64.         strText = Replace(strText, "“", """")
  65.         strText = Replace(strText, "”", """")
  66.         strText = Replace(strText, "‘", "'")
  67.         strText = Replace(strText, "’", "'")
  68.         
  69.         ' 将替换后的文本写回单元格
  70.         cell.Value = strText
  71.     Next cell
  72. End Sub

回复 支持 反对

使用道具 举报

 楼主| 发表于 2025-3-23 07:25:23 | 显示全部楼层
437271963 发表于 2025-3-22 19:00
AutoCAD2024版本开始,中文由一个字节组成。
24版本之前,(vl-string->list "中")得到(214 208)
24版本开 ...

学到一个知识点:AutoCAD2024版本开始,中文由一个字节组成
漏掉了两个该替换的字符:("】" "]")、("【" "[")
zw_yw子程序我再好好研究一下。抛砖引玉之作,感谢您的指导。
回复 支持 反对

使用道具 举报

发表于 2025-3-23 08:32:55 | 显示全部楼层
437271963 发表于 2025-3-22 19:00
AutoCAD2024版本开始,中文由一个字节组成。
24版本之前,(vl-string->list "中")得到(214 208)
24版本开 ...

这个是范例,就是中文替换的时候,要拆开内容替换才不会出错。
回复 支持 反对

使用道具 举报

 楼主| 发表于 2025-3-23 09:57:50 | 显示全部楼层
在word中使用的代码,用于选中部分的中英文标点符号互转
  1. Sub 标点英转中()
  2.     Dim rng As Range
  3.     Dim strText As String
  4.    
  5.     ' 获取当前选中的文本范围
  6.     Set rng = Selection.Range
  7.    
  8.     strText = rng.Text
  9.    
  10.     ' 替换英文标点符号为中文标点符号
  11.     strText = Replace(strText, ",", ",")
  12.     strText = Replace(strText, ".", "。")
  13.     strText = Replace(strText, "?", "?")
  14.     strText = Replace(strText, "!", "!")
  15.     strText = Replace(strText, ":", ":")
  16.     strText = Replace(strText, ";", ";")
  17.     strText = Replace(strText, "(", "(")
  18.     strText = Replace(strText, ")", ")")
  19.     ' 替换英文双引号为中文双引号
  20.     Dim firstQuote As Boolean
  21.     firstQuote = True
  22.     Dim i As Integer
  23.     For i = 1 To Len(strText)
  24.         If Mid(strText, i, 1) = """" Then
  25.             If firstQuote Then
  26.                 Mid(strText, i, 1) = "“"
  27.             Else
  28.                 Mid(strText, i, 1) = "”"
  29.             End If
  30.             firstQuote = Not firstQuote
  31.         End If
  32.     Next i
  33.     ' 替换英文单引号为中文单引号
  34.     firstQuote = True
  35.     For i = 1 To Len(strText)
  36.         If Mid(strText, i, 1) = "'" Then
  37.             If firstQuote Then
  38.                 Mid(strText, i, 1) = "‘"
  39.             Else
  40.                 Mid(strText, i, 1) = "’"
  41.             End If
  42.             firstQuote = Not firstQuote
  43.         End If
  44.     Next i
  45.    
  46.     ' 将替换后的文本写回选中范围
  47.     rng.Text = strText
  48. End Sub

  49. Sub 标点中转英()
  50.     Dim rng As Range
  51.     Dim strText As String
  52.    
  53.     ' 获取当前选中的文本范围
  54.     Set rng = Selection.Range
  55.    
  56.     strText = rng.Text
  57.    
  58.     ' 替换中文标点符号为英文标点符号
  59.     strText = Replace(strText, ",", ",")
  60.     strText = Replace(strText, "。", ".")
  61.     strText = Replace(strText, "?", "?")
  62.     strText = Replace(strText, "!", "!")
  63.     strText = Replace(strText, ":", ":")
  64.     strText = Replace(strText, ";", ";")
  65.     strText = Replace(strText, "(", "(")
  66.     strText = Replace(strText, ")", ")")
  67.     strText = Replace(strText, "“", """")
  68.     strText = Replace(strText, "”", """")
  69.     strText = Replace(strText, "‘", "'")
  70.     strText = Replace(strText, "’", "'")
  71.    
  72.     ' 将替换后的文本写回选中范围
  73.     rng.Text = strText
  74. End Sub

回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|CAD论坛|CAD教程|CAD下载|联系我们|关于明经|明经通道 ( 粤ICP备05003914号 )  
©2000-2023 明经通道 版权所有 本站代码,在未取得本站及作者授权的情况下,不得用于商业用途

GMT+8, 2025-4-21 18:02 , Processed in 0.210781 second(s), 28 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表