How To Find 3 Backslashes in a String? - c#

I can't find a way to find 3 backslashes in a string like the following...
<div><div class=\\\"entry-content\\\">
Here is my code...
string str1 = "<div><div class=\\\"entry-content\\\">";
int k = str1.IndexOf(#"\\\"); // returns -1
Basically, I'm trying to replace 3 backslashes with 1 backslash like in the code below which doesn't work because k is -1
str1 = str1.Replace(#"\\\", #"\");
I want the string to be this "<div><div class=\"entry-content\">"

Your string has only one backslash.
\\\" = \"
So the first one escapes the second backslash. And third backslash escapes the double quote. Result is one backslash and a double quote.
If you want your string to contain all backslashes without escaping use a verbatim string:
string str1 = #"<div><div class=\\\""entry-content\\\"">";
You can read more about escaping and escape sequences here.

Related

How to Replace String In File With Double Quotes

want to Replace Text in My File strText #Insurer with XYZ
output be like this "XYZ"
till now i do this
strText.Replace("#Insurer",XYZ)
this gives me XYZ but not this "XYZ"
so i did this
strText.Replace("\"#Insurer\"",XYZ)
but it didn't replace my String with XYZ
If you want the text after the replacement to be quoted, then you should put the replacement string in quotes:
strText.Replace("#Insurer", "\"" + XYZ + "\"")
Otherwise, you would be searching for the literal string "#Insurer" and just replace it by XYZ. So if there were quotes (which likely isn’t the case, otherwise you wouldn’t want to add them), then this would actually remove them.
Inorder to replace with quotes you can try using backward slash ("\"). The example below shows how to implement.
public string ReplaceString(string strText)
{
string replaceWith = "\"XYZ\"";
string replacedString = strText.Replace("#Insurer", replaceWith);
return replacedString;
}
Add double quote around xyz. You have to escape the double quotes for that you use escape character i.e. backslash \. Also you are not assigning the resultant string back to strText and wont get the changed string.
strText = strText.Replace("#Insurer", "\"" + XYZ + "\"");
The second attempt that failed to replace is because you have added double quotes in string that you are trying to find and there are not double quotes in source string.

how to use " as character in C#

I need to use " and ' as a character in C#, however they are special characters. So when I put them as character in the string, it will give an error. The issue is that it has many " and ' so I need to find a way to allow me to use these special characters. How can I do that
Use escape sequences: "This is a double-quote: \", and this is a single quote: \'"
Although note that since the string is delimited by double quotes, the \' escape isn't necessary: "This is a double-quote: \", and this is a single quote: '"
Simple prefix your string with # for " use "" This makes newlines easy as pie:
string example = #"A string that has double quote "" and single quote ' also a new line
";
You can use escape character \
Example
var testSTring = "\"test\""
Having a single quote ' in string doesn't trouble C#, its the double quotes ", you have to escape them with backslash like:
string str = "some\"stri'''''ng";
You can also use verbatim string # in the start and then you have to escape double quotes with another double quote like:
string str = #"some""stri'ng";
Put a backslash before special chars you want to use to escape them.
Here's a list:
http://msdn.microsoft.com/en-us/library/h21280bw.aspx

Finding a string with double quotes

I want this line of code to work-
int start = s.IndexOf(""_type": "Person""name": "");
But clearly the double quotes are messing up the search... Any ideas about how to get this working?
You can take two approaches to this.
The first is by using a string-literal and escaping double-quotes with another double quote:
string s = #"This is a ""quoted"" string.";
s.IndexOf(#"a ""quoted"" string");
The other is to escape the double-quotes with a backslash:
string s = "This is a \"quoted\" string.";
s.IndexOf("a \"quoted\" string");
If you want to use a double-quote in a string, one way is to escape it with a backslash. \
string myString = "This is a string \" with a double quote";
So what you want to do is escape the string? Try this:
int start = s.IndexOf(#"this ""word"" is escaped");
I'm assuming you want to run IndexOf() on the entire string, including the quotes inside? All you have to do is use both types of quotes: ' ' and " ". As long as you use one to designate the main string and the other to designate sub-strings, it should work, i.e. something like: s.IndexOf(' "_type": "Person""name": " ');

What does it mean when I enclose a C# string in #" "? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
What does # mean at the start of a string in C#?
Sorry but I can't find this on Google. I guess it maybe is not accepting my search string when I do a search.
Can someone tell me what this means in C#
var a = #"abc";
what's the meaning of the #?
It is a string literal. Which basically means it will take any character except ", including new lines. To write out a ", use "".
The advantage of #-quoting is that escape sequences are not processed,
which makes it easy to write, for example, a fully qualified file
name:
#"c:\Docs\Source\a.txt" // rather than "c:\\Docs\\Source\\a.txt"
It means it's a literal string.
Without it, any string containing a \ will consider the next character a special character, such as \n for new line. With a # in front, it will treat the \ literally.
In the example you've given, there is no difference in the output.
This says that the characters inside the double quotation marks should be interpreted exactly as they are.
You can see that the backslash is treated as a character and not an
escape sequence when the # is used. The C# compiler also allows you to
use real newlines in verbatim literals. You must encode quotation
marks with double quotes.
string fileLocation = "C:\\CSharpProjects";
string fileLocation = #"C:\CSharpProjects";
Look at here for examples.
C# supports two forms of string literals: regular string literals and verbatim string literals.
A regular string literal consists of zero or more characters enclosed
in double quotes, as in "hello", and may include both simple escape
sequences (such as \t for the tab character) and hexadecimal and
Unicode escape sequences.
A verbatim string literal consists of an # character followed by a
double-quote character, zero or more characters, and a closing
double-quote character. A simple example is "hello". In a verbatim
string literal, the characters between the delimiters are interpreted
verbatim, the only exception being a quote-escape-sequence. In
particular, simple escape sequences and hexadecimal and Unicode
escape sequences are not processed in verbatim string literals. A
verbatim string literal may span multiple lines.
Code Example
string a = "hello, world"; // hello, world
string b = #"hello, world"; // hello, world
string c = "hello \t world"; // hello world
string d = #"hello \t world"; // hello \t world
string e = "Joe said \"Hello\" to me"; // Joe said "Hello" to me
string f = #"Joe said ""Hello"" to me"; // Joe said "Hello" to me
string g = "\\\\server\\share\\file.txt"; // \\server\share\file.txt
string h = #"\\server\share\file.txt"; // \\server\share\file.txt
string i = "one\r\ntwo\r\nthree";
string j = #"one
two
three";
Reference link: MSDN

string.Replace does not work for quote

((string)dt.Rows[i][1]).Replace("'", "\\'")
I want the result that if any string have quote it change it into slash quote, e.g. John's -> John\'s
but the above replace function is not working fine.
it results like John\\'s
but if we change the code to
((string)dt.Rows[i][1]).Replace("'", "\'")
it gives the Result like John's
does change it anyway.
Because the backslash is the escape character, you need to tell it you want to treat it like a literal string. You do this by prepending an # to the string:
((string)dt.Rows[i][1]).Replace("'", #"\'")
Try a double backslash.
\\
Just one backslash is an escape; two is an actual backslash.
Use "\\'" or #"\'" for the replacement string. The backslash is the escape character in C# string literals. See the explanation of string literals in C#: \' in a string literal results in just a single quote.
The reason this escape sequence exists, is because single quotes would require escaping if you were using a char literal ('\'').
The # indicates that you're using verbatim string syntax, which allows for multi-line strings and eliminates the need to escape characters, apart from double quote, which you would escape with double double quotes (Visual Basic style).
Can you clarify please? Are you saying that
((string)dt.Rows[i][1]).Replace("'", "\\'")
does not replace a ' with \' ?
Because I just tried it and it works fine. I.e. this
string one = "blah'";
string two = one.Replace("'", "\\'");
Console.WriteLine(two);
Prints blah\'
Replace("'", "\'") use double slash
You could use something like this:
private static string replace(String input)
{
return Regex.Replace(input, "('|\")", "\\'");
}
static void Main(string[] args)
{
String value1 = "John Steve's";
String value2 = "John Steve\"s";
Console.WriteLine(replace(value1));
Console.WriteLine(replace(value2));
}
Results:
John Steve\'s
John Steve\'s
If you want to prepare an SQL query, I think the best method is to replace a single ' for ''. For instance, if you wanto to search John O'Connor, this would work (at least in SQL Server, Access, Oracle, ...).
select ... from users where username = 'Jonh O''Connor'

Categories