'u0022' to introduce quotation marks to string C# - c#

I have a string property that returns a directory, it has "program file//" in it, so to use it in a command prompt, I have to use quotation mark around the string. But if I do the following
string myDic = someDic;
string myCmdPrptDic = '\u0022' + someDic + "\u0022'
myCmdPrptDic ends up like \"C://Program Files//myApp\" and will not work under cmd prompt. Is there a way to just create "C://Program Files//myApp" only? Or maybe I should just use a stringBuilder....
I just need to generate a string like:
copy //data/file// "C://program files//myapp"
but I could not do it as
string = "copy //data//file" +" "+ '\u0022' + someDic + "\u0022';

Are you sure that is actually what your string is and not just the way you are viewing it? In the watch window of VS debugger it puts strings in double quotes already which means it needs to escape any quotes inside it. As it stands the above code will not put the \ into your string. If it really is in there (and not just a misreading of the debug information) then there is some other code somewhere adding it in.

Your quotes " and ' are mis-matched.

Related

Remove "\" from string

I've got string from json.
"[{\"Id\":\"1\",\"someOption\":\"2\",\"someText\":\"qweqe\",\"someNumber\":\"123\"},{\"Id\":\"2\",\"someOption\":\"2\",\"someText\":\"qweqw\",\"someNumber\":\"323\"}]"
I want to remove the "\" sign.
Used this for parsing and it didn't work. Debugging shows it's ineffective:
Regex.Replace(json, #"\", "");
and
string signer = json.Replace(#"\", "");
Also Visual Studio shows that pattern #"\"(tried it too in those functions) isn't matching "\" in fact.
I'm confused. Does anyone knows how to create the right pattern here?
I want to retrieve the data later from this match
ex.
[{"id":"1", "someOption":"2", (..) so on}]
Thanks!
You should replace \" by ' . i.e.
string signer = json.Replace("\"", "'");
So, you have:
[{'id':'1', 'someOption':'2', (..) so on}]
Now, you can retrieve the data as you want.
I hope this will be useful.
Language is c#, that escape some characters in string. Char " is one.
This means that the string not contains "\", but you see an escaped string.
In debug, you can see not escaped value in watch by clicking magnifying glass.

4 Backslash on network path string

If I do this:
string path = "\\myServer\myFile.txt"
I get a compilation error. So I do this:
string path = #"\\myServer\myFile.txt"
Altought the output of path in QUICK WATCH of Visual Studio is:
\\\\myServer\myFile.txt
Is there any clean way to avoid the problem of this 4 backslashes?
Although the output of path is:
\\\\myServer\myFile.txt
No, it's not. The value you might see in the debugger would be
\\\\myServer\\myFile.txt
which is just the debugger escaping the value for you.
The value of the string has a double backslash at the start, and a single backslash in the middle. For example:
Console.WriteLine(#"\\myServer\myFile.txt");
will print
\\myServer\myFile.txt
It's important to differentiate the actual content of the string, and some format seen in the debugger.
If you want to express the same string in code without using a verbatim string literal (that's the form starting with #) you can just escape each backslash:
string path = "\\\\myServer\\myFile.txt";
Again, the actual value there only has a total of three backslashes. For example:
string path1 = "\\\\myServer\\myFile.txt";
string path2 = #"\\myServer\myFile.txt";
Console.WriteLine(path1 == path2); // true
They're different literals representing the same string content.
string path = #"\\" will not create a string with 4 backslashes. It might look like that in the debugger, but try to Debug.WriteLine() it: there's only 2.
No, you have the choise between
string test = #"\\myServer\myFile.txt";
or
string test = "\\\\myServer\\myFile.txt";
Both contains \\myServer\myFile.txt

String escaping

I want find and replace a substring in a string in C#.
The substring I want to find looks like this:
],\"
and the substring i want to replace looks like this ],\"Name
This is what i tried so far:
string find = #"],\""";
string replace = #"],\""Name";
string newjson = jsonstring.Replace( find, replace );
From your comment
Debugger show me like this "],\\\"Name"
That is the correct output, the debugger is showing you the escaped version of your string. the \\ turns in to a single \ and the \" turns in to a " once the escaping has been applied.
If you click the magnifying glass in the box in your debugger it will open a new window with the escaping applied.
Are you getting any errors when doing what you did?
Otherwise try without using a literal "#"
so something like this:
string find = "],\\\"";
string replace = "],\\\"Name";
string newjson = jsonstring.Replace(find, replace);
Sometime double quotes and string literals still give me issues so I do it that way without using the literal. Hope that helps.

Having issue appending double quote inline into string builder

Having an issue replacing a pipe with a double quote in a stringbuilder.
sbSql.Append("|" + State + "|");
string strSql = sbSql.ToString().Replace("|", "\"");
Code above yields \"KS\" where I need it to be "KS" or whatever value is in State.
Ideas?
No, it doesn't. You only think that there are backslashes in front of the quotation marks because you are looking at the value in the debugger, which shows the string as you would write it as a string literal.
If you are seeing \"KS\" in a debug window, you have "KS" in strSql (unless I am mistaken in how I am reading your question.)

.NET string IndexOf unexpected result

A string variable str contains the following somewhere inside it: se\">
I'm trying to find the beginning of it using:
str.IndexOf("se\\\">")
which returns -1
Why isn't it finding the substring?
Note: due to editing the snippet showed 5x \ for a while, the original had 3 in a row.
Your code is in fact searching for 'se\\">'. When searching for strings including backslashes I usually find it easier to use verbatim strings:
str.IndexOf(#"se\"">")
In this case you also have a quote in the search string, so there is still some escaping, but I personally find it easier to read.
Update: my answer was based on the edit that introduced extra slashes in the parameter to the IndexOf call. Based on current version, I would place my bet on str simply not containing the expected character sequence.
Update 2:
Based on the comments on this answer, it seems to be some confusion regarding the role of the '\' character in the strings. When you inspect a string in the Visual Studio debugger, it will be displayed with escaping characters.
So, if you have a text box and type 'c:\' in it, inspecting the Text property in the debugger will show 'c:\\'. An extra backslash is added for escaping purposes. The actual string content is still 'c:\' (which can be verified by checking the Length property of the string; it will be 3, not 4).
If we take the following string (taken from the comment below)
" '<em
class=\"correct_response\">a
night light</em><br
/><br /><table
width=\"100%\"><tr><td
class=\"right\">Ingrid</td></tr></table>')"
...the \" sequences are simply escaped quotation marks; the backslashes are not part of the string content. So, you are in fact looking for 'se">', not 'se\">'. Either of these will work:
str.IndexOf(#"se"">"); // verbatim string; escape quotation mark by doubling it
str.IndexOf("se\">"); // regular string; escape quotation mark using backslash
This works:
string str = "<case\\\">";
int i = str.IndexOf("se\\\">"); // i = 3
Maybe you're not correctly escaping one of the two strings?
EDIT there's an extra couple of \ in the string you are searching for.
Maybe the str variable does not actually contain the backslash.
It may be just that when you mouse over the variable while debugging, the debugger tooltip will show the escape character.
e.g. If you put a breakpoint after this assignment
string str = "123\"456";
the tooltip will show 123\"456 and not 123"456.
However if you click on the visualize icon, you will get the correct string 123"456
Following code:
public static void RunSnippet()
{
string s = File.ReadAllText (#"D:\txt.txt");
Console.WriteLine (s);
int i = s.IndexOf("se\\\">");
Console.WriteLine (i);
}
Gives following output:
some text before se\"> some text after
17
Seems like working to me...
TextBox2.Text = TextBox1.Text.IndexOf("se\"">")
seems to work in VB.
DoubleQuotes within a string need to be specified like "" Also consider using verbatim strings - So an example would be
var source = #"abdefghise\"">jklmon";
Console.WriteLine(source.IndexOf(#"se\"">")); // returns 8
If you are looking for se\">
then
str.IndexOf(#"se\"">")
is less error-prone. Note the double "" and single \
Edit, after the comment: it seems like the string may contain ecaping itself, in which case in se\"> the \" was an escaped quote, so the literal text is simply se"> and the string to use is Indexof("se\">")

Categories

Resources