Take a look at the following example:
cout << "option 1:
\n option 2:
\n option 3";
I know,it's not the best way to output a string,but the question is why does this cause an error saying that a " character is missing?There is a single string that must go to stdout but it just consists of a lot of whitespace charcters.
What about this:
string x="
string_test";
One may interpret that string as: "\nxxxxxxxxxxxxstring_test" where x is a whitespace character.
Is it a convention?
That's called multiline string literal.
You need to escape the embedded newline. Otherwise, it will not compile:
std::cout << "Hello world \
and stackoverflow";
Note: Backslashes must be immediately before the line ends as they need to escape the newline in the source.
Also you can use the fun fact "Adjacent string literals are concatenated by the compiler" for your advantage by this:
std::cout << "Hello World"
"Stack overflow";
See this for raw string literals. In C++11, we have raw string literals. They are kind of like here-text.
Syntax:
prefix(optional) R"delimiter( raw_characters )delimiter"
It allows any character sequence, except that it must not contain the
closing sequence )delimiter". It is used to avoid escaping of any
character. Anything between the delimiters becomes part of the string.
const char* s1 = R"foo(
Hello
World
)foo";
Example taken from cppreference.
The problem is that I have a string like:
public class MyFirstJavaProgram {
public static void main ( String []args ) {
System.out.println ( "Hello World" );
}
}
When I try to get a string list like:
["public";"class";"MyFirstJavaProgram";...;"(";""Hello World"";")";"...]
Im getting
["public class MyFirstJavaProgram {"; "";
" public static void main ( String []args) {";
" System.out.println("Hello World"); "; " }"; "}"]
How can i remove those white spaces.
Given the sample output in the original question, I think the closest simple solution would be:
File.ReadAllText("MyFirstJavaProgram.java").Split([|' '; '\n'|], StringSplitOptions.RemoveEmptyEntries)
|> Array.map (fun s -> s.Trim())
However, this will not treat the string "Hello World" as one entry in the array. For that, you would need to use a proper tokenization algorithm.
If you are not interested in some lines, the way to go is filter:
File.ReadAllLines(...)
|> Seq.filter (not << String.IsNullOrWhiteSpace)
Then, you can split all lines:
|> Seq.collect (fun line -> line.Split([| " " |], StringSplitOptions.RemoveEmptyEntries))
RemoveEmptyEntries will remove empty lines and leading/trailing whitespace. So filtering is not needed anymore and no trimming is neccessary.
Note that this is not a proper Java tokenizer though, e.g. main( won't be split into main and ( but "Hello World" will be split into "Hello and World".
For doing proper Java parsing, look for a library.
Note that asking for and recommending libraries is not appropriate on StackOverflow. SoftwareRecommendations might be able to help.
I've searched many times, and haven't found the answer here or elsewhere. I want to replace each space ' ' in variables containing file names with a '\ '. (A use case could be for shell commands, with the spaces escaped, so each file name doesn't appear as a list of arguments.) I have looked through the StackOverflow question "how to replace single backslash in R", and find that many combinations do work as advertised:
> gsub(" ", "\\\\", "a b")
[1] "a\\b"
> gsub(" ", "\\ ", "a b", fixed = TRUE)
[1] "a\\ b"
but try these with a single-slash version, and R ignores it:
> gsub(" ", "\\ ", "a b")
[1] "a b"
> gsub(" ", "\ ", "a b", fixed = TRUE)
[1] "a b"
For the case going in the opposite direction — removing slashes from a string, it works for two:
> gsub("\\\\", " ", "a\\b")
[1] "a b"
> gsub("\\", " ", "a\\b", fixed = TRUE)
[1] "a b"
However, for single slashes some inner perversity in R prevents me from even attempting to remove them:
> gsub("\\", " ", "a\\b")
Error in gsub("\\", " ", "a\\b") :
invalid regular expression '\', reason 'Trailing backslash'
> gsub("\", " ", "a\b", fixed = TRUE)
Error: unexpected string constant in "gsub("\", " ", ""
The 'invalid regular expression' is telling us something, but I don't see what. (Note too that the perl = True option does not help.)
Even with three back slashes R fails to notice even one:
> gsub(" ", "\\\ ", "a b")
[1] "a b"
The patter extends too! Even multiples of two work:
> gsub(" ", "\\\\\\\\", "a b")
[1] "a\\\\b"
but not odd multiples (should get '\\\ ':
> gsub(" ", "\\\\\\ ", "a b")
[1] "a\\ b"
> gsub(" ", "\\\ ", "a b", fixed = TRUE)
[1] "a\\ b"
(I would expect 3 slashes, not two.)
My two questions are:
How can my goal of replacing a ' ' with a '\ ' be accomplished?
Why did the odd number-slash variants of the replacements fail, while the even number-slash replacements worked?
For shell commands a simple work-around is to quote the file names, but part of my interest is just wanting to understand what is going on with R's regex engine.
Get ready for a face-palm, because this:
> gsub(" ", "\\\ ", "a b", fixed = TRUE)
[1] "a\\ b"
is actually working.
The two backslashes you see are just the R console's way of displaying a single backslash, which is escaped when printed to the screen.
To confirm the replacement with a single backslash is indeed working, try writing the output to a text file and inspect yourself:
f <- file("C:\\output.txt")
writeLines(gsub(" ", "\\", "a b", fixed = TRUE), f)
close(f)
In output.txt you should see the following:
a\b
Very helpful discussion! (I've been Googling the heck out of this for 2 days.)
Another way to see the difference (rather than writing to a file) is to compare the contents of the string using print and cat.
z <- gsub(" ", "\\", "a b", fixed = TRUE)
> print(z)
[1] "a\\ b"
> cat(z)
a\ b
So, by using cat instead of print we can confirm that the gsub line is doing what was intended when we're trying to add single backslashes to a string.
How do I convert a list of strings into a string in DrRacket? For example, if I have
'("44" "444") convert it into "44 444"?
I tried string-join, but it takes a delimiter and if I put one it replaces the space with the delimiter and if I use "" for the delimiter it simply gets rid of it.
In fact string-join is the right procedure for using in this case, simply use " " (a single space) as delimiter:
(string-join '("44" "444") " ")
=> "44 444"
Just to clarify: in a list the spaces between elements are not considered part of the list, they're there to separate the elements. For example, all these lists are equal and evaluate to the same value:
'("44""444")
'("44" "444")
'("44" "444")
If for some reason you want to consider the spaces as part of the list then you have to explicitly add them as elements in the list:
(define lst '("a" " " "b" " " "c" " " "d"))
(string-join lst "")
=> "a b c d"
I have a string like this:
std::string input("I #am going to# learn how #to use #boost# library#");
I do this:
std::vector<std::string> splitVector;
boost::split(splitVector, input, boost::is_any_of("#"));
And got this: (splitVector)
splitVector:
"I "
"am going to"
" learn how "
"to use "
"boos"
" library"
"" // **That's odd, why do I have an empty string here ?**
But need something like this:
splitVector:
"I "
"#am going to"
"# learn how "
"#to use "
"#boost"
"# library"
"#"
How to do that ? Or maybe there is another way to do it in boost library ?
And why do I get an empty string in splitVector ?
You cannot use boost::split because the internal implementation that uses the split_iterator from boost/algorithm/string/find_iterator.hpp swallows the tokens.
However you can get by with boost::tokenizer, as it has an option to keep the delimiters:
Whenever a delimiter is seen in the input sequence, the current token is finished, and a new token begins. The delimiters in dropped_delims do not show up as tokens in the output whereas the delimiters in kept_delims do show up as tokens. http://www.boost.org/doc/libs/1_55_0/libs/tokenizer/char_separator.htm
See next live:
#include <iostream>
#include <string>
#include <boost/tokenizer.hpp>
int main() {
// added consecutive tokens for illustration
std::string text = "I #am going to# learn how ####to use #boost# library#";
boost::char_separator<char> sep("", "#"); // specify only the kept separators
boost::tokenizer<boost::char_separator<char>> tokens(text, sep);
for (std::string t : tokens) { std::cout << "[" << t << "]" << std::endl; }
}
/* Output:
[I ]
[#]
[am going to]
[#]
[ learn how ]
[#]
[#]
[#]
[#]
[to use ]
[#]
[boost]
[#]
[ library]
[#] */