I know this has been covered lots of times but I still have a problem with all of the solutions.
I need to build a string to send to a JSON parser which needs quotes in it. I've tried these forms:
string t1 = "[{\"TS\"}]";
string t2 = "[{" + "\"" + "TS" + "\"" + "}]";
string t3 = #"[{""TS""}]";
Debug.Print(t1);
Debug.Print(t1);
Debug.Print(t1);
The debug statement shows it correctly [{"TS"}] but when I look at it in the debugger and most importantly when I send the string to my server side json parser is has the escape character in it:
"[{\"TS\"}]"
How can I get rid of the escape characters in the actual string?
The debug statement shows it correctly [{"TS"}] but when I look at it
in the debugger and most importantly when I send the string to my
server side json parser is has the escape character in it:
"[{\"TS\"}]"
From the debugger point of view it will always show the escaped version (this is so you, as the developer, know exactly what the string value is). This is not an error. When you send it to another .Net system, it will again show the escaped version from the debugger point of view. If you output the value, (Response.Write() or Console.WriteLine()) you will see that the version you expect will be there.
If you highlight the variable (from the debugger) and select the dropdown next to the magnifying glass icon and select "Text Visualizer" you will see how it displays in plain text. This may be what you are looking for.
Per your comments, i wanted to suggest that you also watch how you convert your string in to bytes. You want to make sure you encode your bytes in a format that can be understood by other machines. Make sure you convert your string into bytes using a command as follows:
System.Text.Encoding.ASCII.GetBytes(mystring);
I have the sneaking suspicion that you are sending the bit representation of the string itself instead of an encoded version.
Related
I'm completely a junior here. I have tried something like
save a path and file string in a file like:
c:\aaa\bbb\text.txt
then I need to read again as path but I get c:\aaa\bbb\text.txt from streamreader, but I need c:\\\aaa\\\bbb\\\text.txt
Can anyone help me?
I think you might be confusing string literals with a string.
Say I write var myString = "\\" or var myString = #"\", this will show in the debugger as \\, because the debugger will format it as a literal. But if print it to the console, a file, or press the magnifying glass next to the string in the debugger, it will be shown as \, because that is the actual string value. See also verbatim string literal
So, if you do myStreamWriter.Write("c:\\aaa\\bbb\\text.txt");, you will be actually saving the string c:\aaa\bbb\text.txt, and that is also the string that will be read back.
However I fail to understand why you would want three slashes, I can only assume the OP thinks the escaping is done multiple times.
I´m using a .NET core 3.0 project on Windows 10. I´m trying to encode a string to base64 with below code:
var stringvalue = "Row1" + Environment.NewLine + "\n\n" + "Row2";
var encodedString = Convert.ToBase64String(Encoding.UTF8.GetBytes(stringvalue));
encodedString has then below result:
Um93MQ0KCgpSb3cy
stringvalue is:
Row1\r\n\n\nRow2
However, if I´m passing the same value to this site (https://www.base64encode.org/), i´m getting another result:
Um93MVxyXG5cblxuUm93Mg==
In visual studio, I tried to resave the file with Unix lineendings, but without any luck:
I want the string to be encoded as how it´s done in https://www.base64encode.org. Any ideas how to get this done?
From the screenshot, I can see that you have entered a different string from the string you used in your C# code. The string you used in https://www.base64encode.org is represented as a C# string literal like this:
"Row1\\r\\n\n\\nRow2"
// or
#"Row1\r\n\n\nRow2"
So to answer your question:
I want the string to be encoded as how it´s done in https://www.base64encode.org. Any ideas how to get this done?
You should do:
var encodedString = Convert.ToBase64String(Encoding.UTF8.GetBytes("Row1\\r\\n\n\\nRow2"));
But that's probably not what you actually want. Your first attempt at the C# code is more likely to be desired, because that is actually a carriage return character, followed by 3 new line characters. The string you entered in https://www.base64encode.org is simply the backslash character followed by the letter r (or n).
You can't really make the output on https://www.base64encode.org match the C# output, because you can only choose one kind of line separator on there. You can only either encode Row1\r\n\r\n\r\nRow2 or Row\n\n\nRow2. Nevertheless, you can check that the C# result is correct by decoding the output using https://www.base64decode.org.
The \r\n will be encoded on the website, this is not a newline, these are 4 characters. There is this newline-separator-checkbox, to say you want the windows style, to convert your real world input value:
Row1
Row2.
I guess your \r\n\n\n is just a mistake, the website is prepared to convert it to \r\n\r\n only.
Below I have a regex statement I have been working on for quite awhile. The problem I am having is that their are a lot of quotations I am trying to parse out (I think that's the terminology I am looking for) so Visual Studio is freaking out about it. I have tried to fix this using escape characters, but it still won't recognize the whole phrase.
Here is the phrase without the escape characters:
string exceptionPattern = #"(?:(?:"([^"}]*)")|(\w+))\s*:\s*(?:(?:"([^"}]*)")|(\w+))";
With just this code in, nothing else, almost every line in my code gets affected. Here is the code using escape characters:
string exceptionPattern = #"(?:(?:\"([^\"}]*)\")|(\w+))\s*:\s*(?:(?:\"([^\"}]*)\")|(\w+))";
Once this comes into play, only this line is not working. In VS, the ([^\ part close to the beginning is not highlighted, meaning that it is not in quotes. Does anyone have any idea on how I can fix this problem?
This is the string I am trying to match. Note: THIS IS NOT JSON! I have confirmed it many times with the developer who made the database where this sample is coming from and he confirmed it is not JSON, so please do not try to use JSON on this. Also, the regex I have is trying to match displayException and the message after it, and also exception and the message after that including success false, using the quotes as a point of splitting
{"data":"","displayException":"Invalid Account Status. Please complete the registration process by clicking the verification link in your eTTek Dash Registration Verification email. Please contact 1-800-341-6184 M-F 9a-5pm CT for further assistance.","exception":"UNABLE TO LOGIN","success":false}
Inside a verbatim string, to escape a double quotes you must need to add another double double quotes near to that like "". So the compiler treats "" as a double quotes or otherwise it would treat " as an end of the verbatim string.
I want to write something like this
string input = "<form action=\"http://blabla.com\" method=\"post\">...</form>";
but the backslash() where .com ends merge with link... what i can do?
So link becoming http://blabla.com\
This might be more readable with strings that contain both slashes and double quotes. It is for me.
string input = #"<form action=""http://blabla.com"" method=""post"">...</form>";
Also note that a single quote is acceptable in HTML, so this should work too:
string input = #"<form action='http://blabla.com' method='post'>...</form>";
Here's some additional info on literals in C#
http://msdn.microsoft.com/en-us/library/aa691090(v=vs.71).aspx
This is only visually inside the Visual Studio Editor. When running, your program should work fine.
I want to add double quotes for a sting . I know by using /" we can add double quotes . My string is
string scrip = "$(function () {$(\"[src='" + names[i, 0] + "']\"" + ").pinit();});";
When i do this on the browser i am getting " instead of " quotes . How can i overcome with the problem ?
If your browser has displayed a """ instead of a " character, than there are only a few causes possible. The character should have been emitted to the browser as either itself, or as a HTML entity of ". Please note the semicolor at the end. If a browser sees such 'code', it presents a quote. This is to allow writing the HTML easier, when its attribtues need to contain special characters, compare:
<div attribute="blahblahblah" />
if you want to put a " into the blahs, it'd terminate the attribute's notation, and the HTML code would break. So, adding a single " character should look like:
<div attribute="blah"e;blahblah" />
Now, if you miss the semicolon, the browser will display blah"blahblah instead of blah"blahblah.
I've just noted that your code is actually glueing up the JavaScript code. In JavaScript, the semicolon is an expression delimiter, so probably there is actually a " in the emitted HTML and it is just improperly presented in the error message... Or maybe you have forgotten to open/close some quotes in the javascript, and the semicolon is actually treated as expression terminator?
Be also sure to check why the JavaScript code undergoes html-entity translation. Usually, blocks are not reparsed. Are you setting that JavaScript code as a HTML element attribute? like OnClick or OnSend? Then stop doing it now. Create a javascript-function with this code and call that function from the click/send instead.. It is not worth to encode long expressions in the JS into an attribute! Just a waste of time and nerves.
If all else fails and if the JavaScript is emitted correctly, then look for any text-correcting or text-highlighting or text-formatting modules you have on your site. Quite probable that one of them is mis-reading the html entities and removed the semicolon, or the opposite - that they add them were they are not needed. The ASP.Net itself in general does its job right, and it translates the entites correctly wherever they are needed, so I'd look at the other libraries first.
You can use something like this:
String str=#"hello,,?!"
This should escape all characters
Or
String TestString = "This is a <Test String>.";
String EncodedString = Server.HtmlEncode(TestString);
Here's the manual: http://msdn.microsoft.com/en-us/library/w3te6wfz.aspx
What else are you doing with the string?
Seems that somewhere after that the string gets encoded. You can could use HttpUtility.HtmlDecode(str); but first you'll have to figure out where your string gets encoded in the first place.
Keep in mind that if you use <%: %> in aspx or #yourvarin Razor it will get encoded automatically. You'll have to use #Html.Raw(yourvar) to suppress that.