Validating if a string is a valid ARGB [duplicate] - c#

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.

Related

C# Selenium WebDriver Element Text Match text containing Random 8 digit Hex Code using Regex [duplicate]

This question already has answers here:
C# Find a matching string by regex
(5 answers)
How do I match an entire string with a regex?
(8 answers)
Closed 3 years ago.
I have a table element 'ClassTable' which contains text as follows:
"Class Name\r\nCreated By\r\nClass Type\r\nClass Code\r\nMathematics Grade 6\r\nTeacher1\r\nSchool\r\n7BD6231E\r\nManage"
The above text contains a random 8 digit Hex code 7BD6231E which changes after each validation.
Is there any way to match the whole text using Selenium WebDriver or C# with Regex like [A-F0-9]{8} for the random hex code so that my validation passes every time without any issues as the Hex Code will change every time.
I have tried the Regex [A-F0-9]{8} with ClassTable.Text.Contains function as follows but I guess Regex is not supported in Text.Contains function.
ClassTable.Text.Contains("Class Name\r\nCreated By\r\nClass Type\r\nClass Code\r\nMathematics Grade 6\r\nTeacher1\r\nSchool\r\n[A-F0-9]{8}\r\nManage");
The suggested solutions should match the whole text including the random Hex Code. Thanks.

C# - wrong string to single conversion [duplicate]

This question already has answers here:
How do I parse a string with a decimal point to a double?
(19 answers)
Closed 6 years ago.
I am writing a program to get data from microcontroller to PC. The data is in float format. I tried to convert the string into float using Convert.ToSingle(string), but the conversion result is wrong:
"0.11" is converted to 11, sometimes 12.
"0.10" is converted to 10. etc
As you can see, it is losing the leading 0. , which is unexpected. How could this happen?
Your problem is culture specific. In some cultures float numbers are separated by a , and in some they are separated by a .
In your case
String a = "0,11";
Convert.ToSingle(a)
should result in your desired outcome of 0,11.
So you should explicitly specify a relevant culture that uses . as decimal separator. One possibility is the invariant culture which is based on the English language.
Try the following:
String a = "0.11";
Convert.ToSingle(a, CultureInfo.InvariantCulture)

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

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.

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

How to represent comma's in string? [duplicate]

This question already has answers here:
.NET String.Format() to add commas in thousands place for a number
(23 answers)
Closed 9 years ago.
I have a integer like this i.e 3356890. I'm converting it to string and showing on screen.
Now I want to display like this 3,356,890.
How to do?
You can use:
value = 1234567890;
Console.WriteLine(value.ToString("0,0", CultureInfo.InvariantCulture));
// Displays 1,234,567,890
However, for the purpose of internationalization and localization, it's probably best to allow the user's current culture settings to determine how to format the number.
Further Reading
Custom Numeric Format Strings
Take a look please here,
Number string format
string res = string.Format(CultureInfo.InvariantCulture, "{0:#,##0}", 3356890);
int val = 3356890;
string valString = val.ToString("#,##0")

Categories