How to include quotes in quotes c# - c#

My code here:
String text = "" + label1.Text + ""; //label1 is: C:\myfiles\download
textBox1.Text = text;
I would textbox shown after I built: (has quotes)
"C:\myfiles\download"
Please help me.
Thank you very much.
Sorry my English is bad.

In order to avoid such errors, use formatting:
textBox1.Text = String.Format("\"{0}\"", label1.Text);
and let compiler ensure that the provided string "\"{0}\"" is free from typos.

This?
String text = "\"" + label1.Text + "\""; //label1 is: C:\myfiles\download
This escapes the quotes meaning: the character after the \ has no special meaning any more and is a usual character.
Or even easier using verbatim-string:
String text = #"""" + label1.Text + #""""; //label1 is: C:\myfiles\download

Using C# 6, you can very neatly do this using the following syntax:
textBox1.Text = $"\"{label1.Text}\"";
This is shorthand for textBox1.Text = String.Format("\"{0}\"", label1.Text); and, as with String.Format, the compiler will check the validity of the string for you.

Solving your problem
You need to use backslashes. The second occurence of " will escape the string declaration. Backslashes will prevent this.
String text = "\"" + label1.Text + "\"";
textBox1.Text = text;
Now, a little refactoring
You don't need to declare a variable, except you use this value again. Futhermore, you can use string.Format(). For more information about this method, watch the link in the references section.
textBox1.Text = string.Format("\"{0}\"", label1.Text);
Some references
This Stackoverflow-question focuses on the same problem:
How to use a string with quotation marks inside it?
Here a MSDN reference: https://msdn.microsoft.com/en-us/library/aa983682(v=vs.71).aspx
MSDN reference string.Format

