How to strsplit using '|' character, it behaves unexpectedly?
The problem is that by default strsplit
interprets " | "
as a regular expression, in which |
has special meaning (as "or").
Use fixed
argument:
unlist(strsplit("I am | very smart", " | ", fixed=TRUE))
# [1] "I am" "very smart"
Side effect is faster computation.
stringr
alternative:
unlist(stringr::str_split("I am | very smart", fixed(" | ")))
strsplit with vertical bar (pipe)
As you can read on ?strsplit, the argument split in function strsplit is a regular expression. Hence either you need to escape the vertical bar (it is a special character)
strsplit(r,split='\\|and')
or you can choose fixed=TRUE to indicate that split is not a regular expression
strsplit(r,split='|and',fixed=TRUE)
R splitting string based on double-character delimiter
We can wrap with fixed
as |
is a metacharacter for OR
library(stringr)
str_split(myString, fixed("||"))[[1]]
#[1] "Test" "Test1" "test2"
Or another option is to escape (\\
- as @joran mentioned in the comments) or place it inside a square bracket
data
myString <- "Test||Test1||test2"
Using strsplit wid pipe separator in R
Just needs to add to backslash before the bar:
strsplit(x, "\\|")
For example:
> x <- "Hello | Could you help me please?"
> strsplit(x, "\\|")
[[1]]
[1] "Hello " " Could you help me please?"
Splitting string with pipe character ( | )
|
is a metacharacter in regex. You'd need to escape it:
String[] value_split = rat_values.split("\\|");
Split string into key-value pairs
You could do a single call to split() and a single pass on the String using the following code. But it of course assumes the String is valid in the first place:
Map<String, String> map = new HashMap<String, String>();
String test = "pet:cat::car:honda::location:Japan::food:sushi";
// split on ':' and on '::'
String[] parts = test.split("::?");
for (int i = 0; i < parts.length; i += 2) {
map.put(parts[i], parts[i + 1]);
}
for (String s : map.keySet()) {
System.out.println(s + " is " + map.get(s));
}
The above is probably a little bit more efficient than your solution, but if you find your code clearer, then keep it, because there is almost zero chance such an optimization has a significant impact on performance, unless you do that millions of times. Anyway, if it's so important, then you should measure and compare.
EDIT:
for those who wonder what ::?
means in the above code: String.split() takes a regular expression as argument. A separator is a substring that matches the regular expression. ::?
is a regular expression which means: 1 colon, followed by 0 or 1 colon. It thus allows considering ::
and :
as separators.
How to use strsplit() to access elements in an R list?
A "tidyverse" approach similar to suggestion in comments by @GavinSimpson:
library(purrr)
library(stringr)
x <- rep("stringA, stringB", 10)
str_split(x, ", ") %>% map_chr(`[`, 2)
#> [1] "stringB" "stringB" "stringB" "stringB" "stringB" "stringB" "stringB"
#> [8] "stringB" "stringB" "stringB"
str_split()
acts likestrsplit()
.map_chr()
acts likelapply()
, but also converts resulting list to a character vector.
For your problem, substitute x
for list1$field2
How do I replace all occurrences of a string in JavaScript?
In the latest versions of most popular browsers, you can use replaceAll
as shown here:
let result = "1 abc 2 abc 3".replaceAll("abc", "xyz");
// `result` is "1 xyz 2 xyz 3"
But check Can I use or another compatibility table first to make sure the browsers you're targeting have added support for it first.
For Node.js and compatibility with older/non-current browsers:
Note: Don't use the following solution in performance critical code.
As an alternative to regular expressions for a simple literal string, you could use
str = "Test abc test test abc test...".split("abc").join("");
The general pattern is
str.split(search).join(replacement)
This used to be faster in some cases than using replaceAll
and a regular expression, but that doesn't seem to be the case anymore in modern browsers.
Benchmark: https://jsben.ch/TZYzj
Conclusion:
If you have a performance-critical use case (e.g., processing hundreds of strings), use the regular expression method. But for most typical use cases, this is well worth not having to worry about special characters.
Library (stringr) doesn't work for |
str_replace_all(dts$Campaign, "[|]", "")
Splitting String based on letters case
Just do this. It works by (a) locating an upper case letter, (b) capturing it in a group and (c) replacing it with the same with a space preceding it.
gsub('([[:upper:]])', ' \\1', x)
Related Topics
How to Access Global/Outer Scope Variable from R Apply Function
Display HTML File in Shiny App
Writing Data Frame to PDF Table
How to Return 5 Topmost Values from Vector in R
Reading Big Data with Fixed Width
Deleting Every N-Th Row in a Dataframe
How to Find Difference Between Values in Two Rows in an R Dataframe Using Dplyr
Output a Good-Looking Matrix Using Rendertable()
Nested If Else Statements Over a Number of Columns
Weird Characters Added to First Column Name After Reading a Toad-Exported CSV File
Generating Multiple Plots in Ggplot by Factor
Geom_Density to Match Geom_Histogram Binwitdh
Convert Month Year to a Date in R
Importing Excel File Using Url Using Read.Xls