Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I know that DateTime parsing can be tricky, but since there are a lot of mechanisms are relying on DateTime.Parse() especially its DateTime.Parse(string) overload, I think it makes sense to understand how does it work under the hood and how does it behave on different inputs.
What we know:
MSDN states when you use the DateTime.Parse(String) overload, the formatting is derived from the CurrentThread.Culture, however it draws the attention:
culture-specific data can change (and it did) between the different versions of the framework
the culture-specific data can be overridden by the OS settings
DateTime.Parse() tries to be smart
Because of these it's a bit hard for me to predict what will be result when somebody calls this function on different user inputs.
Even when I specify a culture, DateTime.Parse can recognize strings as valid DateTimes what you might not think to recognize. For example, all the following dates are valid - here are some of my findings:
var at = new System.Globalization.CultureInfo("de-AT", false);
System.Threading.Thread.CurrentThread.CurrentCulture = at;
// it doesn't care about the order:
DateTime.Parse("21.12.2020", at).Dump();
DateTime.Parse("2020.12.31", at).Dump();
// it accepts multiple separators:
DateTime.Parse("2020,12,31", at).Dump();
DateTime.Parse("2020/12/31", at).Dump();
DateTime.Parse("2020 12 31", at).Dump();
// it accepts multiple separators even in a single string:
DateTime.Parse("1999/12-31", at).Dump();
// year must consist at least 3 digits:
DateTime.Parse("100/12-31", at).Dump(); // this works
//DateTime.Parse("99/12-31", at).Dump(); -> this doesn't
DateTime.Parse("001/12-31", at).Dump(); // but this works again (3 digits)
// trimming (well, MSDN mentions this)
DateTime.Parse(" 100/12,31 ", at).Dump();
For me it's not so clear what's going on here. The separators / and , are not even mentioned in the DateTimeFormatInfo.CurrentInfo so I have no idea where did it come from. Are these separators hardcoded in DateTime.Parse? I tried to read the disassembled code, but it was a bit complex for me.
Is there any easy way to summarize what happens and which formats are supported?
I know that in a "real life example" if I have to parse a DateTime with a given format, I should use ParseExact instead, but since there are a lot of stuff relying on this (such as ASP.NET MVC model binding) I think it worths a question what it does exactly - and why does it recognize "100/3.14" as a valid DateTime instead of a division :)
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 months ago.
Improve this question
I need to convert a string to decimal with specific format in C#. This string can be in different formats. For example it can be: 20 or 20.5.
I need it to convert to xx.xxx. Is there is a method to do this?
C# decimal values are binary. They do not have a human-readable format and can NEVER have a human-readable format. Anything you see otherwise is a convenience provided by the debugger or other tooling. If you need a decimal formatted in a certain way, what you really need is a string.
That said, the decimal type is a good intermediary to be sure you get the correct desired string output: first Parse() the original string value to a decimal, then convert from a decimal to the final formatted string result using the decimal's ToString() method.
Finally, it's important to understand cultural and internationalization issues mean converting between strings and true numeric values is far more error-prone and slow than we'd like to believe. It's something to avoid. Therefore the best strategy is usually parsing a value into an unformatted decimal as quickly as possible, and then keeping it there as long as possible — until the last possible moment before you need to format it for output.
You can specify the format like the below:
string t = "2020.5";
var d = decimal.Parse(t).ToString("00.000");
If your string contains any special character you need to sanitize it before you parse it. E.g. if the string contains space you can replace it with empty char like this:
t = t.Replace(" ", "");
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
what is CultureInfo.CurrentCulture in c# , actually unable to find the answer , why we use it , for what purpose , please explain in easy words
Please Explain in easy words ! Why we use this . On the other hand , if i donot use this , results are still same . So why what is the purpose of this ?
You can think of CultureInfo as a collection of settings for a particular geographical region.
A CultureInfo object specifies how a piece of information is rendered for a particular culture. It contains settings for writing directions and other related settings, dates formatting, calender, currency etc.
With CultureInfo.CurrentCulture the culture context for the current thread is given. This is used by functions like ToString() to define the string format of a DateTime value, when no explict culture is provided as a function parameter.
CultureInfo contains information on how to deal with data representation in different countries.
As for example date format (in my country it's DD/MM/YYYY, in the US it's MM-DD-YYYY) or floating point number representation (3,14 vs 3.14).
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
Hello experts, I have to generate series of folders from a TextBox into specified location.I am having two textboxes to specify the limit of folders(say 30 folders).The problem am facing is that the folder names that i will be providing are alpha-numeric(say 121cs3h101) .
How to set limit when i provide an alpha-numeric values?
(For example: i provide textbox1=12cs3h101 and textbox2=12cs3h131 , i need the series limit to be generated). I am working with visual studio 2013 in c# windows form application. Thanks in advance.
ok I will try to give you a lead.
To parse a string or find specific characters one can use RegEx.Match or a simler method called String.Split. In both cases you have to be aware how your string is structured and how it can vary. The limits of variation are very important.
If as you say the beginning is always"12cs3h" you can either split the string at the character 'h'.
string[] sa = s.Split('h');
Or you can even use the index of 'h' (since the length seems to be fixed) and take the rest of the string to get the numbers.
int index = s.IndexOf('h');
The rest is up to you, ... convert, enumerate and so on.
EDIT: There is a nice method that does the enumeration job for you: Enumerable.Range Good luck
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
In one of the interview I was asked to test a method. The details are mentioned below. Though I could answer , still they were expecting some more test cases. Am I missing any scenarios here ?
string concatenatefunc(strin1,string2).
{
//returns concatenation
}
This method accepts two string parameters and returns the concatenation. No other details are mentioned. I need to test this method and i have written below scenarios/unit test cases:
1.Pass empty parameters and see empty string is returned
2.Pass valid non empty strings and see the string returned is correct or not.
3.Pass the special characters in the both the parameters and test the response.
4.Pass integers and test the response.
5.pass large strings(not sure what we can give as the max length) and test the response.
....
Anything to add here?
You're not testing for null parameters.
Test for multi language support. An area that many developers fail to test which can give a guy like me problems. I have the danish character 'ø' in my name. This has been a problem in my web based interactions with several VERY BIG companies including software companies, preventing me from logins, accounts, payments etc.
Number 5 will really depend on the size of available resources so testing that might be problematic.
You could add tests for either parameter being empty and see if the non-empty parameter is returned
Same for Passing integers, test if either parameter is integer
Then you could start mixing them empty/integer, non-empty/integer, etc. and reverse
Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 9 years ago.
Improve this question
How do I validate a UK phone number in C# using a regex?
The regex in the accepted answer does not match all valid UK numbers, as it is too restricive (additional number ranges have been opened up in the meanwhile such as 0203, which it sees as invalid).
UK phone numbers follow fairly simple rules:
They can be either 10 or 11 digits long (with the exception of some special numbers, but you're unlikely to need to validate those)
They consist of an area code followed by a local number. The area code varies in length between three and five digits, and the local portion of the number takes up the remaining length of the 10 or 11 digits. For all practical purposes, no-one ever quotes just the local portion of their number, so you can ignore the distinction now, except for how it affects formatting.
They start with zero.
The second digit can be anything. Currently no valid numbers start with 04 or 06, but there's nothing stopping these ranges coming into use in the future. (03 has recently been brought into use)
They can be formatted with a set of brackets and with spaces (one or more, in varying positions), but those are all entirely optional.
Therefore, a basic working expression for UK phone numbers could look like this:
/^\(?0( *\d\)?){9,10}$/
This will check for 10 or 11 digit numbers, starting with a zero, with formatting spaces between any of the digits, and optionally a set of brackets for the area code.
(and yes, this would allow mis-matched brackets, as I'm not checking that there's only one closing bracket. Enforcing this would make the expression a lot more complex, and I don't have time for this right now, but feel free to add this if you wish)
By the way, in case you want to do additional filtering, you might want to also note the following rules:
Numbers starting 08, 09 and 070 are special price numbers, and would not generally be given as private numbers, so can be excluded if validating a private number.
07 numbers are mobile (except 070; see above) so can be excluded if you're specifically validating for a landline.