【问题标题】:How can I get the file extension from a path?如何从路径中获取文件扩展名?
【发布时间】:2022-06-23 01:57:42
【问题描述】:

如何从以下path获取extension

C:\\Users\\Nova\\Research\\data.txt # Output: .txt

我可以使用string/has-suffix?,但所做的只是检查.txt 是否存在于string 中。

【问题讨论】:

    标签: janet


    【解决方案1】:

    file api 不包含file/extension,但您可以自己编写:

    (defn get-extension
       [path]
        
        (def path_reverse (string/reverse path)) 
        (def ext_index  (string/find "." path_reverse))  
        (string/reverse (string/slice path_reverse 0 (+ 1 ext_index)))
    )
    
    (def path_ext (get-extension "C:\\Users\\Nova\\Research\\data.txt")) #Output: .txt
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-05-03
      • 1970-01-01
      • 2011-12-08
      • 2020-08-12
      • 2010-09-27
      • 2010-10-15
      相关资源
      最近更新 更多