Splitting a file name into name,extension

前端 未结 3 1076
南笙
南笙 2020-11-29 04:32

I have the name of a file like this: name1.csv and I would like to extract two substrings of this string. One that stores the name1 in one variable

3条回答
  •  Happy的楠姐
    2020-11-29 05:12

    Use strsplit:

    R> strsplit("name1.csv", "\\.")[[1]]
    [1] "name1" "csv"  
    R> 
    

    Note that you a) need to escape the dot (as it is a metacharacter for regular expressions) and b) deal with the fact that strsplit() returns a list of which typically only the first element is of interest.

    A more general solution involves regular expressions where you can extract the matches.

    For the special case of filenames you also have:

    R> library(tools)   # unless already loaded, comes with base R
    R> file_ext("name1.csv")
    [1] "csv"
    R> 
    

    and

    R> file_path_sans_ext("name1.csv")
    [1] "name1"
    R> 
    

    as these are such a common tasks (cf basename in shell etc).

提交回复
热议问题