Skip to content

Commit 4377a13

Browse files
authored
System.String F# snippets (#7875)
* String F# snippets * Delete fs.fsproj * StringComparison, StringSplitOptions snippet refs
1 parent 979ddb0 commit 4377a13

File tree

239 files changed

+7097
-7
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

239 files changed

+7097
-7
lines changed
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
module FormatExample2
2+
3+
// <Snippet2>
4+
open System
5+
6+
type CustomerFormatter() =
7+
interface IFormatProvider with
8+
member this.GetFormat(formatType) =
9+
if formatType = typeof<ICustomFormatter> then
10+
this
11+
else
12+
null
13+
14+
interface ICustomFormatter with
15+
member this.Format(format, arg, formatProvider: IFormatProvider) =
16+
if this.Equals formatProvider |> not then
17+
null
18+
else
19+
let format =
20+
if String.IsNullOrEmpty format then "G"
21+
else format.ToUpper()
22+
23+
let customerString =
24+
let s = string arg
25+
if s.Length < 8 then
26+
s.PadLeft(8, '0')
27+
else s
28+
29+
match format with
30+
| "G" ->
31+
customerString.Substring(0, 1) + "-" +
32+
customerString.Substring(1, 5) + "-" +
33+
customerString.Substring 6
34+
| "S" ->
35+
customerString.Substring(0, 1) + "/" +
36+
customerString.Substring(1, 5) + "/" +
37+
customerString.Substring 6
38+
| "P" ->
39+
customerString.Substring(0, 1) + "." +
40+
customerString.Substring(1, 5) + "." +
41+
customerString.Substring 6
42+
| _ ->
43+
raise (FormatException $"The '{format}' format specifier is not supported.")
44+
45+
let acctNumber = 79203159
46+
String.Format(CustomerFormatter(), "{0}", acctNumber)
47+
|> printfn "%s"
48+
String.Format(CustomerFormatter(), "{0:G}", acctNumber)
49+
|> printfn "%s"
50+
String.Format(CustomerFormatter(), "{0:S}", acctNumber)
51+
|> printfn "%s"
52+
String.Format(CustomerFormatter(), "{0:P}", acctNumber)
53+
|> printfn "%s"
54+
try
55+
String.Format(CustomerFormatter(), "{0:X}", acctNumber)
56+
|> printfn "%s"
57+
with :? FormatException as e ->
58+
printfn $"{e.Message}"
59+
60+
// The example displays the following output:
61+
// 7-92031-59
62+
// 7-92031-59
63+
// 7/92031/59
64+
// 7.92031.59
65+
// The 'X' format specifier is not supported.
66+
// </Snippet2>
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
module format4
2+
3+
// <Snippet4>
4+
open System
5+
6+
let formatString =
7+
" {0,10} ({0,8:X8})\nAnd {1,10} ({1,8:X8})\n = {2,10} ({2,8:X8})"
8+
9+
let value1 = 16932
10+
let value2 = 15421
11+
String.Format(formatString, value1, value2, value1 &&& value2)
12+
|> printfn "%s"
13+
// The example displays the following output:
14+
// 16932 (00004224)
15+
// And 15421 (00003C3D)
16+
// = 36 (00000024)
17+
// </Snippet4>
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
module format5
2+
3+
open System
4+
5+
// <Snippet5>
6+
let date1 = DateTime(2009, 7, 1)
7+
let hiTime = TimeSpan(14, 17, 32)
8+
let hiTemp = 62.1m
9+
let loTime = TimeSpan(3, 16, 10)
10+
let loTemp = 54.8m
11+
12+
String.Format("Temperature on {0:d}:\n{1,11}: {2} degrees (hi)\n{3,11}: {4} degrees (lo)", date1, hiTime, hiTemp, loTime, loTemp)
13+
|> printfn "%s\n"
14+
15+
String.Format("Temperature on {0:d}:\n{1,11}: {2} degrees (hi)\n{3,11}: {4} degrees (lo)", [| date1 :> obj; hiTime; hiTemp; loTime; loTemp |])
16+
|> printfn "%s"
17+
// The example displays output like the following:
18+
// Temperature on 7/1/2009:
19+
// 14:17:32: 62.1 degrees (hi)
20+
// 03:16:10: 54.8 degrees (lo)
21+
// Temperature on 7/1/2009:
22+
// 14:17:32: 62.1 degrees (hi)
23+
// 03:16:10: 54.8 degrees (lo)
24+
// </Snippet5>
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
module format7
2+
3+
open System
4+
5+
// <Snippet7>
6+
let birthdate = DateTime(1993, 7, 28)
7+
let dates =
8+
[ DateTime(1993, 8, 16)
9+
DateTime(1994, 7, 28)
10+
DateTime(2000, 10, 16)
11+
DateTime(2003, 7, 27)
12+
DateTime(2007, 5, 27) ]
13+
14+
for dateValue in dates do
15+
let interval = dateValue - birthdate
16+
// Get the approximate number of years, without accounting for leap years.
17+
let years = (int interval.TotalDays) / 365
18+
// See if adding the number of years exceeds dateValue.
19+
if birthdate.AddYears years <= dateValue then
20+
String.Format("You are now {0} years old.", years)
21+
else
22+
String.Format("You are now {0} years old.", years - 1)
23+
|> printfn "%s"
24+
// The example displays the following output:
25+
// You are now 0 years old.
26+
// You are now 1 years old.
27+
// You are now 7 years old.
28+
// You are now 9 years old.
29+
// You are now 13 years old.
30+
// </Snippet7>
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
module format_paramarray1
2+
3+
// <Snippet10>
4+
open System
5+
6+
type CityInfo =
7+
{ Name: string
8+
Population: int
9+
Area: Decimal
10+
Year: int }
11+
12+
let showPopulationData city =
13+
let args: obj[] = [| city.Name; city.Year; city.Population; city.Area |]
14+
String.Format("{0} in {1}: Population {2:N0}, Area {3:N1} sq. feet", args)
15+
|> printfn "%s"
16+
17+
{ Name = "New York"; Population = 8175133; Area = 302.64m; Year = 2010 }
18+
|> showPopulationData
19+
20+
21+
{ Name = "Seattle"; Population = 608660; Area = 83.94m; Year = 2010 }
22+
|> showPopulationData
23+
24+
// The example displays the following output:
25+
// New York in 2010: Population 8,175,133, Area 302.6 sq. feet
26+
// Seattle in 2010: Population 608,660, Area 83.9 sq. feet
27+
// </Snippet10>
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
module formatexample4
2+
3+
// <Snippet6>
4+
open System
5+
open System.Collections.Generic
6+
7+
let temperatureInfo = Dictionary<DateTime, float>()
8+
temperatureInfo.Add(DateTime(2010, 6, 1, 14, 0, 0), 87.46)
9+
temperatureInfo.Add(DateTime(2010, 12, 1, 10, 0, 0), 36.81)
10+
11+
printfn $"Temperature Information:\n"
12+
for item in temperatureInfo do
13+
String.Format("Temperature at {0,8:t} on {0,9:d}: {1,5:N1}°F", item.Key, item.Value)
14+
|> printfn "%s"
15+
// The example displays output like the following:
16+
// Temperature Information:
17+
//
18+
// Temperature at 2:00 PM on 6/1/2010: 87.5°F
19+
// Temperature at 10:00 AM on 12/1/2010: 36.8°F
20+
// </Snippet6>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
module formatoverload1
2+
3+
// <Snippet8>
4+
open System
5+
6+
let dat = DateTime(2012, 1, 17, 9, 30, 0)
7+
let city = "Chicago"
8+
let temp = -16
9+
String.Format("At {0} in {1}, the temperature was {2} degrees.", dat, city, temp)
10+
|> printfn "%s"
11+
// The example displays output like the following:
12+
// At 1/17/2012 9:30:00 AM in Chicago, the temperature was -16 degrees.
13+
// </Snippet8>
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
module formatoverload2
2+
3+
open System
4+
5+
// <Snippet9>
6+
// Create a list of 5-tuples with population data for three U.S. cities, 1940-1950.
7+
let cities =
8+
[ "Los Angeles", DateTime(1940, 1, 1), 1504277, DateTime(1950, 1, 1), 1970358
9+
"New York", DateTime(1940, 1, 1), 7454995, DateTime(1950, 1, 1), 7891957
10+
"Chicago", DateTime(1940, 1, 1), 3396808, DateTime(1950, 1, 1), 3620962
11+
"Detroit", DateTime(1940, 1, 1), 1623452, DateTime(1950, 1, 1), 1849568 ]
12+
13+
// Display header
14+
String.Format("{0,-12}{1,8}{2,12}{1,8}{2,12}{3,14}\n", "City", "Year", "Population", "Change (%)")
15+
|> printfn "%s"
16+
17+
for name, year1, pop1, year2, pop2 in cities do
18+
String.Format("{0,-12}{1,8:yyyy}{2,12:N0}{3,8:yyyy}{4,12:N0}{5,14:P1}",
19+
name, year1, pop1, year2, pop2,
20+
double (pop2 - pop1) / double pop1)
21+
|> printfn "%s"
22+
// The example displays the following output:
23+
// City Year Population Year Population Change (%)
24+
//
25+
// Los Angeles 1940 1,504,277 1950 1,970,358 31.0 %
26+
// New York 1940 7,454,995 1950 7,891,957 5.9 %
27+
// Chicago 1940 3,396,808 1950 3,620,962 6.6 %
28+
// Detroit 1940 1,623,452 1950 1,849,568 13.9 %
29+
// </Snippet9>
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
module formatsyntax1
2+
3+
// <Snippet12>
4+
open System
5+
6+
String.Format("{0,-10:C}", 126347.89m)
7+
|> printfn "%s"
8+
// </Snippet12>

