text stringlengths 0 13.4k |
|---|
strings manipulation: |
var sb = new StringBuilder(someInitialString); |
foreach(var str in manyManyStrings) |
{ |
sb.Append(str); |
} |
var finalString = sb.ToString(); |
GoalKicker.com – .NET Framework Notes for Professionals |
8 |
Chapter 3: DateTime parsing |
Section 3.1: ParseExact |
var dateString = "2015-11-24"; |
var date = DateTime.ParseExact(dateString, "yyyy-MM-dd", null); |
Console.WriteLine(date); |
11/24/2015 12:00:00 AM |
Note that passing CultureInfo.CurrentCulture as the third parameter is identical to passing null. Or, you can |
pass a specific culture. |
Format Strings |
Input string can be in any format that matches the format string |
var date = DateTime.ParseExact("24|201511", "dd|yyyyMM", null); |
Console.WriteLine(date); |
11/24/2015 12:00:00 AM |
Any characters that are not format specifiers are treated as literals |
var date = DateTime.ParseExact("2015|11|24", "yyyy|MM|dd", null); |
Console.WriteLine(date); |
11/24/2015 12:00:00 AM |
Case matters for format specifiers |
var date = DateTime.ParseExact("2015-01-24 11:11:30", "yyyy-mm-dd hh:MM:ss", null); |
Console.WriteLine(date); |
11/24/2015 11:01:30 AM |
Note that the month and minute values were parsed into the wrong destinations. |
Single-character format strings must be one of the standard formats |
var date = DateTime.ParseExact("11/24/2015", "d", new CultureInfo("en-US")); |
var date = DateTime.ParseExact("2015-11-24T10:15:45", "s", null); |
var date = DateTime.ParseExact("2015-11-24 10:15:45Z", "u", null); |
Exceptions |
ArgumentNullException |
GoalKicker.com – .NET Framework Notes for Professionals |
9 |
var date = DateTime.ParseExact(null, "yyyy-MM-dd", null); |
var date = DateTime.ParseExact("2015-11-24", null, null); |
FormatException |
var date = DateTime.ParseExact("", "yyyy-MM-dd", null); |
var date = DateTime.ParseExact("2015-11-24", "", null); |
var date = DateTime.ParseExact("2015-0C-24", "yyyy-MM-dd", null); |
var date = DateTime.ParseExact("2015-11-24", "yyyy-QQ-dd", null); |
// Single-character format strings must be one of the standard formats |
var date = DateTime.ParseExact("2015-11-24", "q", null); |
// Format strings must match the input exactly* (see next section) |
var date = DateTime.ParseExact("2015-11-24", "d", null); // Expects 11/24/2015 or 24/11/2015 for |
most cultures |
Handling multiple possible formats |
var date = DateTime.ParseExact("2015-11-24T10:15:45", |
new [] { "s", "t", "u", "yyyy-MM-dd" }, // Will succeed as long as input matches one of these |
CultureInfo.CurrentCulture, DateTimeStyles.None); |
Handling culture differences |
var dateString = "10/11/2015"; |
var date = DateTime.ParseExact(dateString, "d", new CultureInfo("en-US")); |
Console.WriteLine("Day: {0}; Month: {1}", date.Day, date.Month); |
Day: 11; Month: 10 |
date = DateTime.ParseExact(dateString, "d", new CultureInfo("en-GB")); |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.