Reputation: 3594
this is going to be silly.
I have a string like:
word <- "dirtyboards.csv"
I want to remove the csv part and get "dirtyboards".
I am trying:
require(stringr)
str_extract(word, ".*[^.csv]")
I get in return: "dirtyboard" . The "s" before the ".csv" goes missing.
I know I can do ,
gsub(".csv", "", word)
Upvotes: 2
Views: 2405
Reputation: 28441
If your situation is like it is presented, you can try:
substr(word, 1, nchar(word)-4)
[1] "dirtyboards"
This code starts from the first character and captures the rest of the string up to the last four tokens. The solution wholly depends on the application involved.
Upvotes: 1
Reputation: 887108
Try
library(stringr)
str_extract(word, '.*(?=\\.csv)')
#[1] "dirtyboards"
Another option which works for the example provided (and not very specific)
str_extract(word, '^[^.]+')
#[1] "dirtyboards"
Including 'foo.csv.csv',
word1 <- c("dirtyboards.csv" , "boardcsv.csv", "foo.csv.csv")
str_extract(word1, '.*(?=\\.csv$)')
#[1] "dirtyboards" "boardcsv" "foo.csv"
Upvotes: 9
Reputation: 109864
Base R has an ap for that:
word <- "dirtyboards.csv"
tools::file_path_sans_ext(word)
## [1] "dirtyboards"
Upvotes: 9
Reputation: 3194
word <- c("dirtyboards.csv","boardcsv.csv")
sub(".csv$","",word)
[1] "dirtyboards" "boardcsv"
Upvotes: 1