snippets/fsharp/System/FormatException/Overview/fs.fsproj

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,33 @@
88
<Compile Include="example1.fs" />
99
<Compile Include="example2.fs" />
1010
<Compile Include="example3.fs" />
11+
<Compile Include="format_paramarray1.fs" />
12+
<Compile Include="format4.fs" />
13+
<Compile Include="format5.fs" />
14+
<Compile Include="format7.fs" />
15+
<Compile Include="FormatExample2.fs" />
16+
<Compile Include="FormatExample4.fs" />
17+
<Compile Include="formatoverload1.fs" />
18+
<Compile Include="formatoverload2.fs" />
19+
<Compile Include="formatsyntax1.fs" />
1120
<Compile Include="iformattable1.fs" />
1221
<Compile Include="iformattable2.fs" />
1322
<Compile Include="iformattable3.fs" />
1423
<Compile Include="iformattable4.fs" />
24+
<Compile Include="interceptor2.fs" />
25+
<Compile Include="qa-interpolated1.fs" />
26+
<Compile Include="qa-interpolated2.fs" />
1527
<Compile Include="qa1.fs" />
1628
<Compile Include="qa2.fs" />
1729
<Compile Include="qa3.fs" />
30+
<Compile Include="qa11.fs" />
31+
<Compile Include="qa21.fs" />
32+
<Compile Include="qa26.fs" />
33+
<Compile Include="qa27.fs" />
34+
<Compile Include="qa28.fs" />
35+
<Compile Include="qa29.fs" />
36+
<Compile Include="starting1.fs" />
37+
<Compile Include="starting2.fs" />
38+
<Compile Include="starting3.fs" />
1839
</ItemGroup>
1940
</Project>

0 commit comments

Comments
 (0)