Double quotation mark represented by \" as an escape sequence character like;
String text = "\"" + label1.Text + \""";
Or you can double it with a verbatim string literal as;
String text = #"""" + label1.Text + #"""";
Or you can use string.Format as;
String text = string.Format("\"{0}\"", label1.Text);

Related

Is there a way to split a literal string to multiple lines with out enter to it \r

In my code i got a very long and complicated string that I want to save as a literal string
At the moment my line is 1200 character long.
And I want to separate the lines in the way which every line it wouldn't be longer than 200 characters
string comlexLine = #"{""A"": {""B"": [{""C"": {""D"": {""E"":""+"" ""/F;
I would like to separate it into shorter lines so the code would be more readable.
At the moment when I enter a new line, because it is a literal string a \r is entering to the string
for example:
string comlexLine = #"{""A"": {""B"": "
+ "[{""C"": {""D"": {""E"":""+"" ""/F;
Console.WriteLine(comlexLine);
would print:
#"{""A"": {""B"": //r "[{""C"": {""D"": {""E"":""+"" ""/F
I prefer not to split it to different constant and also to use a literal string.
Is there any solution?
Use Environment.NewLine instead for including a new line in your string. i would rather make it like below using a back slash \ to escape the extra double quotes
string comlexLine = "{\"A\": {\"B\": " + Environment.NewLine + "[{\"C\": {\"D\": {\"E\":\"+\" \"/F";
Console.WriteLine(comlexLine);
Try not using the literal and escaping the double quotes with a slash.
string comlexLine = "{\"A\": {\"B\": [{\"C\": "
+ "{\"D\": {\"E\":\"+\" \"/F";
If I use that, it doesn't introduce the //r.

Add file extension to variable

I am new to c sharp
can anybody say what the mistake
string cPict= "Picture\"+firstSelectedItem+".jpg";
where
"Picture\" = folder
firstSelectedItem = Employee Number
".jpg" = file extension
getting following error
string does not contain definition for jpg
please help
thanks in advance
The problem is that in "\"+firstSelectedItem all is treated as string, even the firstSelectedItem variable because you've used the \-character to escape the following ".
You either have to
escape the \-character by another one,
use a verbatim string literal or
use the Path-class, especially Path.Combine:
1)
string cPict = "Picture\\" + firstSelectedItem + ".jpg";
2)
string cPict = #"Picture\" + firstSelectedItem + ".jpg";
3)
string cPict = Path.Combine("Picture", firstSelectedItem + ".jpg");
You can replace it with normal slash like that:
string cPict= "Picture/"+firstSelectedItem+".jpg";
The \ is a special character that escapes the next character in a string, therefore, according to the compiler then + firstSelectedItem + is still part of the string. Your code should look like one of the following:
string cPict = #"Picture\" +
or:
string cPict = "Picture\\" +
and that should work.
you need to escape the backslash \ character
string cPict= "Picture\\"+firstSelectedItem+".jpg";
learn about Escape Sequences here
The solution is to add double slashes as below:
string cPict= "Picture\\"+firstSelectedItem+".jpg";
"Picture\\"=folder

How do I make a string of ","?

I tried:
string endtag = "\"","\"";
But that's not working. The string should be: ","
The " is also part of the string not only the ,
Try this:
string endtag = "\",\"";
To explain, the first " begins a string literal, the \" is an escape sequence for a " character, the , is a regular character, again, the \" is an escape sequence for a " character, then the final " closes the string literal.
You could also use a verbatim literal like this:
string endtag = #""",""";
Here the first " begins a string literal, and the # preceding it introduces it as a verbatim string literal. The next two "" are a special escape sequence for a " character within a verbatim string literal, the , is a regular character, again, next two "" are a special escape sequence for a " character, then the final " closes the string literal.
try string endtag = "\",\""; Alternatively you could try string endtag = #""","""; Hope that helps.
Another option:
string endtag = #""",""";
#"..." is a verbatim string literal
To use a " in this literal, it is doubled
Like so:
string endtag = "\",\"";
Try this :
String endtag = "\"" , "\"";
" is an escape sequence character.
If you want to put " in your string, you should use \" like this:
string endtag = "\",\"";
Or you can define verbatim string literal using double double quotation marks ("") like;
string endtag = #""",""";
This will work:
string endtag="\",\"";

New lines dissapear when reading .txt from Android

Doing this
String t = "asd\nasd";
TextBox objTxt = (TextBox)messageBox;
bjTxt.Text = t;
doesnt show
asd
asd
as expected, it shows
asdasd
why, its driving me crazy. TextBox is set to multiline and I can write lines in it manually. Thanks!
TextBox unlike Label and MessageBox ignores "\n" so if you want to get to the newline you will need to use "\r\n" combination. Nevertheless there is a better solution, just use Environment.NewLine and you won't need to think about \r\n combination for a newline.
Either:
String t = "asd\r\nasd";
Or:
String t = "asd" + Environment.NewLine + "asd";
The beautiful thing about Environment.NewLine is that there is no need to worry about the newline in any environment for which you are developing (or at least it should be that way).
EDIT:
I saw your comment, so I'll add few words. You could still use ReadToEnd() and if the text contains only "\n" for newline, you could do the following:
t = t.Replace("\n", "\r\n");
Or:
t = t.Replace("\n", Environment.NewLine);
since Environment.NewLine is essentially a string
Try to use Lines property of TextBox to assign all lines from file:
textBox.Lines = File.ReadAllLines(fileName);
And, as I stated in comment above, for your sample you should use Environment.NewLine for new line to appear in TextBox:
textBox.Text = "asd" + Environment.NewLine + "asd";

Substring on an html tag containing quotes

How can I properly use substring, at least, make it work ?
I want to find the position of this substring '#fff'\"> in the stringString this.style.backgroundColor='#fff'\">Choose a translation mode,
I'm doing this :
string substrToSearch = "'#fff'\\\">";
int substrPosition = stringString.IndexOf(substrToSearch);
Unfortunately, substrPosition = -1, and it is not working.
May someone help me ?
EDIT SOLVED :
I don't realy understand why I can't do this string quote = " " "; but I can do this quote = " \" " ,in fact I know that I can but.. here, the "\" is used as an escaping character, so why in the substring that I'm searching it is not interpreted as an escape character but as simple text ?
To me , string substrToSearch = "'#fff'\">";, substrToSearch is '#fff'"> as \" = ".
To be simple, why \" is not interpreted as an escape character ?
try this:
string substrToSearch = "'#fff'\">";
the stringString is not ecapsed when you access it
Here you go...
[TestMethod]
public void StringIndex
{
var stringString = "this.style.backgroundColor='#fff'\">Choose a translation mode,:";
var substrToSearch = "'#fff'\">";
var substrPosition = stringString.IndexOf(substrToSearch);
}
The answer to your added question is fundamental to the concept of string escaping. The syntax string quote = " " "; is problematic because a compiler would think that the string ended with the second quote and not the third one. String escaping allows representing an actual quotation mark with \", so the actual value of " \" " is ", not \".

Categories

Resources