Skip to content

Commit 1dcd3df

Browse files
authored
Add F# examples for System.String.IsNullOrEmpty (#7750)
Add project+modules for F# String.IsNullOrEmpty snippets Fix F# snippet link
1 parent 01813ff commit 1dcd3df

File tree

6 files changed

+99
-6
lines changed

6 files changed

+99
-6
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net6.0</TargetFramework>
6+
</PropertyGroup>
7+
8+
<ItemGroup>
9+
<Compile Include="inoe.fs" />
10+
<Compile Include="NullString1.fs" />
11+
<Compile Include="NullString2.fs" />
12+
<Compile Include="isnullorempty1.fs" />
13+
</ItemGroup>
14+
15+
</Project>
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
namespace IsNullOrEmpty
2+
open System
3+
4+
module NullString1 =
5+
6+
// <Snippet2>
7+
let (s: string) = null
8+
9+
printfn "The value of the string is '%s'" s
10+
11+
try
12+
printfn "String length is %d" s.Length
13+
with
14+
| :? NullReferenceException as ex -> printfn "%s" ex.Message
15+
16+
// The example displays the following output:
17+
// The value of the string is ''
18+
// Object reference not set to an instance of an object.
19+
// </Snippet2>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
namespace IsNullOrEmpty
2+
open System
3+
4+
module NullString2 =
5+
6+
// <Snippet3>
7+
let s = ""
8+
printfn "The length of '%s' is %d." s s.Length
9+
10+
// The example displays the following output:
11+
// The length of '' is 0.
12+
// </Snippet3>
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
namespace IsNullOrEmpty
2+
open System
3+
4+
module Inoe =
5+
6+
//<Snippet1>
7+
let test (s: string): string =
8+
if String.IsNullOrEmpty(s)
9+
then "is null or empty"
10+
else $"(\"{s}\") is neither null nor empty"
11+
12+
let s1 = "abcd"
13+
let s2 = ""
14+
let s3 = null
15+
16+
printfn "String s1 %s" (test s1)
17+
printfn "String s2 %s" (test s2)
18+
printfn "String s2 %s" (test s3)
19+
20+
// The example displays the following output:
21+
// String s1 ("abcd") is neither null nor empty.
22+
// String s2 is null or empty.
23+
// String s3 is null or empty.
24+
//</Snippet1>
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
namespace IsNullOrEmpty
2+
open System
3+
4+
module IsNullOrEmpty1 =
5+
6+
// <Snippet1>
7+
let testForNullOrEmpty (s: string): bool =
8+
s = null || s = String.Empty
9+
10+
let s1 = null
11+
let s2 = ""
12+
13+
printfn "%b" (testForNullOrEmpty s1)
14+
printfn "%b" (testForNullOrEmpty s2)
15+
16+
// The example displays the following output:
17+
// true
18+
// true
19+
// </Snippet1>

xml/System/String.xml

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8368,6 +8368,7 @@ The <xref:System.Runtime.CompilerServices.CompilationRelaxations.NoStringInterni
83688368
:::code language="cpp" source="~/snippets/cpp/VS_Snippets_CLR_System/system.string.isnullorempty/cpp/isnullorempty1.cpp" id="Snippet1":::
83698369
:::code language="csharp" source="~/snippets/csharp/System/String/IsNullOrEmpty/isnullorempty1.cs" interactive="try-dotnet-method" id="Snippet1":::
83708370
:::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.string.isnullorempty/vb/isnullorempty1.vb" id="Snippet1":::
8371+
:::code language="fsharp" source="~/snippets/fsharp/System/String/IsNullOrEmpty/isnullorempty1.fs" interactive="try-dotnet-method" id="Snippet1":::
83718372

83728373
You can use the <xref:System.String.IsNullOrWhiteSpace%2A> method to test whether a string is `null`, its value is <xref:System.String.Empty?displayProperty=nameWithType>, or it consists only of white-space characters.
83738374

@@ -8378,6 +8379,7 @@ A string is `null` if it has not been assigned a value (in C++ and Visual Basic)
83788379
:::code language="cpp" source="~/snippets/cpp/VS_Snippets_CLR_System/system.string.isnullorempty/cpp/NullString1.cpp" id="Snippet2":::
83798380
:::code language="csharp" source="~/snippets/csharp/System/String/IsNullOrEmpty/NullString1.cs" interactive="try-dotnet-method" id="Snippet2":::
83808381
:::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.string.isnullorempty/vb/NullString1.vb" id="Snippet2":::
8382+
:::code language="fsharp" source="~/snippets/fsharp/System/String/IsNullOrEmpty/NullString1.fs" interactive="try-dotnet-method" id="Snippet2":::
83818383

