I want to display sth like
string test1 = "test2";
so i would like this to be displayed in a richtextbox, but i don't know how to add " in a string output, i have everything working except the displaying of ""; and its C# :) so it should look like that:
string test2 = "test2";
richTextBox.Text = "string test1 = " + (the ") + test2 + (the ") + ";";
You can use backslashes (\) to "escape" a quotation mark. So this string is actually ":
string s = "\"";
Got it? If you still don't, just remember that when ever you want to write a quotation in a string, write \".
So your text would be something like this
richTextBox.Text = "string test1 = \"test2\";";
See? The outer most two quotes denote a string literal and the inner two quotes actually denote the actual quotes in the string. Also, don't forget the semicolon at the end. (You will be likely to forget this because you saw the semi colon in the string literal. But remember! The semicolon is in the string, it's "fake"!)
You have to escape the character " in your string. In C# in many other languages the escape key is \
So your final string will be :
richTextBox.Text = "string test1 = \"" + test2 + "\";";
You can do this by escaping the " like so \":
richTextBox.Text = "string test1 = \"test2\";"
Which will display:
string test1 = "test2";
Related
How to concatenate double quotes with slashes to a string value?
Expected out put: "\"Hello World\""
Here is my code:
string term="Hello World";
string output = "\"" + term + "\"";
above code result is giving Hello World
Does this help?
string output = "\"\\\"" + term + "\\\"\"";
Live Demo
string output = "\"\\\"" + term + "\\\"\"";
To output quotes and slashes you should use slash before symbols(slash, quotes etc.)
In your case
string term = "Hello World";
string output = "\\\"" + term + "\\\"";
First slash is used to output second slash and third slash used to output quotes.
I don't know whether I should consider first and last quotes in your question or not. If you want to output them just add
\"
You must escape every char, that you want to display.
\"Hello World\":
string output = "\\\"" + term + "\\\"";
"\"Hello World\"":
string output = "\"\\\"" + term + "\\\"\"";
Sure you could add the escape characters yourself, but what if you want to escape values inside the string too? You could write that loop yourself easily enough, or;
public string Escape(string value)
{
using (var writer = new StringWriter())
{
using (var provider = CodeDomProvider.CreateProvider("CSharp"))
{
provider.GenerateCodeFromExpression(new CodePrimitiveExpression(value), writer, null);
return writer.ToString();
}
}
}
String myStr = "hai, 'how,are','you.','Local's'";
String[] valueList = Regex.Split(myStr , ",(?=(?:[^\']*\'[^\']*\')*(?![^\']*\')");
Without single quote after "l" in 'Locals' the above regex works.
But how do I escape the single Quote in 'Local's'?
I'd do some data cleanup before trying to parse it:
myStr = Regex.Replace(myStr, #",\s+'", ",\"");
myStr = Regex.Replace(myStr, #"'\s+,", "\",");
if(myStr.StartsWith("'"))
myStr = '"' + myStr.Substring(1);
if(myStr.EndsWith("'"))
myStr = myStr.Remove(myStr.Length - 1) + '"';
This, or something like it - maybe you'll have more crazy data that defeats it and it'll need improving - will convert your string to a more typical hai,"how,are","you.","Local's"
Then run it through some popular csv library
I have a string variable such as this:
string title = string.empty;
I have to display the content of whatever is passed to it inside a div within double quotes. I have written something like this:
...
...
<div>"+ title +#"</div>
...
...
How can I add the double quotes here? So that it will display like:
"How to add double quotes"
You need to escape them by doubling them (verbatim string literal):
string str = #"""How to add doublequotes""";
Or with a normal string literal you escape them with a \:
string str = "\"How to add doublequotes\"";
So you are essentially asking how to store doublequotes within a string variable? Two solutions for that:
var string1 = #"""inside quotes""";
var string2 = "\"inside quotes\"";
In order to perhaps make it a bit more clear what happens:
var string1 = #"before ""inside"" after";
var string2 = "before \"inside\" after";
If you have to do this often and you would like this to be cleaner in code, you might like to have an extension method for this.
This is really obvious code, but still I think it can be useful to grab it and make you save time.
/// <summary>
/// Put a string between double quotes.
/// </summary>
/// <param name="value">Value to be put between double quotes ex: foo</param>
/// <returns>double quoted string ex: "foo"</returns>
public static string AddDoubleQuotes(this string value)
{
return "\"" + value + "\"";
}
Then you may call foo.AddDoubleQuotes() or "foo".AddDoubleQuotes(), on every string you like.
If I understand your question properly, maybe you can try this:
string title = string.Format("<div>\"{0}\"</div>", "some text");
You can also include the double quotes into single quotes.
string str = '"' + "How to add doublequotes" + '"';
Another note:
string path = #"H:\\MOVIES\\Battel SHIP\\done-battleship-cd1.avi";
string hh = string.Format("\"{0}\"", path);
Process.Start(#"C:\Program Files (x86)\VideoLAN\VLC\vlc.exe ", hh + " ,--play");
The real value of hh, as passed, will be "H:\MOVIES\Battel SHIP\done-battleship-cd1.avi".
When needing double double literals, use: #"H:\MOVIES\Battel SHIP\done-battleship-cd1.avi";
Instead of: #"H:\MOVIESBattel SHIP\done-battleship-cd1.avi";
Because the first literals is for the path name and then the second literals are for the double quotation marks.
Using string interpolation with a working example:
var title = "Title between quotes";
var string1 = $#"<div>""{title}""</div>"; // Note the order of the $#
Console.WriteLine (string1);
Output
<div>"Title between quotes"</div>
If you want to add double quotes to a string that contains dynamic values also. For the same in place of CodeId[i] and CodeName[i] you can put your dynamic values.
data = "Test ID=" + "\"" + CodeId[i] + "\"" + " Name=" + "\"" + CodeName[i] + "\"" + " Type=\"Test\";
You can use $.
Interpolated Strings:
Used to construct strings. An interpolated string looks like a template string that contains interpolated expressions. An interpolated string returns a string that replaces the interpolated expressions that it contains with their string representations. This feature is available in C# 6 and later versions.
string commentor = $"<span class=\"fa fa-plus\" aria-hidden=\"true\"> {variable}</span>";
You could use " instead of ". It will be displayed correctly by the browser.
Use either
&dquo;
<div>&dquo;"+ title +#"&dquo;</div>
or escape the double quote:
\"
<div>\""+ title +#"\"</div>
In C# you can use:
string1 = #"Your ""Text"" Here";
string2 = "Your \"Text\" Here";
string doubleQuotedPath = string.Format(#"""{0}""",path);
An indirect, but simple to understand alternative to add quotes at start and end of string -
char quote = '"';
string modifiedString = quote + "Original String" + quote;
Put a backslash (\) before the double quotes. That should work.
"<i class=\"fa fa-check-circle\"></i>" is used with ternary operator with Eval() data binding:
Text = '<%# Eval("bqtStatus").ToString()=="Verified" ?
Convert.ToString("<i class=\"fa fa-check-circle\"></i>") :
Convert.ToString("<i class=\"fa fa-info-circle\"></i>"
In C#, if we use "" it means that it will indicate the following symbol is not a C# built-in symbol that will used by the developer.
So in a string we need double quotes, meaning we can put "" symbol before double quotes:
string s = "\"Hi\""
With C# 11.0 preview you can use Raw String Literals.
Raw string literals are a new format for string literals. Raw string literals can contain arbitrary text, including whitespace, new lines, embedded quotes, and other special characters without requiring escape sequences. A raw string literal starts with at least three double-quote (""") characters. It ends with the same number of double-quote characters. Typically, a raw string literal uses three double quotes on a single line to start the string, and three double quotes on a separate line to end the string.
string str = """
"How to add double quotes"
""";
Now with C# 11
var string1 = """before "inside" after""";
var string2 = """ "How to add double quotes" """;
If you want to add double quotes in HTML
echo "<p>Congratulations, “" . $variable . "”!</p>";
Output
Congratulations, "Mr John"!
I am trying to replace double quotes with space but it's not working, says syntax error,
"'asd"asd', 'asdasda"sdsa'" // This is the string pattern I am getting "somehow"
string b = a.Replace(""", " "));
You have to escape the " with a backslash:
string b = a.Replace("\"", " ");
where string a is bla bla " bla " bla bla
You need to escape the qoute. Given you have had quotes, this is what will happen
string a = "\"asdasd\", \"asdasdasdsa\"";
string b = a.Replace("\"", " "); // b = " asdasd , asdasdasdsa "
Firstly,
I think you are declaring the string a in a wrong way.
Normally in a declaration of a string, there should be a double quote in each end, like string a = "...", and they won't be counted in the content of the string. If you want to include a double quote in the string, you should put a slash ahead of it, like: string a = "I said, \"Thanks.\"".
If you print a, you will get
I said, "Thanks."
Secondly, the same problem.
string b = a.Replace("\"", " "));
I want to remove a backslash character from this string:
String result = "[{\"venues\":{\"venueId\":1,\"name\":\"First Venue\","
+ "\"telephone\":\"jkljl\",\"description\":\"Edited Description\","
+ "\"address\":\"jlkjlj\",\"city\":\"lkjl\",\"postcode\":\"M221TX\","
+ "\"image\":z\"abcImage007.jpg\",\"latitude\":53.37655,\"longitude\":-2.27418,\"deleted\":0,"
+ "\"events\":[{\"eventId\":3,\"name\":\"Test Event\",\"description\":\"Test Event Description\",\"date\":\"24/07/2011\",\"startTime\":\"11:11\",\"venueId\":0,\"deleted\":1},"
+ "{\"eventId\":3,\"name\":\"Test Event\",\"description\":\"Test Event Description\",\"date\":\"25/07/2011\",\"startTime\":\"11:11\",\"venueId\":0,\"deleted\":1}]}}]";
I have tried:
String abc = result.Replace(#"\",#"");
String abc = result.Replace(#"\",string.Empty);
String abc = result.Replace(#"\\",#"");
String abc = result.Replace(#"\\",string.Empty);
But nothing is working. Could someone help please.
Thanks
Your string doesn't contain \
you dont need to remove them. \" is escape sequence that shows that in your string is " symbol(quotation mark)
More fully:
Your string doesn't contain the \ character. In the variable declaration it is used to escape the " character so that it can be put into the string without causing the end of the string to occur.
If write out the value of the variable somewhere you'll find there are no \ characters
test this code
String abc = result[0].Replace(#"\",#"");
String abc = result[0].Replace(#"\",string.Empty);
String abc = result[0].Replace(#"\\",#"");
String abc = result[0].Replace(#"\\",string.Empty).