Difference between LastIndexOf('string') vs LastIndexOf('string')OrdincalComparison [duplicate] - c#

This question already has answers here:
Difference between InvariantCulture and Ordinal string comparison
(9 answers)
Closed 8 years ago.
What is the difference between
This
var ext = name.LastIndexOf(#".");
and This
var ext = name.LastIndexOf(#".", System.StringComparison.Ordinal);

From the StringComparison enum documentation on MSDN:
An operation that uses ordinal sort rules performs a comparison based on the numeric value (Unicode code point) of each Char in the string. An ordinal comparison is fast but culture-insensitive. When you use ordinal sort rules to sort strings that start with Unicode characters (U+), the string U+xxxx comes before the string U+yyyy if the value of xxxx is numerically less than yyyy.
The extra parameter is telling the method how to compare strings. With Ordinal, it's going to use unicode code points for the comparison. Other values of the enum use the culture (invariant or the current one) and can use case insensitive comparison.

Related

Validating if a string is a valid ARGB [duplicate]

This question already has answers here:
How do I get the color from a hexadecimal color code using .NET?
(20 answers)
Closed 1 year ago.
I want to validate if a string is valid ARGB.
E.g. #ffffffff is ARGB 255,255,255,255
How can I validate this using TypeScript and C#?
For either language, you can use regular expressions to ensure a string matches your expectations. Here are some examples in Typescript and C#.
Typescript
const argbRegex = /^#[0-9a-fA-F]{8}$/;
argbRegex.test("#ffffffff"); // true
argbRegex.test("#gggggggg"); // false
C#
Regex argbRegex = new Regex("^#[0-9a-fA-F]{8}$");
argbRegex.IsMatch("#ffffffff"); // True
argbRegex.IsMatch("#gggggggg"); // False
Since you didn't mention it in your question, there are some things that you might want to consider that I did not cover:
Should a 3 character RGB string be a valid ARGB string?
Should a 6 character RGB string be a valid ARGB string?
Do you strictly require the # symbol?
Do you need to extract the decimal values for each ARGB component?
However, if all you need to do is validate the string is strictly in the 8 characters ARGB format, the above examples will work ok.

How to compare case insensitive string using FluentAssertions? C# [duplicate]

This question already has answers here:
Can Fluent Assertions use a string-insensitive comparison for IEnumerable<string>?
(4 answers)
Closed 5 years ago.
How can I easy compare string case insensitive using FluentAssertions?
Something like:
symbol.Should().Be(expectedSymbol, StringComparison.InvariantCultureIgnoreCase);
Edit: Regarding possible duplicate and code:
symbol.Should().BeEquivalentTo(expectedSymbol);
it is comparing using CurrentCulture. And it will brake in situation like Turkish culture. Where
Thread.CurrentThread.CurrentCulture = new CultureInfo("tr-TR", false);
string upper = "in".ToUpper(); // upper == "İN"
"in".Should().BeEquivalentTo("In"); // It will fail
so the part "StringComparison.InvariantCultureIgnoreCase" is crucial here.
You can use
symbol.ToLower().Should().Be(expectedSymbol.ToLower());
OR
Instead of Be use BeEquivalentTo
symbol.Should().BeEquivalentTo(expectedSymbol);
BeEquivalentTo metadata states
Asserts that a string is exactly the same as another string, including any leading or trailing whitespace, with the exception of the casing.

Why do Boolean.TryParse()] and Convert.ToBoolean() evaluate a string differently? [duplicate]

This question already has answers here:
What is the difference between Convert.ToBoolean(string) and Boolean.Parse(string)?
(3 answers)
Closed 6 years ago.
Why do Boolean.TryParse() and Convert.ToBoolean() evaluate a string differently?
I understand how they end up evaluating differently:
Boolean.TryParse() will match (case insensitive) 'true' and 'false'.
Convert.ToBoolean() will match to a whole range of values (example demonstrated in Microsoft doco linked above) which I would consider more natural.
Its the reasoning behind the difference I dont understand.
There are a couple of discussions touching on this subject which don't seem to address this particular question.
It's in the method/class names.
Convert -> you already have some value, you convert it to another type. e.g. you have value 1 which can be converted to true.
Parse -> you have the value as a string and you parse it.

How to compare strings at C# like in SQL server case insensitive and accent insensitive [duplicate]

This question already has answers here:
How to compare strings with case insensitive and accent insensitive
(2 answers)
How can I do a case insensitive string comparison?
(9 answers)
Closed 8 years ago.
Alright here 4 words which are equal at SQL server Latin1_General_100_CI_AI collation which is case insensitive and accent insensitive
taraflı
TaraFLI
TARaFLİ
Tarafli
However i could not find a way to compare these as equal at C# .net 4.5.1
Are there any way to make such comparsion like in SQL server ?
I checked this thread as well : Ignoring accented letters in string comparison
Event RemoveDiacritics method at that thread fails
This compares all those strings as equal:
string.Compare(s1,s2,
CultureInfo.InvariantCulture,
CompareOptions.IgnoreNonSpace | CompareOptions.IgnoreCase)

format decimal number using ToString [duplicate]

This question already has answers here:
Using String Format to show decimal up to 2 places or simple integer
(18 answers)
Closed 9 years ago.
If I got decimal number like 14.50 and I want to be represented like decimal 10.2
0000000014.50
how can I do this?
Thank you
Use custom numeric format string:
var value = 14.50m;
string valueString = value.ToString("0000000000.00");
0 is a placeholder: Replaces the zero with the corresponding digit if one is present; otherwise, zero appears in the result string.
If you don't have an issue with the data type being converted to string then you could use Padding in c#.
Refer the link below :
http://msdn.microsoft.com/en-us/library/66f6d830(v=vs.100).aspx

Categories