Replace (,) with (',') [duplicate] - c#

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

Related

c# - Split Comma separated String with nested children in brackets [duplicate]

This question already has answers here:
How to split string while ignoring portion in parentheses?
(5 answers)
Closed 1 year ago.
I have a nested comma string such as
a(x,y,z),b,c(n,o,p),d,e,f(t,w)
Want to split this string in C# such as
a(x),a(y),a(z),b,c(n),c(o),c(p),d,e,f(t),f(w)
I tried splitting using combination String.Split & String.SubString. Please let me know f you any solution for this problem.
Many problems get easier if you split them into smaller problems. This is one of them.
Step 1: Split on , while ignoring separators in parenthesis (see this related question for a regex-based solution: How to split string while ignoring portion in parentheses?)
This will yield a(x,y,z), b, c(n,o,p), ...
Step 2: Split the part before and inside the parenthesis (using a regular expression or just String.Split), split the inside part on ,, loop through it and add the component before the parenthesis.
This will transform a(x,y,z) into a(x), a(y), ...

Extracting a computer name from a string [duplicate]

This question already has answers here:
Split string using backslash
(3 answers)
Closed 4 years ago.
I'm looking for the best way to extract the computer name from a predictably formatted string. The string will always be in this format:
C:\\Folder1\\Folder2\\NOOBCOMPUTER\\...
If there is a way to extract the contents of a string between the third pair of backslashes and the fourth pair, that should work.
Though I have no idea where to begin with the regex to achieve that, nor do I know if regex is the most "foolproof" way of going about this in C#.
You can split the string and then inspect each element.
string [] s = yourstring.Split("\\");
string final = s[3];

c# replace double concatenation character with single concatenation in replace method [duplicate]

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.

C# Regex: matching anything between single quotes (except single quotes) [duplicate]

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

Removing " from string in c# [duplicate]

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.

Categories

Resources