Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<Compile Include="inoe.fs" />
<Compile Include="NullString1.fs" />
<Compile Include="NullString2.fs" />
<Compile Include="isnullorempty1.fs" />
</ItemGroup>

</Project>
19 changes: 19 additions & 0 deletions snippets/fsharp/System/String/IsNullOrEmpty/NullString1.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
namespace IsNullOrEmpty
open System

module NullString1 =

// <Snippet2>
let (s: string) = null

printfn "The value of the string is '%s'" s

try
printfn "String length is %d" s.Length
with
| :? NullReferenceException as ex -> printfn "%s" ex.Message

// The example displays the following output:
// The value of the string is ''
// Object reference not set to an instance of an object.
// </Snippet2>
12 changes: 12 additions & 0 deletions snippets/fsharp/System/String/IsNullOrEmpty/NullString2.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
namespace IsNullOrEmpty
open System

module NullString2 =

// <Snippet3>
let s = ""
printfn "The length of '%s' is %d." s s.Length

// The example displays the following output:
// The length of '' is 0.
// </Snippet3>
24 changes: 24 additions & 0 deletions snippets/fsharp/System/String/IsNullOrEmpty/inoe.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
namespace IsNullOrEmpty
open System

module Inoe =

//<Snippet1>
let test (s: string): string =
if String.IsNullOrEmpty(s)
then "is null or empty"
else $"(\"{s}\") is neither null nor empty"

let s1 = "abcd"
let s2 = ""
let s3 = null

printfn "String s1 %s" (test s1)
printfn "String s2 %s" (test s2)
printfn "String s2 %s" (test s3)

// The example displays the following output:
// String s1 ("abcd") is neither null nor empty.
// String s2 is null or empty.
// String s3 is null or empty.
//</Snippet1>
19 changes: 19 additions & 0 deletions snippets/fsharp/System/String/IsNullOrEmpty/isnullorempty1.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
namespace IsNullOrEmpty
open System

module IsNullOrEmpty1 =

// <Snippet1>
let testForNullOrEmpty (s: string): bool =
s = null || s = String.Empty

let s1 = null
let s2 = ""

printfn "%b" (testForNullOrEmpty s1)
printfn "%b" (testForNullOrEmpty s2)

// The example displays the following output:
// true
// true
// </Snippet1>
16 changes: 10 additions & 6 deletions xml/System/String.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8368,6 +8368,7 @@ The <xref:System.Runtime.CompilerServices.CompilationRelaxations.NoStringInterni
:::code language="cpp" source="~/snippets/cpp/VS_Snippets_CLR_System/system.string.isnullorempty/cpp/isnullorempty1.cpp" id="Snippet1":::
:::code language="csharp" source="~/snippets/csharp/System/String/IsNullOrEmpty/isnullorempty1.cs" interactive="try-dotnet-method" id="Snippet1":::
:::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.string.isnullorempty/vb/isnullorempty1.vb" id="Snippet1":::
:::code language="fsharp" source="~/snippets/fsharp/System/String/IsNullOrEmpty/isnullorempty1.fs" interactive="try-dotnet-method" id="Snippet1":::

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.

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

## What is an empty string?

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

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

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

]]></format>
</remarks>
Expand Down Expand Up @@ -11383,16 +11387,16 @@ This method searches for all newline sequences within the string and canonicaliz
occurrences of non-Windows newline sequences will be replaced with the sequence CRLF. When
running on Unix, all occurrences of non-Unix newline sequences will be replaced with
a single LF character.

It is not recommended that protocol parsers utilize this API. Protocol specifications often
mandate specific newline sequences. For example, HTTP/1.1 (RFC 8615) mandates that the request
line, status line, and headers lines end with CRLF. Since this API operates over a wide range
of newline sequences, a protocol parser utilizing this API could exhibit behaviors unintended
by the protocol's authors.

This overload is equivalent to calling <xref:System.String.ReplaceLineEndings(System.String)>, passing
<xref:System.Environment.NewLine> as the <em>replacementText</em> parameter.

This method is guaranteed O(n) complexity, where <em>n</em> is the length of the input string.

]]></format>
Expand Down Expand Up @@ -11435,17 +11439,17 @@ This method searches for all newline sequences within the string and canonicaliz
This method searches for all newline sequences within the string and canonicalizes them to the
newline sequence provided by `replacementText`. If `replacementText`
is <xref:System.String.Empty>, all newline sequences within the string will be removed.

It is not recommended that protocol parsers utilize this API. Protocol specifications often
mandate specific newline sequences. For example, HTTP/1.1 (RFC 8615) mandates that the request
line, status line, and headers lines end with CRLF. Since this API operates over a wide range
of newline sequences, a protocol parser utilizing this API could exhibit behaviors unintended
by the protocol's authors.

The list of recognized newline sequences is CR (U+000D), LF (U+000A), CRLF (U+000D U+000A),
NEL (U+0085), LS (U+2028), FF (U+000C), and PS (U+2029). This list is given by the Unicode
Standard, Sec. 5.8, Recommendation R4 and Table 5-2.

This method is guaranteed O(n * r) complexity, where <em>n</em> is the length of the input string,
and where <em>r</em> is the length of `replacementText`.

Expand Down