Is there a way to print a string as a Literal? [duplicate] - c++

This question already has answers here:
Escaping a C++ string
(5 answers)
Closed 4 years ago.
As you know we can use something like this:
string s = L("some\nstr\t");
My question is if there is a way to print a string using a literal. For example something like this:
string s = "s\nsome\n"
cout<< L(s); // the output printed should be s\nsome\n and not new lines
Thank you.

Yes,
string s = "Here is \\nan \\n example"
cout<< s;

Francois' answer is already good, but I'd like to elaborate a little more about what's happening here...
When you put \n in a string, that is a single character. The \ is saying "don't treat whatever comes next as you normally would, escape it." An escaped n is a newline, so \n is the newline character.
So if you want \ in your string, how would you get it? Normally this is treated as the escape character, but we want it to act as just a regular character. So how do we do that? We escape it! \\ will be interpreted as a single \ character.
So if you want s\nsome\n to be printed, you need to construct your string as "s\\nsome\\n". Notice that the n's aren't being escaped, the escape characters are!

Related

Ruby empty String regex [duplicate]

This question already has answers here:
What is a regex to match ONLY an empty string?
(11 answers)
Closed 2 years ago.
Is there any other way than .match(""), to specify that my String may as well contain an empty String alongside with some other stuff?
So far my regexp looks like this:
s = /(?<beginString>[\sA-Za-z\.]*)(?<marker1>[\*\_]+)(?<word>[\sA-Za-z]+)(?<marker2>[\*\_]+)(?<restString>[\sA-Za-z\*\_\.]*)/ =~ myString
now I want to assign beginString an empty String, because the program runs recursively. I want to be able to pass this function a String like "*this* _is_ ***a*** _string_" so as you can see, there is plenty possibility that beginString has to be empty in order that it reads the next marker.
In the end I want my program to print this:
{"01"=>#<Textemph #content="this ">, "02"=>#<Textemph #content="is">,
"11"=>#<Textstrongemph #content="a">, "12"=>#<Textemph #content="string">}
I think that it might fix my problem if I could just put somthing like \nil up there in the [ ].
Please just tell me how to add .match("") but in //-form.
Thank you for your Help <3
Match Start-of-String Followed by End-of-String
To match an empty string in Ruby, you could use the \A and \z atoms to match the beginning of the string immediately followed by the end of the string. For example:
"".match /\A\z/
#=> #<MatchData "">
Note that this is different from /^$/, which doesn't handle newlines as you might expect. Consider:
"\n".match /\A\z/
#=> nil
"\n".match /^$/
#=> #<MatchData "">
To properly detect an empty string, you need to use start/end of string matchers rather than start/end of line.

Detecting Quotes in a String C++ [duplicate]

This question already has answers here:
How can I get double quotes into a string literal?
(2 answers)
Closed 2 years ago.
I am having issues detecting double quotes ("") or quotation marks in general from a string.
I tried using the str.find(""") or str.find("""") however, the first one doesn't compile and the second does not find the location. It returns 0. I have to read a string from a file, for example:
testFile.txt
This is the test "string" written in the file.
I would read the string using and search it
string str;
size_t x;
getline(inFile, str);
x = str.find("""");
however the value returned is 0. Is there another way to find the quotation marks that enclose 'string'?
The string """" doesn't contain any quotes. It is actually an empty string: when two string literals are next to each other, they get merged into just one string literal. For example "hello " "world" is equivalent to "hello world".
To embed quotes into a string you can do one of the following:
Escape the quotes you want to have inside your string, e.g. "\"\"".
Use a raw character string, e.g. R"("")".
You should use backslash to protect your quotes.
string a = str.find("\"")
will find ".
The " character is a special one, used to mark the beginning and end of a string literal.
String literals have the unusual property that consecutive ones are concatenated in translation phase 6, so for example the sequence "Hello" "World" is identical to "HelloWorld". This also means that """" is identical to "" "" is identical to "" - it's just a long way to write the empty string.
The documentation on string literals does say they can contain both unescaped and escaped characters. An escape is a special character that suppresses the special meaning of the next character. For example
\"
means "really just a double quote, not with the special meaning that it begins or ends a string literal".
So, you can write
"\""
for a string consisting of a single double quote.
You can also use a character literal since you only want one character anyway, with the 4th overload of std::string::find.

How to replace '\' with '/' using regex in Python [duplicate]

This question already has answers here:
How to convert back-slashes to forward-slashes?
(9 answers)
Closed 4 years ago.
I tried this but did not work.
import re
s = "Ex\am\ple String"
replaced = re.sub("\", "/", s)
print replaced
Any suggestions?
Just use this code:
s = s.replace("\\", "/")
It replaces all the backslashes with the forward slash. The reason for the two backslashes is because you have to escape the other backslash.
Try it like this with \\\\:
import re
s = "Ex\am\ple String"
replaced = re.sub("\\\\", "/", s)
print replaced
You likely aren't using enough backslashes.
Backslash is often an escape character. Try commenting here using one \, then two \, you'll notice you only get one in the output here.
code version of what I just typed to show what I mean:
Backslash is often an escape character. Try commenting here using one \,
then two \\, you'll notice you only get one in the output here.
I wrote a similar code in R for when I cut and paste file locations
fileLoc = function(){
Rlocation = gsub( "\\\\","/",readClipboard())
return(Rlocation)
}
Notice it required 4 backslashes.
Try the following:
s = "Ex\am\ple String"
replaced = re.sub("\\\\", "/", s)
You have two problems: First, your string is not what you think it is, because '\a' is interpreted as an escape literal. To fix this, use a raw or regex string,
s = r"Ex\am\ple String"
Now you can do what you want using
replaced = re.sub(r"\\", "/", s)
Here, two \ are needed because re expect a single \ to be an escape character. Also, the r"\\" needs to be a raw string because Python more generally also interprets "\" as an escape character. To "escape the escape character" itself, you can do the double \ trick again, and so another way of doing it would be
replaced = re.sub("\\\\", "/", s)
Better method
Escaping and the re module aside, for simple replacements like this, you should use the str replace method,
replaced = s.replace("\\", "/")
("only" two \ needed because we are only using "bare" Python, not also re)

Print on the screen the symbol \ as text [duplicate]

This question already has answers here:
using \ in a string as literal instead of an escape
(2 answers)
Closed 7 years ago.
I want the following printout to appear in the my screen when I run my code:
\begin{tabular}
\hline \\
For that, I am using the following command on my code:
std::cout<<"\begin{tabular}<< std::endl;
std::cout<<"\hline \\"<< std::endl;
And I'm getting the following compiler message (regarding the second line of code):
unknown escape sequence: '\h'
and the following incomplete printout:
egin{tabular}
hline\
Where in the first one the "\b" is missing and the first and last \ are missing for the second sentence.
The question is: does anyone know how I can print the \ symbol as text, such that it will get printed and not be interpreted as a command, etc?
The backslash forms escape sequences in C++. You have two options:
Double all the backslashes, a la "\\hline \\\\" (the backslash will escape itself, just as in TeX).
Use C++11 raw strings, which look like R"(\hline \\)" (the text is enclosed by the parens inside the quotes).
Just escape it wiht '\'. So if you want to print out '\' character you must do:
cout<<'\\'<<endl;
Use double backslash (\) if you would like to print \ as a character in your output, otherwise, single \ followed by some character has inherent meaning of some special character, e.g. \n for newline, \r for carriage return \t for tab etc
Double all the backslashes.
e.g.
std::cout<<"\\hline \\\\"<< std::endl;
backslash is an escape code in C++.
As in, below, the "\"escape sequence and n is escape code. Which means a newline character.
"hello\n"
so if you want to print \ as well, you need to escape it too.
"hello\\hi"

gsub("BLAH", "", "BLAH\WHAT") won't let x have a backslash? [duplicate]

This question already has answers here:
How to escape a backslash in R? [duplicate]
(2 answers)
Closed 8 years ago.
I'm doing some batch string clean up and a lot of the entries look like this:
"ABC\Company Co."
Which causes weird errors, and I can't seem to remove the backslash.
For example, try entering this into your console:
gsub("BLAH", "", "BLAH\WHAT")
and you get:
Error: '\W' is an unrecognized escape in character string starting ""BLAH\W"
I know that it's thinking \W is a command.. I'm actually suprised that gsub's 'interpreting' x, since x is just the string I want to sub out. I don't get why gsub cares what's actually in x, just that it should replace "BLAH" with "" within "BLAH\WHAT"...
The obvious solution would be to remove the \ from the string ahead of time.
gsub("\\", "", "BLAH\WHAT")
But then you get the exact same error message!
Thoughts? Thanks!
Use
gsub("\\\\", "", "BLAH\\WHAT")
which gives
[1] "BLAHWHAT"
To produce one backslash, you need to escape it using a \. Thus, "\\\\" produces two backslashes, which matches the two inside "BLAH\\WHAT".
See these related questions:
How to escape a backslash in R?
How to escape backslashes in R string