To remove double quotes in string array - c#

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 .

Related

How to convert a string with one backslash to three backslashes

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.

Delete string from a double in C#

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

How to remove quotation marks from a string?

I have a string value returned by a method and it's within quotation. As an example '59'. Now i want to remove quotation marks and give an output as 59. How can i do it?
'59' -> 59
The simplest way I think is to replace the ' with an empty string:
string Quoted = "'59'";
string UnQuoted = Quoted.Replace("'", "");
yet another more generic solution to extract numeric values from strings:
string str = "'59'";
int num = int.Parse(Regex.Match(str, #"\d+").Value); //59

how to remove first and last double quotes in a json string in c#?

I want to remove first and last double quotes in my json string.
"[{\"CircleID\":1,\"CircleName\":\"Andhra Pradesh\"},{\"CircleID\":4,\"CircleName\":\"Assam\"},{\"CircleID\":5,\"CircleName\":\"Bihar\"},{\"CircleID\":6,\"CircleName\":\"Chennai\"},{\"CircleID\":7,\"CircleName\":\"Delhi\"},{\"CircleID\":8,\"CircleName\":\"Gujarat\"},{\"CircleID\":9,\"CircleName\":\"Himachal Pradesh\"},{\"CircleID\":10,\"CircleName\":\"Haryana\"},{\"CircleID\":17,\"CircleName\":\"Mumbai\"},{\"CircleID\":26,\"CircleName\":\"Jharkhand\"},{\"CircleID\":27,\"CircleName\":\"Chhattisgarh\"}]"
in above json string I want to remove first and last double quotes.how to remove.Iam tried but no use. Iam using string.replce method,string.trim() method but quotes are not removed.please help me.
You can use String.Trim do not forget to assign the result back to string variable. string in c# are immutable and we need to assign the changed value back to the variable.
jsonStr = jsonStr.Trim( '"' );
If you have multiple characters you can use overloaded String.Trim(Char[])
jsonStr = jsonStr.Trim( new Char[] { '"' } );
Edit in OP there is only on double quote in beginning and end of string but if we have multiple double quotes in beginning and end and we just want to remove only first and last then we can use string.Substring
jsonStr = jsonStr.Substring(1, jsonStr.Length-2);
I'm assuming " and \" are actually in the string, and not an artifact of embedding that string in source code or displaying it in the debugger.
In that case, you don't want to just remove the initial and trailing ", but you need to treat it as a json string and decode it. This will also replace the encoded \" by plain " inside the string.
If you're using Newtonsoft Json:
decodedString = JsonConvert.DeserializeObject<string>(jsonString);
this is help for me.
responseContent = responseContent.Substring(1, responseContent.Length - 1);
responseContent = responseContent.Substring(0, responseContent.Length - 1);

Remove text after point in a string

So as the title says I want to know how to remove text after point in a string. E.g.
I have a string, "abcdefg#fedcba". How can I remove any text that is after the # symbol (if we did not know what was after it)?
string input = "abcdefg#fedcba";
string result = input.Substring(0, input.IndexOf('#'));
To make solution more flexible you can use a variable:
char borderCharacter = '#';
string input = '...';
string result = input.Substring(0, input.IndexOf(borderCharacter));

Categories

Resources