In my current project, the user writes a file path (Example: "C:\Data") into a Textbox. Then I read it with:
string PathInput = tbPath.Text;
And then send it into an SQL Insert Query.
If I then read the data from SQL, I get back: C:Data
So I tried to do:
string Path = PathInput.Replace(#"\", "\\");
So that it would double the \\, because when I enter C:\\Data I get C:\Data. But it looks like the \ get lost in Textbox and not in Database.
So, how can I read the TextBox without losing the \s?
Your replace doesn't actually replace anything:
PathInput.Replace(#"\", "\\");
Since you use an # before the first string, you don't have to escape anything. But in the second string, you don't use #, meaning you have to escape characters in that string - that means you're replacing the \ with another \.
Change it to:
PathInput.Replace(#"\", #"\\");
Related
I'm working on application where my goal is to import a data from .txt file and store it in database.
One row in that file looks like this: .txt file for import
Lets take a look at "Papua New Guinea" which I marked with red square in the previous image.
So after importing this file using IFormFile I get something like this: List of items in code
My plan is to store this values to database, but I am having redudant characters as can be seen in previous picture "\"Papua New Guinea\"".
How can I remove those redudant characters? Having in mind that not every item will have those redudant (\") characters (2nd image you can see some integer values)
The \ is used as escape character to show you that the following " does not mark the end of the string but is that it is part of it. The escape character is not part of the string. I.e., in "\"Papua New Guinea\"", the string begins with the characters ", P, a and ends with e, a, ".
You can use an overload of Trim accepting characters to be trimmed from the beginning and the end of the string.
string s = "\"Papua New Guinea\"";
string trimmed = s.Trim('"');
Note that since character literals are delimited with single quotes, you can specify the double quote character without escape character.
Those slashes wont' actually show up if you try to print or do anything with the string. They're just there because strings start with a " and \" is a way of adding a quotation mark to a string. Just like \n adds a new line. The integers don't have them because they don't include a quotation mark.
I am trying to open a pdf file that is located on the network :
I call the file like this in c# (Sorry for sending my code as a picture because of breakpoint i have to)
But it can't find the path .Another thing that i should add is when i call the file outside the c# like this \\127.0.0.1\dccfile\test\dcc1\1.pdf it works .
The value you're looking at in the debugger tooltip is a C# literal, not a string. C# literals delimit strings with straight quotes " and escapes metacharacters with backslashes \. See the quotes at the start and end of the literal in the tooltip? They're not part of the string. Backslashes are C# metacharacters, to include one in a string you have to precede it with another backslash. The C# literal "\\" encodes a string containing a single backslash character. The first \ you see in "\\127.0.0... is a metacharacter telling C# that the next character is a literal backslash, not a metacharacter. The code "\\127.0.0.1\\DCCFile\\test\\dcc1\\1.pdf" you see in the tooltip encodes the C# string \127.0.0.1\DCCFile\test\dcc1\1.pdf with no quotes and single backslashes.
Your problem is the value of Configuration.AccountDetail.DCCFileAddress needs to start with two backslashes and it does not.
Your code pathString.Replace(#"\\", #"\") will have no effect because there are no double backslashes in your string; the debugger is displaying the backslashes doubled so you know they are literal backslashes and not metacharacters.
Hey I have an issue with Regex.Escape I'm trying to feed it an Email from TextBox Controll. The function recieves "test#test.test". What I expect to get is this "test#test\.test" Regex.Escape escapes the dot character. Hovever what I get instead is "test#test\\.test" which is very confusing. I plan on handing that string down to an SQL query and I'm worried abut users misbehaving.
holder.address = Regex.Escape(EmailAddressInput.Text);
This is how I assign resulting string to field in holder class.
I have been researching this problem on my own but most sources (including MSDN) suggest to prefix the dot ("the special character") with one backslash.
As it is right now backslash escapes backslash and result is a badly formatted email address.
var s = "test#test\\.test"; means the s holds the test#test\.test string. Your issue does not exist. There is a single backslash. Click the magnifier button on the right - you will see that in the Text Visualizer.
Regex has to have \\ because its escaping the \
the string itself actually only has one \ in it.
I have a StringBuilder object and wanted to used its Append() method to add this whole string to it:
so I used "#" and copy pasted that whole string like this, but it gives a lot of errors such as "; expected ", "Invalid Expression '<'" , etc
myString.Append(#"COPY-PASTED-THAT_WHOLE-STRING");
What is the correct way of adding this string to my string builder object?
Thank you.
Even with an # prefixing the string, you need to escape any " characters, otherwise they will be interpreted as the end of the string literal.
EDIT:
e.g.
var entity = #"<!ENTITY xsd ""http://www.w3.org/2001/XMLSchema#"">";
Double-quotes (") inside the string you want to paste need to be escaped by being replaced with two consecutive double-quotes, as in "". Here's a trick to use:
Paste your string into a new instance of Notepad
Replace all double quotes (") with two double quotes ("")
Select and copy the content from Notepad back into clipboard
Paste it into #"…" in your code/text editor
From C# docs:
In a verbatim string literal, the characters between the delimiters
are interpreted verbatim, the only exception being a
quote-escape-sequence.
You can use the # syntax to add multiple lines. But you need to escape the "s inside your string by using ""
For example
#"<Ontology xmlns=""http://www.w3.org/2002/07/owl#"""
If you don't escape them, C# will treat the quote mark as the end of the string.
One option, as others have said, is to escape all of the double quotes (") with a double double quote ("").
What I prefer to do, as it makes the code more readable, when adding an XML block as a literal string, is to use single quotes rather than double quotes. Just put the XML file into a text editor and do a replace all on double quote with a single quote (').
Another option, since your XML literal isn't all that short, is to put it into a file and read in that file at runtime.
You can escape them like this as well...
#"<Ontology xmlns=\"http://www.w3.org/2002/07/owl#\""
I am just curious about the title.
String a = "abc\ndef";
Console.Writeline(a);
The output is
abc
def
Then I stored that value into an ini file and retrieved it from there.
ini.iniwritevalue("a", "a", a);
string b = ini.inireadvalue("a", "a");
Then I showed it on the console. The result is the following:
abc\ndef
Why is \n not working after I retrieved it from the ini file?
P.S. I have a ini.dll file. Our company is using that dll to read and write ini files.
The interpretation of the \n escape code in your source code is done by the compiler when parsing the source file.
If you just read in a file as "data" at runtime, no such interpretation is necessarily going to occur.
You may need to find or write a function which takes a string containing escape sequences and converts them to binary values (\n usually becomes 0x0a)
This is because \n in C# is not just a \ and an n, but an escape sequence with a special meaning. \n is considered a single character and is a line ending. You will not get it when you simply read a \ and an n from a file.
Possibly, you read \\n from there. \\ is also an escape sequence which means the \ character. All you have to do is replace \\n with \n, and it'll be okay.
string s = ... //get the value
s = s.Replace("\\n", "\n");
You need to escape the slash when you write the value, like this:
abc\\ndef