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.
Related
This question already has answers here:
Regular Expression Except this Characters
(4 answers)
C# Regular Expressions, string between single quotes
(4 answers)
Closed 5 years ago.
How do I match anything between single quotes? I need to match all attribute = 'some value' statements within a WHERE clause of queries. I tried:
= '(.+)'
But that doesn't work: somehow messes up all single quotes and matches.
If anyone could help me out it'd be much appreciated!
Try:
= '([^']*)'
Meaning you want anything/everything from = ' that isn't a single quote up to a single quote.
Python example:
import re
text = "attribute = 'some value'"
match = re.search("= '([^']*)'", text)
print(match.group(1))
To read more about that, it is called a negated character class: https://www.regular-expressions.info/charclass.html
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:
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:
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('\"',' ');