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('\"',' ');
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:
How to read RegEx Captures in C#
(3 answers)
Closed 7 years ago.
First of all sorry about the tittle. Didn't know how to explain it.
This is my code:
string sString = #"docs/horaires/1/images/1"
var PickImage = Regex.Matches(sString, "/horaires/(.*?)/images/");
Console.WriteLine(PickImage[0].Value);
This will print /horaires/1/images/ instead of 1. I tried all the RegexOptions but didn't find the solution in there.
What am I doing wrong here?
This is what you need:
string sString = #"docs/horaires/1/images/1";
var pickImage = Regex.Match(sString, #"/horaires/(.*?)/images/");
if (pickImage.Success)
Console.WriteLine(pickImage.Groups[1].Value);
In your original code, PickImage[0] is a Match object, and Value will return the full match. You want the first captured group, so use match.Groups[1].Value. Note that Groups[0] always contains the full match.
No need to use Matches is you want a single result, use Match instead.
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.
This question already has answers here:
Trim last character from a string
(15 answers)
Closed 8 years ago.
I have string like below.
string s="this is item1,item2,item3,,, ,, ,";
now i want to remove (,) from right side of string.
Thanks in advance
i have tried
string.Replace(",", "");
and
string.TrimEnd("'");
but not working
Try this:
s = s.TrimEnd(',', ' ');
I think the problem in your code is that you do not assign the result of your replacement to any variable. And also your solution would remove all , from the string, not only those on the right side.
Try string.TrimEnd():
s= s.TrimEnd(',',' ');