Remove '\' from a string reading from a CSV file - C# [closed] - c#

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
I'm running into a problem where I'm reading a CSV file and I don't see any '\' in the file.
When I split the string by ',' and store it into a string variable, this is what the string looks like.
"\"3040063816\""
The whole entire string is this before spliting
"\"3040063816\",\"123456789\",\"0.00\",\"0.00\",\"-95.99\",\"10/28/19\",\"09:04:11\",\"1\"\r"
How do you remove '\' from the string?
Because when I try to convert/parse the number into an INT, it gives me an error.
I've tried to replace(#"\", string.empty) and it doesn't work.

Try the following. This should get you going.
string input = "\"3040063816\",\"123456789\",\"0.00\",\"0.00\",\"-95.99\",\"10/28/19\",\"09:04:11\",\"1\"\r";
List<string> stringArr = input.Split(',').Select(x => x.Replace(#"""", "")).ToList();
Console.WriteLine(Convert.ToInt64(stringArr[0])); // This is a large number. Has to be int64.
Console.WriteLine(int.Parse(stringArr[1]));
Console.WriteLine(float.Parse(stringArr[2]));
Console.WriteLine(float.Parse(stringArr[3]));
Console.WriteLine(float.Parse(stringArr[4]));
Console.WriteLine(DateTime.Parse(stringArr[5]));
Console.WriteLine(DateTime.Parse(stringArr[6]));
Console.WriteLine(int.Parse(stringArr[7]));
Output
3040063816
123456789
0
0
-95.99
10/28/2019 12:00:00 AM
12/30/2019 9:04:11 AM
1
Each of the strings inside your string has double quotes in it. To remove that and make it a number representation, remove the quotes from each of the string after splitting it.
Review this post for conversion of large numbers and which format they can be converted to.

Related

Why indexof search text not correct? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I am using IndexOf to find a character in text. My text include some special characters:
This is my code:
string text = "123<g>ٕ</g>";
int iPosEnd = text.IndexOf(">");
Result:
iPosEnd =10
Why IndexOf did not find character at Position 5?
string.IndexOf(string) does culture-sensitive search. This means it may combine characters when comparing chars. You'll find more info about string comparison in these documentation page:
Compare strings in .NET
Best practices for comparing strings in .NET
String comparisons are harder than it seems
If you want this method to return 5, you can use the char overload or use StringComparison.Ordinal
string text = "123<g>ٕ</g>";
int iPosEnd1 = text.IndexOf(">"); // 10
int iPosEnd2 = text.IndexOf('>'); // 5
int iPosEnd3 = text.IndexOf(">", StringComparison.Ordinal); // 5

How to use this regex filter string in regex in C#? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I have the following regex string "(\"\--[a-zA-Z]+\")" but I can not use it since it uses a double quotation.
What are the escape characters I could try here?
I want to use as follows in C# code to filter the "KernalSize" from the following string :
parser.add_argument("--KernalSize", default = 6, help = "Noise removal")
Following filter (\"\--[a-zA-Z]+\") returns "--KernalSize". However, when I try to set the filter in C# code, it failed due to a double quotation sign within the string.
const string ParamNameFilter = #"(\"\--[a-zA-Z]+\")";
so your regex string (\"\--[a-zA-Z]+\") should match strings like "--AnythingAlpha"?
in reality the backslash before one of the - is not even necessary, so it begs a question if there is any type of a typo there, as it is equivalent to just (\"--[a-zA-Z]+\") or even a simpler (\"--[a-z]+\") with case-insensitive flag.
Now it's just about representing this string in C# strings:
(\"\--[a-zA-Z]+\") => to one of
#"(\""\--[a-zA-Z]+\"")"
"(\\\"\\--[a-zA-Z]+\\\")"
you can test them with something like:
public static void Main (string[] args)
{
Console.WriteLine(#"(\""\--[a-zA-Z]+\"")");
Console.WriteLine("(\\\"\\--[a-zA-Z]+\\\")");
}
The two quoting options are
Double quote "(\"--[a-zA-Z]+\")"
Verbatim #"(""--[a-zA-Z]+"")"
Note inside a verbatim string, the double quote is escaped by double quoting it.

assigning variable in c# fails [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
I am assigning variable to another variable but it throws error
string conf = "[32882914,32877390,32877377,32882917,32882922,32882926,32877379,32882887,32882891,32877382,32882897,32882902,32882907,32882909]"
int sku = 34444
string param1 = "{\"SameDayDeliveryStoreNumber\":0,\"styleSkus\":[{\"StyleId\":{0},\"SkuIds\":{1}}],\"RefreshSameDayDeliveryStore\":true}",sku,conf;
Error:
unexpected value string
varibale is already defined sku,conf
how to use variables in another variable in c#
You're missing a key part of the syntax, you need string.Format. Your code should instead read
string param1 = String.Format("{{\"SameDayDeliveryStoreNumber\":0,\"styleSkus\":[{{\"StyleId\":{0},\"SkuIds\":{1}}}],\"RefreshSameDayDeliveryStore\":true}}",sku,conf);
There are other ways to do this that you can use, for example you can use +
string param1 = "{\"SameDayDeliveryStoreNumber\":0,\"styleSkus\":[{\"StyleId\":" + sku + ",\"SkuIds\":" + conf + "}],\"RefreshSameDayDeliveryStore\":true}";
Or you could use the more similar interpolation
string param1 = $"{{\"SameDayDeliveryStoreNumber\":0,\"styleSkus\":[{{\"StyleId\":{sku},\"SkuIds\":{conf}}}],\"RefreshSameDayDeliveryStore\":true}}";
Notice in the first and third examples there are three curled brackets at the end of the second parameter instead of two, this is because two would escape to a literal "}" character, to instead get the syntax } followed by the literal "}", three must be used
To add on to what Alfie said, the back slashes (\) should also be escaped in C# with double back slashes.
"{{\\"SameDayDeliveryStoreNumber\\"

How should I separate a string which contains slashes with a single slash? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
This is a sting like this:
string a = "C:\folder1\folder2\folder3";
I want to separate string a with '\', so write like this:
List<string> result = a.Split('\\').ToList();
But, result only contains ONE member:
{C: older1 older2 older3}
I want to have 4 members in result:
{C:,folder1,folder2,folder3}
So, how shold I do it?
The problem is that your sample string does not contain backslashes.
This string contains three:
string a = "C:\\folder1\\folder2\\folder3";
or this:
string a = #"C:\folder1\folder2\folder3"; // google: verbatim string literal
\f is an escape sequence for formfeed.
Define your string as
string a = #"C:\folder1\folder2\folder3";
so it does not takes backslash as special char.

Split string is not working in WPF (C#) [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
When using the following code,
string text = comboLanguages.SelectedItem.ToString();
string[] split = text.Split("\t", 1);
I get an error (below) on the 2nd line.
Error 1 The best overloaded method match for 'string.Split(params char[])' has some invalid arguments MainWindow.xaml.cs 46 30
Can anyone please tell me what I'm doing wrong?
String.Split takes a char parameter, not a string. You needed to write:
string text = comboLanguages.SelectedItem.ToString();
string[] split = text.Split(new char[] {'\t'}, 1);
You need to change the code to:
string[] split = text.Split(new char[] { '\t' }, 1);
Note that the error is telling you exactly what the problem is.
If you are going to call this code regularly you might want to move the array declaration outside of the call to Split.

Categories