Skip to content

Commit 55cf9a0

Browse files
authored
StringBuilder F# snippets (dotnet#8866)
1 parent e77c6c5 commit 55cf9a0

Some content is hidden

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

44 files changed

+1569
-0
lines changed
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
// This sample demonstrates how to use each member of the StringBuilder class.
2+
//<Snippet2>
3+
open System.Text
4+
5+
let constructorWithNothing () =
6+
// Initialize a new StringBuilder object.
7+
//<Snippet1>
8+
let stringBuilder = StringBuilder()
9+
//</Snippet1>
10+
()
11+
12+
let constructorWithCapacity () =
13+
// Initialize a new StringBuilder object with the specified capacity.
14+
//<Snippet3>
15+
let capacity = 255
16+
let stringBuilder = StringBuilder capacity
17+
//</Snippet3>
18+
()
19+
20+
let constructorWithString () =
21+
// Initialize a new StringBuilder object with the specified string.
22+
//<Snippet4>
23+
let initialString = "Initial string."
24+
let stringBuilder = StringBuilder initialString
25+
//</Snippet4>
26+
()
27+
28+
let constructorWithCapacityAndMax () =
29+
// Initialize a new StringBuilder object with the specified capacity
30+
// and maximum capacity.
31+
//<Snippet5>
32+
let capacity = 255
33+
let maxCapacity = 1024
34+
let stringBuilder = StringBuilder(capacity, maxCapacity)
35+
//</Snippet5>
36+
()
37+
38+
let constructorWithSubstring () =
39+
// Initialize a new StringBuilder object with the specified substring.
40+
//<Snippet6>
41+
let initialString = "Initial string for stringbuilder."
42+
let startIndex = 0
43+
let substringLength = 14
44+
let capacity = 255
45+
46+
let stringBuilder =
47+
StringBuilder(initialString, startIndex, substringLength, capacity)
48+
//</Snippet6>
49+
()
50+
51+
let constructorWithStringAndMax () =
52+
// Initialize a new StringBuilder object with the specified string
53+
// and maximum capacity.
54+
//<Snippet7>
55+
let initialString = "Initial string. "
56+
let capacity = 255
57+
let stringBuilder = StringBuilder(initialString, capacity)
58+
//</Snippet7>
59+
60+
// Ensure that appending the specified string will not exceed the
61+
// maximum capacity of the object.
62+
//<Snippet8>
63+
let phraseToAdd = "Sentence to be appended."
64+
65+
if (stringBuilder.Length + phraseToAdd.Length) <= stringBuilder.MaxCapacity then
66+
stringBuilder.Append phraseToAdd |> ignore
67+
//</Snippet8>
68+
69+
// Retrieve the string value of the StringBuilder object.
70+
//<Snippet9>
71+
let builderResults = stringBuilder.ToString()
72+
// let builderResults = string stringBuilder
73+
//</Snippet9>
74+
75+
// Retrieve the last 10 characters of the StringBuilder object.
76+
//<Snippet10>
77+
let sentenceLength = 10
78+
let startPosition = stringBuilder.Length - sentenceLength
79+
let endPhrase = stringBuilder.ToString(startPosition, sentenceLength)
80+
//</Snippet10>
81+
82+
// Retrieve the last character of the StringBuilder object.
83+
//<Snippet11>
84+
let lastCharacterPosition = stringBuilder.Length - 1
85+
let lastCharacter = stringBuilder[lastCharacterPosition]
86+
//</Snippet11>
87+
()
88+
89+
constructorWithNothing ()
90+
constructorWithCapacity ()
91+
constructorWithString ()
92+
constructorWithCapacityAndMax ()
93+
constructorWithSubstring ()
94+
constructorWithStringAndMax ()
95+
96+
printfn "This sample completed successfully press Enter to exit."
97+
stdin.ReadLine() |> ignore
98+
99+
// This sample produces the following output:
100+
//
101+
// This sample completed successfully press Enter to exit.
102+
//</Snippet2>
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<PropertyGroup>
3+
<OutputType>Exe</OutputType>
4+
<TargetFramework>net7.0</TargetFramework>
5+
</PropertyGroup>
6+
7+
<ItemGroup>
8+
<Compile Include="constructors.fs" />
9+
</ItemGroup>
10+
</Project>
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
module append2
2+
3+
open System
4+
open System.Text
5+
6+
let appendBool () =
7+
// <Snippet2>
8+
let flag = false
9+
let sb = StringBuilder()
10+
sb.Append("The value of the flag is ").Append(flag).Append "." |> ignore
11+
printfn $"{sb}"
12+
// The example displays the following output:
13+
// The value of the flag is False.
14+
// </Snippet2>
15+
16+
let appendByte () =
17+
// <Snippet3>
18+
let bytes = [| 16uy; 132uy; 27uy; 253uy |]
19+
let sb = StringBuilder()
20+
21+
for value in bytes do
22+
sb.Append(value).Append " " |> ignore
23+
24+
printfn $"The byte array: {sb}"
25+
// The example displays the following output:
26+
// The byte array: 16 132 27 253
27+
// </Snippet3>
28+
29+
let appendChar () =
30+
// <Snippet4>
31+
let str = "Characters in a string."
32+
let sb = StringBuilder()
33+
34+
for ch in str do
35+
sb.Append(" '").Append(ch).Append "' " |> ignore
36+
37+
printfn "Characters in the string:"
38+
printfn $" {sb}"
39+
// The example displays the following output:
40+
// Characters in the string:
41+
// 'C' 'h' 'a' 'r' 'a' 'c' 't' 'e' 'r' 's' ' ' 'i' 'n' ' ' 'a' ' ' 's' 't' 'r' 'i' 'n' 'g' '.'
42+
// </Snippet4>
43+
44+
let appendMultipleChars () =
45+
// <Snippet5>
46+
let value = 1346.19m
47+
let sb = StringBuilder()
48+
sb.Append('*', 5).AppendFormat("{0:C2}", value).Append('*', 5) |> ignore
49+
printfn $"{sb}"
50+
// The example displays the following output:
51+
// *****$1,346.19*****
52+
// </Snippet5>
53+
54+
let appendCharArray () =
55+
// <Snippet6>
56+
let chars = [| 'a'; 'e'; 'i'; 'o'; 'u' |]
57+
let sb = StringBuilder()
58+
sb.Append("The characters in the array: ").Append chars |> ignore
59+
printfn $"{sb}"
60+
// The example displays the following output:
61+
// The characters in the array: aeiou
62+
// </Snippet6>
63+
64+
let appendPartialCharArray () =
65+
// <Snippet7>
66+
let chars = [| 'a'; 'b'; 'c'; 'd'; 'e' |]
67+
let sb = StringBuilder()
68+
let startPosition = Array.IndexOf(chars, 'a')
69+
let endPosition = Array.IndexOf(chars, 'c')
70+
71+
if startPosition >= 0 && endPosition >= 0 then
72+
sb
73+
.Append("The array from positions ")
74+
.Append(startPosition)
75+
.Append(" to ")
76+
.Append(endPosition)
77+
.Append(" contains ")
78+
.Append(chars, startPosition, endPosition + 1)
79+
.Append "."
80+
|> ignore
81+
82+
printfn $"{sb}"
83+
// The example displays the following output:
84+
// The array from positions 0 to 2 contains abc.
85+
// </Snippet7>
86+
87+
let appendDecimal () =
88+
// <Snippet8>
89+
let value = 1346.19m
90+
let sb = StringBuilder()
91+
sb.Append('*', 5).Append(value).Append('*', 5) |> ignore
92+
printfn $"{sb}"
93+
// The example displays the following output:
94+
// *****1346.19*****
95+
// </Snippet8>
96+
97+
let appendDouble () =
98+
// <Snippet9>
99+
let value = 1034769.47
100+
let sb = StringBuilder()
101+
sb.Append('*', 5).Append(value).Append('*', 5) |> ignore
102+
printfn $"{sb}"
103+
// The example displays the following output:
104+
// *****1034769.47*****
105+
// </Snippet9>
106+
107+
108+
appendBool ()
109+
printfn "-----"
110+
appendByte ()
111+
printfn "-----"
112+
appendChar ()
113+
printfn "-----"
114+
appendMultipleChars ()
115+
printfn "-----"
116+
appendCharArray ()
117+
printfn "-----"
118+
appendPartialCharArray ()
119+
printfn "-----"
120+
appendDecimal ()
121+
printfn "-----"
122+
appendDouble ()
123+
printfn "-----"
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
module append3
2+
3+
open System
4+
open System.Text
5+
6+
let appendInt16 () =
7+
// <Snippet10>
8+
let sb = StringBuilder "The range of a 16-bit integer: "
9+
sb.Append(Int16.MinValue).Append(" to ").Append Int16.MaxValue |> ignore
10+
printfn $"{sb}"
11+
// The example displays the following output:
12+
// The range of a 16-bit integer: -32768 to 32767
13+
// </Snippet10>
14+
15+
let appendInt32 () =
16+
// <Snippet11>
17+
let sb = StringBuilder "The range of a 32-bit integer: "
18+
sb.Append(Int32.MinValue).Append(" to ").Append Int32.MaxValue |> ignore
19+
printfn $"{sb}"
20+
// The example displays the following output:
21+
// The range of a 32-bit integer: -2147483648 to 2147483647
22+
// </Snippet11>
23+
24+
let appendInt64 () =
25+
// <Snippet12>
26+
let sb = StringBuilder "The range of a 64-bit integer: "
27+
sb.Append(Int64.MinValue).Append(" to ").Append Int64.MaxValue |> ignore
28+
printfn $"{sb}"
29+
// The example displays the following output:
30+
// The range of a 64-bit integer: -9223372036854775808 to 9223372036854775807
31+
// </Snippet12>
32+
33+
let appendSByte () =
34+
// <Snippet13>
35+
let sb = StringBuilder "The range of an 8-bit signed integer: "
36+
sb.Append(SByte.MinValue).Append(" to ").Append SByte.MaxValue |> ignore
37+
printfn $"{sb}"
38+
// The example displays the following output:
39+
// The range of an 8-bit unsigned integer: -128 to 127
40+
// </Snippet13>
41+
42+
let appendSingle () =
43+
// <Snippet14>
44+
let value = 1034769.47f
45+
let sb = StringBuilder()
46+
sb.Append('*', 5).Append(value).Append('*', 5) |> ignore
47+
printfn $"{sb}"
48+
// The example displays the following output:
49+
// *****1034769.47*****
50+
// </Snippet14>
51+
52+
let appendUInt16 () =
53+
// <Snippet15>
54+
let sb = StringBuilder "The range of a 16-bit unsigned integer: "
55+
sb.Append(UInt16.MinValue).Append(" to ").Append UInt16.MaxValue |> ignore
56+
printfn $"{sb}"
57+
// The example displays the following output:
58+
// The range of a 16-bit unsigned integer: 0 to 65535
59+
// </Snippet15>
60+
61+
let appendUInt32 () =
62+
// <Snippet16>
63+
let sb = StringBuilder "The range of a 32-bit unsigned integer: "
64+
sb.Append(UInt32.MinValue).Append(" to ").Append UInt32.MaxValue |> ignore
65+
printfn $"{sb}"
66+
// The example displays the following output:
67+
// The range of a 32-bit unsigned integer: 0 to 4294967295
68+
// </Snippet16>
69+
70+
let appendUInt64 () =
71+
// <Snippet17>
72+
let sb = StringBuilder "The range of a 64-bit unsigned integer: "
73+
sb.Append(UInt64.MinValue).Append(" to ").Append UInt64.MaxValue |> ignore
74+
printfn $"{sb}"
75+
// The example displays the following output:
76+
// The range of a 64-bit unsigned integer: 0 to 18446744073709551615
77+
// </Snippet17>
78+
79+
let appendString () =
80+
// <Snippet19>
81+
let str = "First;George Washington;1789;1797"
82+
let mutable index = 0
83+
let sb = StringBuilder()
84+
let length = str.IndexOf(';', index)
85+
86+
sb.Append(str, index, length).Append " President of the United States: "
87+
|> ignore
88+
89+
index <- index + length + 1
90+
let length = str.IndexOf(';', index) - index
91+
sb.Append(str, index, length).Append ", from " |> ignore
92+
index <- index + length + 1
93+
let length = str.IndexOf(';', index) - index
94+
sb.Append(str, index, length).Append " to " |> ignore
95+
index <- index + length + 1
96+
sb.Append(str, index, str.Length - index) |> ignore
97+
printfn $"{sb}"
98+
// The example displays the following output:
99+
// First President of the United States: George Washington, from 1789 to 1797
100+
// </Snippet19>
101+
102+
103+
appendInt16 ()
104+
printfn "---"
105+
appendInt32 ()
106+
printfn "---"
107+
appendInt64 ()
108+
printfn "---"
109+
appendSByte ()
110+
printfn "---"
111+
appendSingle ()
112+
printfn "---"
113+
appendUInt16 ()
114+
printfn "---"
115+
appendUInt32 ()
116+
printfn "---"
117+
appendUInt64 ()
118+
printfn "---"
119+
appendString ()
120+
printfn "---"
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
module append4
2+
// <Snippet18>
3+
open System.Text
4+
5+
type Dog(name: string, breed: string) =
6+
member _.Name = name
7+
member _.Breed = breed
8+
override _.ToString() = name
9+
10+
let dog1 = Dog("Yiska", "Alaskan Malamute")
11+
let sb = StringBuilder()
12+
sb.Append(dog1).Append(", Breed: ").Append dog1.Breed |> printfn "%O"
13+
14+
// The example displays the following output:
15+
// Yiska, Breed: Alaskan Malamute
16+
// </Snippet18>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<PropertyGroup>
3+
<OutputType>Exe</OutputType>
4+
<TargetFramework>net7.0</TargetFramework>
5+
</PropertyGroup>
6+
7+
<ItemGroup>
8+
<Compile Include="append2.fs" />
9+
<Compile Include="append3.fs" />
10+
<Compile Include="append4.fs" />
11+
</ItemGroup>
12+
</Project>

0 commit comments

Comments
 (0)