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": " ');
Related
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.
Input String is "How are you"
Expected output \"How are you\"
Due to combination of DoubleQuote and escape sequence I am not able to replace the string in the required format.
Please can someone provide me the code snippet for doing this.
I have tried below does not work
myString.Replace(""","\"");
Yes, that looks a little bit weird, you need to escape both:
string test = "\"How are you\"";
test = test.Replace("\"", "\\\"");
Both " and \ have special meaning within a string literal and must be escaped with \.
So something like this:
myString.Replace("\"","\\\"");
Escape your backslash
myString.Replace("\"","\\\"");
This:
"\\\""
produces this:
\"
And also you should escape your double quote in first parameter.
And you can use verbatim strings,but there is a weird case about double quote, instead of \" you should use two double quotes "" to escepe your charachter:
myString.Replace(#"""",#"\""");
string myString = "\"How are you\"";
myString = myString.Replace("\"", "\\\"");
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
((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'
I'm trying to match on some inconsistently formatted HTML and need to strip out some double quotes.
Current:
<input type="hidden">
The Goal:
<input type=hidden>
This is wrong because I'm not escaping it properly:
s = s.Replace(""","");
This is wrong because there is not blank character character (to my knowledge):
s = s.Replace('"', '');
What is syntax / escape character combination for replacing double quotes with an empty string?
I think your first line would actually work but I think you need four quotation marks for a string containing a single one (in VB at least):
s = s.Replace("""", "")
for C# you'd have to escape the quotation mark using a backslash:
s = s.Replace("\"", "");
I didn't see my thoughts repeated already, so I will suggest that you look at string.Trim in the Microsoft documentation for C# you can add a character to be trimmed instead of simply trimming empty spaces:
string withQuotes = "\"hellow\"";
string withOutQotes = withQuotes.Trim('"');
should result in withOutQuotes being "hello" instead of ""hello""
s = s.Replace("\"", "");
You need to use the \ to escape the double quote character in a string.
You can use either of these:
s = s.Replace(#"""","");
s = s.Replace("\"","");
...but I do get curious as to why you would want to do that? I thought it was good practice to keep attribute values quoted?
s = s.Replace("\"",string.Empty);
c#: "\"", thus s.Replace("\"", "")
vb/vbs/vb.net: "" thus s.Replace("""", "")
If you only want to strip the quotes from the ends of the string (not the middle), and there is a chance that there can be spaces at either end of the string (i.e. parsing a CSV format file where there is a space after the commas), then you need to call the Trim function twice...for example:
string myStr = " \"sometext\""; //(notice the leading space)
myStr = myStr.Trim('"'); //(would leave the first quote: "sometext)
myStr = myStr.Trim().Trim('"'); //(would get what you want: sometext)
You have to escape the double quote with a backslash.
s = s.Replace("\"","");
s = s.Replace(#"""", "");
This worked for me
//Sentence has quotes
string nameSentence = "Take my name \"Wesley\" out of quotes";
//Get the index before the quotes`enter code here`
int begin = nameSentence.LastIndexOf("name") + "name".Length;
//Get the index after the quotes
int end = nameSentence.LastIndexOf("out");
//Get the part of the string with its quotes
string name = nameSentence.Substring(begin, end - begin);
//Remove its quotes
string newName = name.Replace("\"", "");
//Replace new name (without quotes) within original sentence
string updatedNameSentence = nameSentence.Replace(name, newName);
//Returns "Take my name Wesley out of quotes"
return updatedNameSentence;
s = s.Replace( """", "" )
Two quotes next to each other will function as the intended " character when inside a string.
if you would like to remove a single character i guess it's easier to simply read the arrays and skip that char and return the array. I use it when custom parsing vcard's json.
as it's bad json with "quoted" text identifiers.
Add the below method to a class containing your extension methods.
public static string Remove(this string text, char character)
{
var sb = new StringBuilder();
foreach (char c in text)
{
if (c != character)
sb.Append(c);
}
return sb.ToString();
}
you can then use this extension method:
var text= myString.Remove('"');