dan_g8 发表于 2022-7-5 22:39:02

如何在第45位添加到字符串

您好,我有一个名为“txt\u result”的字符串,它是从文本文档生成的,我需要在字符串的第45个字符处添加一个“\\P”,以便45之后的所有字符都转到新的多行文字。。。。不太确定我需要寻找什么来做这件事。
 
谢谢

Tharwat 发表于 2022-7-5 22:50:10

试试这个功能。
 

(defun _add:new:line (str loc / lst)
;;    Author : Tharwat Al Shoufi    ;;
;;------------------------------------;;
;;    Argument :             ;;
;;   str = string            ;;
;;    loc = location            ;;
(repeat (/ (strlen str) loc)
   (setq lst (cons (strcat (substr str 1 loc) "\\P") lst)
         str (substr str (1+ loc))
   )
)
(if (and str (/= str ""))
   (setq lst (cons str lst))
)
(apply 'strcat (reverse lst))
)

 
使用上述函数的示例。
 
(_add:new:line "abcdefghijklmnopqrstuvwxyz" 5)
 
返回:
 
"abcde\\Pfghij\\Pklmno\\Ppqrst\\Puvwxy\\Pz"

dan_g8 发表于 2022-7-5 22:56:25

谢谢你的代码,效果很好。然而,我似乎有一个新问题。。。。我的绳子太长了!我用下面的方法来创建多行文字,但它把字符串一分为二!
 
(命令“mtext”pt1 pt2 txt\u result“”)
 
有没有更好的方法制作多行文字对象?

Tharwat 发表于 2022-7-5 23:06:22

使用entmake函数,该函数比一般命令更快、更准确。
 
entmake函数
 
DXF代码
 
希望有帮助

Lee Mac 发表于 2022-7-5 23:09:06

另一个:
(defun addnewline ( str pos )
   (if (< pos (strlen str))
       (strcat (substr str 1 pos) "\\P" (addnewline (substr str (1+ pos)) pos))
       str
   )
)
_$ (addnewline "abcdefghijklmnopqrstuvwxyz" 5)
"abcde\\Pfghij\\Pklmno\\Ppqrst\\Puvwxy\\Pz"

hanhphuc 发表于 2022-7-5 23:16:35

 
这也是Lisp程序很有趣的原因之一
该函数可以应用于函数本身
Thanx公司
 
@塔尔瓦祝你开斋节快乐

Tharwat 发表于 2022-7-5 23:28:01

 
你真好,韩
这个场合不属于我,但我尊重它。

MSasu 发表于 2022-7-5 23:35:09

这种技术被称为递归,并在许多编程语言中得到支持。

hanhphuc 发表于 2022-7-5 23:45:25

nvm先生,高兴编码
 
 
谢谢你的链接,先生
编程很有趣
页: [1]
查看完整版本: 如何在第45位添加到字符串