I want to convert this string with one backslah:
"{\"PartyCode\":\"99\",\"Name\":\"ooooo\",\"NameEng\":null,\"Note\":null,\"ManagerId\":1,\ParentId\":null,\"PartyStatusId\":1,\"PartyTypeId\":1,\"CenterInfoId\":4177,\"GovernateInfoId\":321,\"LocationInfoId\":25,\"SectorInfoId\":6,\"SectorGroupInfoId\":36,\"SectorCategroyInfoId\":66,\"MainBranch\":null,\"CoordinateX\":null,\"CoordinateY\":null,\"IssueDate\":\"2022-06-08T00: 00:00\"}"
To this string with 3 backslashes:
"{\\\"PartyCode\\\":\\\"99\\\",\\\"Name\\\":\\\"ooooo\\\",\\\"NameEng\\\":null,\\\"Note\\\":null,\\\"ManagerId\\\":1,\\\"ParentId\\\":null,\\\"PartyStatusId\\\":1,\\\"PartyTypeId\\\":1,\\\"CenterInfoId\\\":4177,\\\"GovernateInfoId\\\":321,\\\"LocationInfoId\\\":25,\\\"SectorInfoId\\\":6,\\\"SectorGroupInfoId\\\":36,\\\"SectorCategroyInfoId\\\":66,\\\"MainBranch\\\":null,\\\"CoordinateX\\\":null,\\\"CoordinateY\\\":null,\\\"IssueDate\\\":\\\"2022-06-08T00: 00:00\\\"}"
I tried .Replace("\\","\\\\\\") but did not work.
I enjoyed
//classical
string input = "{\"PartyCode\":\"99\",\"Name\":\"ooooo\",\"NameEng\":null,\"Note\":null,\"ManagerId\":1,\"ParentId\":null,\"PartyStatusId\":1,\"PartyTypeId\":1,\"CenterInfoId\":4177,\"GovernateInfoId\":321,\"LocationInfoId\":25,\"SectorInfoId\":6,\"SectorGroupInfoId\":36,\"SectorCategroyInfoId\":66,\"MainBranch\":null,\"CoordinateX\":null,\"CoordinateY\":null,\"IssueDate\":\"2022-06-08T00: 00:00\"}";
string output = input.Replace("\"", #"\\\""");
Console.WriteLine(output);
//or extreme something like this
StringBuilder output2 = new StringBuilder();
input.Select(x=> {
if (x.ToString() == "\"")
output2.Append(#"\\\""");
else
output2.Append(x);
return x;
}).ToArray();
Console.WriteLine(output2.ToString());
The string is immutable creates a new string each time. So you have to reassign new string to the previous variable. Also instead of ("\ \ ", use (" \ " ",
var str = "{\"PartyCode\":\"99\",\"Name\":\"ooooo\",\"NameEng\":null,\"Note\":null,\"ManagerId\":1,\"ParentId\":null,\"PartyStatusId\":1,\"PartyTypeId\":1,\"CenterInfoId\":4177,\"GovernateInfoId\":321,\"LocationInfoId\":25,\"SectorInfoId\":6,\"SectorGroupInfoId\":36,\"SectorCategroyInfoId\":66,\"MainBranch\":null,\"CoordinateX\":null,\"CoordinateY\":null,\"IssueDate\":\"2022-06-08T00: 00:00\"}";
str = str.Replace("\"", "\\\\\\\"");
Your original string is escaping the double quotes.
Your desired string is escaping the double quotes, and adding an escaped backslash preceeding the double quotes.
So to get your desired string, you need to ensure that you preceed every double quote with a backslash.
You could do that with .Replace(#"""", #"\""").
Also note that your original string is missing a double quote before ParentId.
Related
I am sending data from arduino to c# and have a problem. The value I get from the serialread comes with an "\r" at the end of it, example: "19.42\r". I found a solution to delete the characters after my number by using Regex. But it also makes my double an integer. "19.42\r" becomes "1942". How can I delete my string but still keep the value as a double?
line = Regex.Replace(line, #"[^\d]", string.Empty);
You want to trim the whitespace from the end of the string.
Use
line = line.TrimEnd();
See the C# demo
If you need to actually extract a double number from a string with regex, use
var my_number = string.Empty;
var match = Regex.Match(line, #"[0-9]+\.[0-9]+");
if (match.Success)
{
my_number = match.Value;
}
If the number can have no fractional part, use #"[0-9]*\.?[0-9]+" regex.
string data = "19.42\r";
return data.Substring(0, data.Length - 1);
or even better
data.TrimEnd('\r')
if \r is fixed characters you want to remove
string str = "awdawdaw\r";
str = str.replace("\r","");
if \r is not fixed characters you want to remove
string str = "awdawdaw\\";
str = str.Substring((str.Length - 2), 2); \\will be removed
I need a c# function which will replace all special characters customized by the client from a string Example
string value1 = #"‹¥ó׬¶ÝÆ";
string input1 = #"Thi¥s is\123a strÆing";
string output1 = Regex.Replace(input1, value1, "");
I want have a result like this : output1 =Thi s is\123a str ing
Why do you need regex? This is more efficient, concise also readable:
string result = string.Concat(input1.Except(value1));
If you don't want to remove but replace them with a different string you can still use a similar(but not as efficient) approach:
string replacement = "[foo]";
var newChars = input1.SelectMany(c => value1.Contains(c) ? replacement : c.ToString());
string result = string.Concat( newChars ); // Thi[foo]s is\123a str[foo]ing
Someone asked for a regex?
string value1 = #"^\-[]‹¥ó׬¶ÝÆ";
string input1 = #"T-^\hi¥s is\123a strÆing";
// Handles ]^-\ by escaping them
string value1b = Regex.Replace(value1, #"([\]\^\-\\])", #"\$1");
// Creates a [...] regex and uses it
string input1b = Regex.Replace(input1, "[" + value1b + "]", " ");
The basic idea is to use a [...] regex. But first you have to escape some characters that have special meaning inside a [...]. They should be ]^-\ Note that you don't need to escape the [
note that this solution isn't compatible with non-BMP unicode characters (characters that fill-up two char)
A solution that is compatible with them is more complex, but for normal use it shouldn't be a problem.
In the below code i have a string array which holds values like "1,2,3" i want to remove the double quotes and display as 1,2,3.pls help me to do this.
string[] Stocks = StockDetails.ToArray();
string s = string.Join(",", Stocks);
You can just use string.Replace
var listOfStrings = StockDetails.Select(s => s.Replace("\"", string.Empty))
.ToList();
If you're having string in this format
string str = "\"1,2,3\"";
..then you can simply remove the double quotation marks using Replace method on string.
str = str.Replace("\"", "");
..this will replace the double quotation from your string, and will return the simple text that is not a double quotes.
More I don't think strings are like this, you're having a look at the actual value of the variable string you're having. When you will use it, it will give you the actual 1, 2, 3 but however, use the above code to remove the quotation .
I tried:
string endtag = "\"","\"";
But that's not working. The string should be: ","
The " is also part of the string not only the ,
Try this:
string endtag = "\",\"";
To explain, the first " begins a string literal, the \" is an escape sequence for a " character, the , is a regular character, again, the \" is an escape sequence for a " character, then the final " closes the string literal.
You could also use a verbatim literal like this:
string endtag = #""",""";
Here the first " begins a string literal, and the # preceding it introduces it as a verbatim string literal. The next two "" are a special escape sequence for a " character within a verbatim string literal, the , is a regular character, again, next two "" are a special escape sequence for a " character, then the final " closes the string literal.
try string endtag = "\",\""; Alternatively you could try string endtag = #""","""; Hope that helps.
Another option:
string endtag = #""",""";
#"..." is a verbatim string literal
To use a " in this literal, it is doubled
Like so:
string endtag = "\",\"";
Try this :
String endtag = "\"" , "\"";
" is an escape sequence character.
If you want to put " in your string, you should use \" like this:
string endtag = "\",\"";
Or you can define verbatim string literal using double double quotation marks ("") like;
string endtag = #""",""";
This will work:
string endtag="\",\"";
How can I replace a single quote (') with a double quote (") in a string in C#?
You need to use the correct escape sequence for the quotes symbol, you can find more information about escape sequencies here.
String stringWithSingleQuotes= "src='http://...';";
String withDoubleQuotes = stringWithSingleQuotes.Replace("'","\"");
var abc = "hello the're";
abc = abc.Replace("'","\"");
string str;
str=strtext.Replace('"','\'');