83828384
## What is an empty string?
83838385

@@ -8386,13 +8388,15 @@ A string is empty if it is explicitly assigned an empty string ("") or <xref:Sy
83868388
:::code language="cpp" source="~/snippets/cpp/VS_Snippets_CLR_System/system.string.isnullorempty/cpp/NullString1.cpp" id="Snippet3":::
83878389
:::code language="csharp" source="~/snippets/csharp/System/String/IsNullOrEmpty/NullString1.cs" interactive="try-dotnet-method" id="Snippet3":::
83888390
:::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.string.isnullorempty/vb/NullString1.vb" id="Snippet3":::
8391+
:::code language="fsharp" source="~/snippets/fsharp/System/String/IsNullOrEmpty/NullString2.fs" interactive="try-dotnet-method" id="Snippet3":::
83898392

83908393
## Examples
83918394
The following example examines three strings and determines whether each string has a value, is an empty string, or is `null`.
83928395

83938396
:::code language="cpp" source="~/snippets/cpp/VS_Snippets_CLR/string.isNullOrEmpty/CPP/inoe.cpp" id="Snippet1":::
83948397
:::code language="csharp" source="~/snippets/csharp/System/String/IsNullOrEmpty/inoe.cs" interactive="try-dotnet-method" id="Snippet1":::
83958398
:::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR/string.isNullOrEmpty/VB/inoe.vb" id="Snippet1":::
8399+
:::code language="fsharp" source="~/snippets/fsharp/System/String/IsNullOrEmpty/inoe.fs" interactive="try-dotnet-method" id="Snippet1":::
83968400

83978401
]]></format>
83988402
</remarks>
@@ -11383,16 +11387,16 @@ This method searches for all newline sequences within the string and canonicaliz
1138311387
occurrences of non-Windows newline sequences will be replaced with the sequence CRLF. When
1138411388
running on Unix, all occurrences of non-Unix newline sequences will be replaced with
1138511389
a single LF character.
11386-
11390+
1138711391
It is not recommended that protocol parsers utilize this API. Protocol specifications often
1138811392
mandate specific newline sequences. For example, HTTP/1.1 (RFC 8615) mandates that the request
1138911393
line, status line, and headers lines end with CRLF. Since this API operates over a wide range
1139011394
of newline sequences, a protocol parser utilizing this API could exhibit behaviors unintended
1139111395
by the protocol's authors.
11392-
11396+
1139311397
This overload is equivalent to calling <xref:System.String.ReplaceLineEndings(System.String)>, passing
1139411398
<xref:System.Environment.NewLine> as the <em>replacementText</em> parameter.
11395-
11399+
1139611400
This method is guaranteed O(n) complexity, where <em>n</em> is the length of the input string.
1139711401

1139811402
]]></format>
@@ -11435,17 +11439,17 @@ This method searches for all newline sequences within the string and canonicaliz
1143511439
This method searches for all newline sequences within the string and canonicalizes them to the
1143611440
newline sequence provided by `replacementText`. If `replacementText`
1143711441
is <xref:System.String.Empty>, all newline sequences within the string will be removed.
11438-
11442+
1143911443
It is not recommended that protocol parsers utilize this API. Protocol specifications often
1144011444
mandate specific newline sequences. For example, HTTP/1.1 (RFC 8615) mandates that the request
1144111445
line, status line, and headers lines end with CRLF. Since this API operates over a wide range
1144211446
of newline sequences, a protocol parser utilizing this API could exhibit behaviors unintended
1144311447
by the protocol's authors.
11444-
11448+
1144511449
The list of recognized newline sequences is CR (U+000D), LF (U+000A), CRLF (U+000D U+000A),
1144611450
NEL (U+0085), LS (U+2028), FF (U+000C), and PS (U+2029). This list is given by the Unicode
1144711451
Standard, Sec. 5.8, Recommendation R4 and Table 5-2.
11448-
11452+
1144911453
This method is guaranteed O(n * r) complexity, where <em>n</em> is the length of the input string,
1145011454
and where <em>r</em> is the length of `replacementText`.
1145111455

0 commit comments

Comments
 (0)