4 people like it.

Exception-free String.Insert

"Please enter the description" - Why'd I do that ?

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
14: 
15: 
16: 
17: 
18: 
19: 
20: 
21: 
22: 
23: 
open System

/// Push a [string] to the [int]-th element of a [string].
let StrPush (sample : string) (cell : int) (input : string) : string = 
     match cell with 
        | cell when cell <= 0 
            -> input.Insert( 0 , sample )
        | cell when cell >= input.Length 
            -> input.Insert( input.Length , sample )
        | _ -> input.Insert( cell , sample )

//  Normal case:

"May the  be with you"
    |>  StrPush "Force" 8
    |>  printf "\n  >>  %s"

//  Tricky cases:

"F#"
    |>  StrPush "I love " -999999
    |>  StrPush " so much !" 999999
    |>  printf "\n  >>  %s" 
namespace System
val StrPush : sample:string -> cell:int -> input:string -> string

Full name: Script.StrPush


 Push a [string] to the [int]-th element of a [string].
val sample : string
Multiple items
val string : value:'T -> string

Full name: Microsoft.FSharp.Core.Operators.string

--------------------
type string = String

Full name: Microsoft.FSharp.Core.string
val cell : int
Multiple items
val int : value:'T -> int (requires member op_Explicit)

Full name: Microsoft.FSharp.Core.Operators.int

--------------------
type int = int32

Full name: Microsoft.FSharp.Core.int

--------------------
type int<'Measure> = int

Full name: Microsoft.FSharp.Core.int<_>
val input : string
String.Insert(startIndex: int, value: string) : string
property String.Length: int
val printf : format:Printf.TextWriterFormat<'T> -> 'T

Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.printf
Raw view Test code New version

More information

Link:http://fssnip.net/7XD
Posted:4 years ago
Author:you
Tags: string