String Replace not replacing single quotes [duplicate] - c#

This question already has answers here:
C# string replace does not actually replace the value in the string [duplicate]
(3 answers)
Closed 5 years ago.
String not replacing Single Quotes with required characters
string abc = "STA\'ASTEST";
if (abc.Contains("'"))
{
abc.Replace("'", "\\'");
}

You are doing the replace but not assigning the result to any variable.
I assume you want to assign the result to abc
string abc = "STA\'ASTEST";
if (abc.Contains("'"))
{
abc = abc.Replace("'", "\'");
}
It is also redundant to have the if (abc.Contains("'")) because the Replace function will only replace if the expression to replace actually exists. So you can just write:
abc = abc.Replace("'", "\'");

Related

Find a pattern into a string without space [duplicate]

This question already has an answer here:
Reference - What does this regex mean?
(1 answer)
Closed 5 years ago.
I'm looking for a way to find and extract the string matching a pattern in a string without a space :
string regexpattern = #"[A-Z]{4}\d{4}$"; // ex : BERF4787
string stringWithoutSpace = "stringsampleBERF4787withpattern";
string stringMatchPattern = ??? //I want to get BEFR4787 in this variable
You are almost there. The problem in your pattern is the $ which matches the end of a string. Since in your example the "BERF4787" is located in the middle of the string you should simply remove it:
string regexpattern = #"[A-Z]{4}\d{4}"; // ex : BERF4787
string stringWithoutSpace = "stringsampleBERF4787withpattern";
If you want to match your pattern in a string you can use the Regex.Match method which returns an object of type Match.
To get the matched value you need to use the Match.Value property like this:
string stringMatchPattern = Regex.Match(stringWithoutSpace, regexpattern).Value;

Split a string after reading square brackets in c# [duplicate]

This question already has answers here:
splitting a string based on multiple char delimiters
(7 answers)
Closed 5 years ago.
string test = "Account.Parameters[\"AccountNumber\"].Caption";
string new = test.Trim("[");
I want output "AccoutNumber".
I have tried the below code but not getting the desired result:
string[] test = "Transaction.Parameters[\"ExpOtherX\"].Caption".Split('[');
string newvalue = test[1];
Just use Split with two delimiters:
string[] test = "Transaction.Parameters[\"ExpOtherX\"].Caption".Split('[', ']');
string newvalue = test[1];
You can also use Regex:
string test = "Account.Parameters[\"AccountNumber\"].Caption";
var match = System.Text.RegularExpressions.Regex.Match(test, ".*?\\.Parameters\\[\"(.*?)\"]");
if (match.Success)
{
Console.WriteLine(match.Groups[1].Value);
}
.*? is a non greedy wildcart capture, so it will match your string until it reaches the next part (in our case, it will stop at .Parameters[", match the string, and then at "])
It will match .Parameters["..."]., and extract the "..." part.
you can do a Split to that string...
string test = "Account.Parameters[\"AccountNumber\"].Caption";
string output = test.Split('[', ']')[1];
Console.WriteLine(output);

Regex replace semicolon between quotation marks C# [duplicate]

This question already has answers here:
How to remove all commas that are inside quotes (") with C# and regex
(3 answers)
Closed 3 years ago.
I want to replace all semicolons that are enclosed in quotation marks with a space. How can I do this in C#?
For example:
this string:
this is an example; "this is other ; example"
Would return:
this is an example; "this is other example"
I await your help!
Edited: This will work.
string yourString = "hello there; \"this should ; be replaced \"";
string fixedString = Regex.Replace(yourString, "(\"[^\",]+);([^\"]+\")", delegate (Match match)
{
string v = match.ToString();
return v.Replace(";", " ");
});
Try following :
string input = "this is an example; \"this is other ; example\"";
string pattern = "\"(?'prefix'[^;]+);(?'suffix'[^\"]+)\"";
string output = Regex.Replace(input,pattern,"\"${prefix} ${suffix}\"");

Convert text entered by user containing with escape sequence to string [duplicate]

This question already has answers here:
Replacing all the '\' chars to '/' with C#
(7 answers)
Closed 6 years ago.
In textbox1 user enters string "Test\u0021Test" and I would like to convert escaped character "\u0021" to "!"
string x = "Test\u0021Test"; // this is easy
string y = textbox1.Text; // here textbox1.Text = "Test\u0021Test" and this I don't know how to convert
Thanks for help
EDIT
Answered by #Simo Erkinheimo
Allow user to enter escape characters in a TextBox
Use string Replace method this case
string x = "Test\u0021Test"; // this is easy
string y = textbox1.Text.Replace("\u0021", "!");
You can do something like this. It will work for all unicode escaped symbols in input string.
var result = Regex.Replace(x, #"\\[u]([0-9a-f]{4})",
match => char.ToString(
(char)ushort.Parse(match.Groups[1].Value, NumberStyles.AllowHexSpecifier)));

Replace a set of characters in a string [duplicate]

This question already has answers here:
RegEx Starts with [ and ending with ]
(4 answers)
Closed 6 years ago.
How to replace a set of characters where I only know the first and the last one, in between is a variable that is not constant.
All I know is that this string will always start with & and it will end with ;
string str = "Hello &145126451; mate!";
How to get rid of &145126451; ?
So the desired result is:
string result = "Hello mate!"
The most easiest way is to use Regex:
Regex yourRegex = new Regex(#"&.*;");
string result = yourRegex.Replace("Hello &145126451; mate!", String.Empty);
Console.WriteLine(result);
Here is a fiddle with example.

Categories