本文介绍了在Elisp中,如何获取正确插入斜杠的路径字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通过连接部分路径和目录名称来手动构建Elisp中的路径字符串。不幸的是,有时路径以斜线结束,有时不是。因此,在必要时,我需要插入斜杠,然后连接目录名称,否则不需要。这是一个好办法吗?

解决方案

(file-name-as-directory dir)将返回目录路径 dir 带有斜杠,如有必要,添加一个,否则。



如果您有序列部分路径在列表中,您可以执行以下操作:

 (let((directory-列表'(/ foobar /p / q /x / y))
(文件名some_file.el))
(concat
mapconcat'file-name-as-directory directory-list)
file-name))

/foo/bar/p/q/x/y/some_file.el

或作为替代方案,如果要在列表中包含文件名,可以使用 directory-file-name file-name-as-directory 相反:

 (let((path-list'(/ foobar /p / q / y / some_file.el)))
(mapconcat'directory-file-name路径列表/))

/foo/bar/p/q/x/y/some_file.el

(如果在非目录下使用 directory-file-name ,有人请不要使用?)


I am manually constructing path strings in Elisp by concatenating partial paths and directory names. Unfortunately sometimes the paths end with slash, sometimes not. Therefore, I need to insert slash before concatenating a directory name when necessary but not otherwise. What's a good way to do this?

解决方案

(file-name-as-directory dir) will return directory path dir with a trailing slash, adding one if necessary, and not otherwise.

If you had your sequence of partial paths in a list, you could do something like:

(let ((directory-list '("/foo" "bar/" "p/q/" "x/y"))
      (file-name "some_file.el"))
  (concat
   (mapconcat 'file-name-as-directory directory-list "")
   file-name))

"/foo/bar/p/q/x/y/some_file.el"

or as an alternative, if you wanted to include the file name in the list, you could utilise directory-file-name which does the opposite of file-name-as-directory:

(let ((path-list '("/foo" "bar/" "p/q/" "x/y/some_file.el")))
  (mapconcat 'directory-file-name path-list "/"))

"/foo/bar/p/q/x/y/some_file.el"

(Someone please correct me if using directory-file-name on a non-directory is not portable?)

这篇关于在Elisp中,如何获取正确插入斜杠的路径字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-27 03:19