Why indexof search text not correct? [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 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

Related

What is the problem with char equal to ","? [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 last month.
Improve this question
string odp = "2,1";
bool abs = odp[1].Equals(",");
When I do Console.Writeline(odp[i]) it shows "," symbol so why does it equal to false?
I tried using == instead but it doesn't work either. Thanks in advance
A Char is encapsulated in single quotation marks ',' in C# (a String uses double quotation marks ",").
Try the below instead:
string odp = "2,1";
bool abs = odp[1].Equals(','); // true

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.

Remove '\' from a string reading from a CSV file - 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 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.

ReplaceTest(int i) not all code paths return a value [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 5 years ago.
Improve this question
public string ReplaceTest(int i)
{
string rep = this.textBox3.Text;
string reped = rep.Replace("sir, this.textBox3.Text");
}
Like this? this is what i want to do but another error comes up not all code paths return a value
string.Replace() takes two arguments:
the string to replace
the string to use instead
If you want to remove "sir", it means to replace it with an empty string:
string reped = rep.Replace("sir", string.Empty);

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.

Categories