From 2cd818abf98240109a9ca9ad4982faeea2f8fdf5 Mon Sep 17 00:00:00 2001 From: MecuStefan <90316972+MecuStefan@users.noreply.github.com> Date: Thu, 9 Sep 2021 14:33:15 +0300 Subject: [PATCH] Sorted the string.fsi entries and added examples to each one (#12130) * Sorted the string.fsi entries and added examples to each one * Applying suggestions to the String examples * Following the suggestion that is more clear. * Sorted the string.fsi entries and added examples to each one * Applying suggestions to the String examples * Following the suggestion that is more clear. --- src/fsharp/FSharp.Core/string.fsi | 254 +++++++++++++++++++++++------- 1 file changed, 194 insertions(+), 60 deletions(-) diff --git a/src/fsharp/FSharp.Core/string.fsi b/src/fsharp/FSharp.Core/string.fsi index cca1054f58a..73174bc6bbd 100644 --- a/src/fsharp/FSharp.Core/string.fsi +++ b/src/fsharp/FSharp.Core/string.fsi @@ -18,6 +18,30 @@ namespace Microsoft.FSharp.Core [] module String = + /// Builds a new string whose characters are the results of applying the function mapping + /// to each of the characters of the input string and concatenating the resulting + /// strings. + /// + /// The function to produce a string from each character of the input string. + /// The input string. + /// + /// The concatenated string. + /// + /// The following samples shows how to interspace spaces in a text + /// + /// let input = "Stefan says: Hi!" + /// input |> String.collect (sprintf "%c ") // evaluates "S t e f a n s a y s : H i ! " + /// + /// + /// + /// How to show the ASCII representation of a very secret text + /// + /// "Secret" |> String.collect (fun chr -> int chr |> sprintf "%d ") // evaluates "83 101 99 114 101 116 " + /// + /// + [] + val collect: mapping:(char -> string) -> str:string -> string + /// Returns a new string made by concatenating the given strings /// with separator sep, that is a1 + sep + ... + sep + aN. /// The separator string to be inserted between the strings @@ -27,54 +51,41 @@ namespace Microsoft.FSharp.Core /// A new string consisting of the concatenated strings separated by /// the separation string. /// Thrown when strings is null. + /// + /// + /// + /// let input1 = ["Stefan"; "says:"; "Hello"; "there!"] + /// input1 |> String.concat " " // evaluates "Stefan says: Hello there!" + /// + /// let input2 = [0..9] |> List.map string + /// input2 |> String.concat "" // evaluates "0123456789" + /// input2 |> String.concat ", " // evaluates "0, 1, 2, 3, 4, 5, 6, 7, 8, 9" + /// + /// let input3 = ["No comma"] + /// input3 |> String.concat "," // evaluates "No comma" + /// + /// [] val concat: sep:string -> strings: seq -> string - /// Applies the function action to each character in the string. - /// - /// The function to be applied to each character of the string. - /// The input string. - [] - val iter: action:(char -> unit) -> str:string -> unit - - /// Applies the function action to the index of each character in the string and the - /// character itself. - /// - /// The function to apply to each character and index of the string. - /// The input string. - [] - val iteri: action:(int -> char -> unit) -> str:string -> unit - - /// Builds a new string whose characters are the results of applying the function mapping - /// to each of the characters of the input string. - /// - /// The function to apply to the characters of the string. - /// The input string. - /// - /// The resulting string. - [] - val map: mapping:(char -> char) -> str:string -> string - - /// Builds a new string whose characters are the results of applying the function mapping - /// to each character and index of the input string. - /// - /// The function to apply to each character and index of the string. - /// The input string. - /// - /// The resulting string. - [] - val mapi: mapping:(int -> char -> char) -> str:string -> string - - /// Builds a new string whose characters are the results of applying the function mapping - /// to each of the characters of the input string and concatenating the resulting - /// strings. + /// Tests if any character of the string satisfies the given predicate. /// - /// The function to produce a string from each character of the input string. + /// The function to test each character of the string. /// The input string. /// - /// The concatenated string. - [] - val collect: mapping:(char -> string) -> str:string -> string + /// True if any character returns true for the predicate and false otherwise. + /// + /// Looking for uppercase characters + /// + /// open System + /// + /// "Yoda" |> String.exists Char.IsUpper // evaluates true + /// + /// "nope" |> String.exists Char.IsUpper // evaluates false + /// + /// + [] + val exists: predicate:(char -> bool) -> str:string -> bool /// Builds a new string containing only the characters of the input string /// for which the given predicate returns "true". @@ -85,9 +96,44 @@ namespace Microsoft.FSharp.Core /// The input string. /// /// The resulting string. + /// + /// Filtering out just alphanumeric characters + /// + /// open System + /// + /// let input = "0 1 2 3 4 5 6 7 8 9 a A m M" + /// input |> String.filter Uri.IsHexDigit // evaluates "123456789aA" + /// + /// + /// Filtering out just digits + /// + /// open System + /// + /// "hello" |> String.filter Char.IsDigit // evaluates "" + /// + /// [] val filter: predicate:(char -> bool) -> str:string -> string + /// Tests if all characters in the string satisfy the given predicate. + /// + /// The function to test each character of the string. + /// The input string. + /// + /// True if all characters return true for the predicate and false otherwise. + /// + /// Looking for lowercase characters + /// + /// open System + /// + /// "all are lower" |> String.forall Char.IsLower // evaluates false + /// + /// "allarelower" |> String.forall Char.IsLower // evaluates true + /// + /// + [] + val forall: predicate:(char -> bool) -> str:string -> bool + /// Builds a new string whose characters are the results of applying the function mapping /// to each index from 0 to count-1 and concatenating the resulting /// strings. @@ -98,26 +144,115 @@ namespace Microsoft.FSharp.Core /// /// The constructed string. /// Thrown when count is negative. + /// + /// Enumerate digits ASCII codes + /// + /// String.init 10 (fun i -> int '0' + i |> sprintf "%d ") // evaluates "48 49 50 51 52 53 54 55 56 57 " + /// + /// [] val init: count:int -> initializer:(int -> string) -> string - /// Tests if all characters in the string satisfy the given predicate. + /// Applies the function action to each character in the string. /// - /// The function to test each character of the string. + /// The function to be applied to each character of the string. /// The input string. + /// + /// Printing the ASCII code for each characater in the string + /// + /// let input = "Hello" + /// input |> String.iter (fun c -> printfn "%c %d" c (int c)) + /// // evaluates unit + /// // prints: + /// H 72 + /// e 101 + /// l 108 + /// l 108 + /// o 111 + /// + /// + [] + val iter: action:(char -> unit) -> str:string -> unit + + /// Applies the function action to the index of each character in the string and the + /// character itself. /// - /// True if all characters return true for the predicate and false otherwise. - [] - val forall: predicate:(char -> bool) -> str:string -> bool + /// The function to apply to each character and index of the string. + /// The input string. + /// + /// Numbering the characters and printing the associated ASCII code + /// for each characater in the input string + /// + /// let input = "Hello" + /// input |> String.iteri (fun i c -> printfn "%d. %c %d" (i + 1) c (int c)) + /// // evaluates unit + /// // prints: + /// 1. H 72 + /// 2. e 101 + /// 3. l 108 + /// 4. l 108 + /// 5. o 111 + /// + /// + [] + val iteri: action:(int -> char -> unit) -> str:string -> unit - /// Tests if any character of the string satisfies the given predicate. + /// Returns the length of the string. /// - /// The function to test each character of the string. /// The input string. /// - /// True if any character returns true for the predicate and false otherwise. - [] - val exists: predicate:(char -> bool) -> str:string -> bool + /// The number of characters in the string. + /// + /// Getting the length of different strings + /// + /// String.length null // evaluates 0 + /// String.length "" // evaluates 0 + /// String.length "123" // evaluates 3 + /// + /// + [] + val length: str:string -> int + + /// Builds a new string whose characters are the results of applying the function mapping + /// to each of the characters of the input string. + /// + /// The function to apply to the characters of the string. + /// The input string. + /// + /// The resulting string. + /// + /// Changing case to upper for all characters in the input string + /// + /// open System + /// let input = "Hello there!" + /// input |> String.map Char.ToUpper // evaluates "HELLO THERE!" + /// + /// + [] + val map: mapping:(char -> char) -> str:string -> string + + /// Builds a new string whose characters are the results of applying the function mapping + /// to each character and index of the input string. + /// + /// The function to apply to each character and index of the string. + /// The input string. + /// + /// The resulting string. + /// + /// Alternating case for all characters in the input string + /// + /// open System + /// + /// let alternateCase indx chr = + /// if 0 = indx % 2 + /// then Char.ToUpper chr + /// else Char.ToLower chr + /// let input = "Hello there!" + /// input |> String.mapi alternateCase // evaluates "HeLlO ThErE!" + /// + /// + [] + val mapi: mapping:(int -> char -> char) -> str:string -> string /// Returns a string by concatenating count instances of str. /// @@ -126,14 +261,13 @@ namespace Microsoft.FSharp.Core /// /// The concatenated string. /// Thrown when count is negative. + /// + /// + /// + /// "Do it!" |> String.replicate 3 // evaluates "Do it!Do it!Do it!" + /// + /// [] val replicate: count:int -> str: string -> string - - /// Returns the length of the string. - /// - /// The input string. - /// - /// The number of characters in the string. - [] - val length: str:string -> int +