diff --git a/snippets/fsharp/System/String/IsNullOrEmpty/IsNullOrEmpty.fsproj b/snippets/fsharp/System/String/IsNullOrEmpty/IsNullOrEmpty.fsproj new file mode 100644 index 00000000000..e3ceb94582d --- /dev/null +++ b/snippets/fsharp/System/String/IsNullOrEmpty/IsNullOrEmpty.fsproj @@ -0,0 +1,15 @@ + + + + Exe + net6.0 + + + + + + + + + + diff --git a/snippets/fsharp/System/String/IsNullOrEmpty/NullString1.fs b/snippets/fsharp/System/String/IsNullOrEmpty/NullString1.fs new file mode 100644 index 00000000000..21f1ee3462c --- /dev/null +++ b/snippets/fsharp/System/String/IsNullOrEmpty/NullString1.fs @@ -0,0 +1,19 @@ +namespace IsNullOrEmpty +open System + +module NullString1 = + + // + 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. + // diff --git a/snippets/fsharp/System/String/IsNullOrEmpty/NullString2.fs b/snippets/fsharp/System/String/IsNullOrEmpty/NullString2.fs new file mode 100644 index 00000000000..d8c8fad9c8c --- /dev/null +++ b/snippets/fsharp/System/String/IsNullOrEmpty/NullString2.fs @@ -0,0 +1,12 @@ +namespace IsNullOrEmpty +open System + +module NullString2 = + + // + let s = "" + printfn "The length of '%s' is %d." s s.Length + + // The example displays the following output: + // The length of '' is 0. + // diff --git a/snippets/fsharp/System/String/IsNullOrEmpty/inoe.fs b/snippets/fsharp/System/String/IsNullOrEmpty/inoe.fs new file mode 100644 index 00000000000..631947955c8 --- /dev/null +++ b/snippets/fsharp/System/String/IsNullOrEmpty/inoe.fs @@ -0,0 +1,24 @@ +namespace IsNullOrEmpty +open System + +module Inoe = + + // + 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. + // diff --git a/snippets/fsharp/System/String/IsNullOrEmpty/isnullorempty1.fs b/snippets/fsharp/System/String/IsNullOrEmpty/isnullorempty1.fs new file mode 100644 index 00000000000..7f4e29126fe --- /dev/null +++ b/snippets/fsharp/System/String/IsNullOrEmpty/isnullorempty1.fs @@ -0,0 +1,19 @@ +namespace IsNullOrEmpty +open System + +module IsNullOrEmpty1 = + + // + 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 + // diff --git a/xml/System/String.xml b/xml/System/String.xml index f18933fb5ba..782b7b9def9 100644 --- a/xml/System/String.xml +++ b/xml/System/String.xml @@ -8368,6 +8368,7 @@ The method to test whether a string is `null`, its value is , or it consists only of white-space characters. @@ -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? @@ -8386,6 +8388,7 @@ A string is empty if it is explicitly assigned an empty string ("") or @@ -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 , passing as the replacementText parameter. - + This method is guaranteed O(n) complexity, where n is the length of the input string. ]]> @@ -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 , 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 n is the length of the input string, and where r is the length of `replacementText`.