How do I make a string of ","? - c#

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="\",\"";

Related

How to convert a string with one backslash to three backslashes

I want to convert this string with one backslah:
"{\"PartyCode\":\"99\",\"Name\":\"ooooo\",\"NameEng\":null,\"Note\":null,\"ManagerId\":1,\ParentId\":null,\"PartyStatusId\":1,\"PartyTypeId\":1,\"CenterInfoId\":4177,\"GovernateInfoId\":321,\"LocationInfoId\":25,\"SectorInfoId\":6,\"SectorGroupInfoId\":36,\"SectorCategroyInfoId\":66,\"MainBranch\":null,\"CoordinateX\":null,\"CoordinateY\":null,\"IssueDate\":\"2022-06-08T00: 00:00\"}"
To this string with 3 backslashes:
"{\\\"PartyCode\\\":\\\"99\\\",\\\"Name\\\":\\\"ooooo\\\",\\\"NameEng\\\":null,\\\"Note\\\":null,\\\"ManagerId\\\":1,\\\"ParentId\\\":null,\\\"PartyStatusId\\\":1,\\\"PartyTypeId\\\":1,\\\"CenterInfoId\\\":4177,\\\"GovernateInfoId\\\":321,\\\"LocationInfoId\\\":25,\\\"SectorInfoId\\\":6,\\\"SectorGroupInfoId\\\":36,\\\"SectorCategroyInfoId\\\":66,\\\"MainBranch\\\":null,\\\"CoordinateX\\\":null,\\\"CoordinateY\\\":null,\\\"IssueDate\\\":\\\"2022-06-08T00: 00:00\\\"}"
I tried .Replace("\\","\\\\\\") but did not work.
I enjoyed
//classical
string input = "{\"PartyCode\":\"99\",\"Name\":\"ooooo\",\"NameEng\":null,\"Note\":null,\"ManagerId\":1,\"ParentId\":null,\"PartyStatusId\":1,\"PartyTypeId\":1,\"CenterInfoId\":4177,\"GovernateInfoId\":321,\"LocationInfoId\":25,\"SectorInfoId\":6,\"SectorGroupInfoId\":36,\"SectorCategroyInfoId\":66,\"MainBranch\":null,\"CoordinateX\":null,\"CoordinateY\":null,\"IssueDate\":\"2022-06-08T00: 00:00\"}";
string output = input.Replace("\"", #"\\\""");
Console.WriteLine(output);
//or extreme something like this
StringBuilder output2 = new StringBuilder();
input.Select(x=> {
if (x.ToString() == "\"")
output2.Append(#"\\\""");
else
output2.Append(x);
return x;
}).ToArray();
Console.WriteLine(output2.ToString());
The string is immutable creates a new string each time. So you have to reassign new string to the previous variable. Also instead of ("\ \ ", use (" \ " ",
var str = "{\"PartyCode\":\"99\",\"Name\":\"ooooo\",\"NameEng\":null,\"Note\":null,\"ManagerId\":1,\"ParentId\":null,\"PartyStatusId\":1,\"PartyTypeId\":1,\"CenterInfoId\":4177,\"GovernateInfoId\":321,\"LocationInfoId\":25,\"SectorInfoId\":6,\"SectorGroupInfoId\":36,\"SectorCategroyInfoId\":66,\"MainBranch\":null,\"CoordinateX\":null,\"CoordinateY\":null,\"IssueDate\":\"2022-06-08T00: 00:00\"}";
str = str.Replace("\"", "\\\\\\\"");
Your original string is escaping the double quotes.
Your desired string is escaping the double quotes, and adding an escaped backslash preceeding the double quotes.
So to get your desired string, you need to ensure that you preceed every double quote with a backslash.
You could do that with .Replace(#"""", #"\""").
Also note that your original string is missing a double quote before ParentId.

c# How to escape a directory path with differend letters?

Hi there I have this
string s = #"A:\"
And i have to change the letter, so i need this
string s= #" + Letter + :\"
I already tried something, but it was lame...
Try this:
string s = Letter + #":\";
If you're using C# 6.0, you could use interpolated strings. (but you need to escape the \)
string s = $"{Letter}:\\";
You either need to escape the backslash, because it's a special character, by using two backslashes, like this:
string s = Letter + ":\\"
or you need to indicate that the string with the backslash should be interpreted "verbatim" by putting an # in front of it, like this:
string s = Letter + #":\"
You can use string format to help. There is also string interpolation if you are using c# 6.0.
var Letter = "A";
string s = string.Format(#"{0}:\", Letter);
String interpolation with c# 6.0
string s = $"{Letter}:\\";

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 \".

How to quote \" (slash double-quote) in a string literal?

This is probably a really simple question but I can't seem to get my head around it. I need to have a string that contains \" without it seeing it as an escape character. I tried using # but it won't work. The only other way I thought of doing this would be to use \u0022 but don't want to unless I can help it.
Desired string - string s = "\"\""; // Obviously this doesn't work!
Desired console output - \"\"
Hope this makes sense.
Thanks!
Try
string s = "\\\"\\\"";
You have to escape your backslashes too.
Mike
You can use the literal, but you need to double-up quotes.
string s = #"\""\""";
In verbatim string literals (#"...") a " in the string value is encoded as "", which happens to also be the only escape sequence in verbatim strings.
#"\""Happy coding!\""" // => \"Happy coding!\"
"\\\"Happy coding!\\\"" // => \"Happy coding!\"
Note that in the 2nd case (not a verbatim string literal), a \ is required before the \ and the " to escape them and prevent their normal meanings.
See the C# string reference for more details and examples.
I think you have to escape backslashes too... so something like "\\\"\\\"" should work, I believe.
Use this string:
string s = "\\\"\\\"";
Console.WriteLine( "\\\"\\\"" );
Just put a \ before each character that needs to be printed.
String s = #"\""\""";
DblQuote characters will escape a second dblquote character
Though for better readability I would go with:
const String DOUBLEQUOTE = """";
const String BACKSLASH = #"\";
String s = BACKSLASH + DOUBLEQUITE + BACKSLASH + DOUBLEQUOTE;
In a verbatim string (a string starting with #"") to escape double quotes you use double quotes, e.g. #"Please press ""Ok"".". If you want to do it with verbatim strings then you would do something like #"\""" (that's 3 double quotes on the end there).
You can do like this,
string s = "something'\\\'";
Use a single '' rather then "" in string to do the same.

How can i declare ^\ in a string or character

I would like to declare ^\ this as a string or character. I used the below but i am getting an error
string str="^\";
So can any one help me
You have to escape the backslash.
string str = "^\\";
Or, you can use the verbatim syntax.
string str = #"^\";
\ is the escape token, you have to double it to use it as a literal in a string:
string str = "^\\";
Alternatively, you can use literal string syntax:
string str = #"^\";
You need to escape backslash characters, so instead of:
string str = "^\";
use:
string str = "^\\";
I have no idea about C#, but can you try:
string str="^\\";

Categories

Resources