简体   繁体   中英

Removing Two Characters From A String

Related question here .

So I have a character vector with currency values that contain both dollar signs and commas. However, I want to try and remove both the commas and dollar signs in the same step.

This removes dollar signs =

d = c("$0.00", "$10,598.90", "$13,082.47")
gsub('\\$', '', d)

This removes commas =

library(stringr)
str_replace_all(c("10,0","tat,y"), fixed(c(","), "")

I'm wondering if I could remove both characters in one step.

I realize that I could just save the gsub results into a new variable, and then reapply that (or another function) on that variable. But I guess I'm wondering about a single step to do both.

Since answering in the comments is bad:

gsub('\\$|,', '', d)

replaces either $ or ( | ) , with an empty string.

take a look at ?regexp for additional special regex notation:

> gsub('[[:punct:]]', '', d)
[1] "000"     "1059890" "1308247"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM