To access function from an library you have either load and attach it:
library(stringr) # Or require(stringr)
str_replace(x, "XXXX", "XXxxx")
or use double colon operator:
stringr::str_replace(x, "XXXX", "XXxxx")
Unfortunately :: is quite expensive so if you prefer to keep your namespace clean you should consider creating a local binding:
str_replace <- stringr::str_replace
str_replace(x, "XXXX", "XXxxx")
On a side not using internal API with ::: is probably not the best idea. Ignoring good practices it is simply far to slow to be useful in practice.
Heres the code I got but I get an error. I tried to remove any strings that contain the word "Seattle" in the column named "County" in the tibble named Population
Population %>% mutate( str_replace(Seattle, "County", ""))
Use of string_replace()
Trouble with str_replace_all
[R] arrow R package: support for stringr::str_replace_all() incomplete
Can not run the code of str_remove
Escaping the parentheses does it...
str_replace(fruit,"\\(\\)","")
# [1] "goodapple"
You may also want to consider exploring the "stringi" package, which has a similar approach to "stringr" but has more flexible functions. For instance, there is stri_replace_all_fixed, which would be useful here since your search string is a fixed pattern, not a regex pattern:
library(stringi)
stri_replace_all_fixed(fruit, "()", "")
# [1] "goodapple"
Of course, basic gsub handles this just fine too:
gsub("()", "", fruit, fixed=TRUE)
# [1] "goodapple"
The accepted answer works for your exact problem, but not for the more general problem:
my_fruits <- c("()goodapple", "(bad)apple", "(funnyapple")
str_replace(my_fruits,"\\(\\)","")
## "goodapple" "(bad)apple", "(funnyapple"
This is because the regex exactly matches a "(" followed by a ")".
Assuming you care only about bracket pairs, this is a stronger solution:
str_replace(my_fruits, "\\([^()]{0,}\\)", "")
## "goodapple" "apple" "(funnyapple"
As @mrlew pointed out,
str is result of parseInt and therefore, it's a number. replace() is a string method, so it will not work on a number.
If I understood correctly, you would like to replace a string, retrieve a code and then generate an image tag with the new string and code.
I'd go with...
<input type="text" id="text" />
<input type="button" value="See Code" onclick="myFunction();">
<input type="text" id="code" name="code">
<script>
function myFunction() {
//changed
var str = document.getElementById("text").value; //get text
var res = str.replace("ftpadress", "htmladress"); //replace
var code = parseInt(str).toString(); //get code and cast it back to a string
document.getElementById("code").value = code; //insert code
var withTag = code.concat("<img src='", res, "' width='100%'>"); //generate tag
}
</script>
Run code snippetEdit code snippet Hide Results Copy to answer Expand
parseInt returns an integer not a string so you can not use str.replace() , you need to cast it first
just add str = str.toString(); before using the replace function