This question already has answers here:
Quotation mark in string
(3 answers)
C# string replace does not actually replace the value in the string [duplicate]
(3 answers)
Closed 7 years ago.
I have a string that has the value /Daily". and the parameter that i'm trying to pass into my application is /Daily. However after trying a few methods to remove the quote, nothing seems to be working.
Methods used
.remove('"');
.replace("\"");
.trim('"');
Remember that strings in .NET are immutable, so calling the Replace method doesn't actually change the underlying string--it returns a value that represents a new string based on the Replace operation. You'll need to capture that returned value for this to work:
var str = "/Daily\"";
str = str.Replace("\"", "");
Also notice that quotes are escaped by backslashes (\) in C#, rather than forward-slashes.
Related
This question already has answers here:
Replacing double quote with a single quote
(3 answers)
Closed 5 years ago.
i want to replace double concatenation to single concatenation in c#
I've tried
sConfigration.Replace('"',''')
plus several other ways.
Any ideas?
You need to assign the result of Replace to the original variable:
sConfigration = sConfigration.Replace("\"","'");
string is immutable. sConfigration.Replace('"',''') returns a new string with the required replacements. If you don't assign it to anything, its as if it hadn't even called the method.
Do the following:
sConfigration = sConfigration.Replace('"',''');
And please fix the spelling.
This question already has answers here:
How to remove empty lines from a formatted string
(11 answers)
Closed 6 years ago.
How can I remove empty line from a string for example:
Turn this:
to This:
I used this code text.Replace("\n",""); but it turns it to:
testtesttes
You should replace two newline characters(if any). Following code will work on Unix as well as non unix platforms.
var stringWithoutEmptyLines = yourString.Replace(Environment.NewLine + Environment.NewLine, Environment.NewLine);
string data = stringData.Replace("\r\n\r\n", "\r\n");
This question already has answers here:
C# string replace does not actually replace the value in the string [duplicate]
(3 answers)
Closed 7 years ago.
I want to replace a single comma (,) with (',').
For example:
"text,text,text" with "text','text','text".
I tried
MyText.Replace(',',"','");
but can't get anything working properly.
Any help would be appreciated.
Try:
MyText = MyText.Replace(",","','");
There are two .Replace methods on strings. One for single characters and one for strings.
When you ',' that defines a single character so it goes to the single character version of the method. If you use double quotes then it defines a string so that version of the method is selected.
Docs on the string replace method: https://msdn.microsoft.com/en-us/library/system.string.replace(v=vs.110).aspx
This question already has answers here:
C# string replacement , not working
(6 answers)
Closed 8 years ago.
I have a string like
mukesh "salaria" engineer
how do i replace " with blank
like i want output as
mukesh salaria engineer
I already tried, str.replace("\"",string.empty);
but it doesn't working for me.
It works but must type this: str = str.replace(...)
string.Replace is a pure method(no side-effects). If you do not assign str.replace("\"",string.empty); to anything, then that statement does not make any change in the state of your object(in other words writing that line of code is equal to not writing it at all.).
Use str = str.Replace(...);
You must use the Replace method and get the string back from it. Your code must be:
str = str.Replace("\"", string.Empty);
msdn :
Returns a new string in which all occurrences of a specified string in the current instance are replaced with another specified string.
try this
str.Replace('\"',' ');
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
String.Format exception when format string contains “{”
Does following possible using C# String.Format?
Required output "products/item/{itemId}"
I've tried escaping braces but this does not work:
const string itemIdPattern = "itemId";
string result = String.Format("products/item/{{0}}", itemIdPattern);
Preferably something more nice than
string result = String.Format("products/item/{0}{1}{2}",
"{",
itemIdPattern,
"}");
You'll need 3 braces per side for that -- 2 for the braces and 1 for the replacement.
string result = String.Format("products/item/{{{0}}}", itemIdPattern);