Skip to content

Instantly share code, notes, and snippets.

@manofstick
Created June 6, 2016 20:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save manofstick/37f243fedaae203104c7dcc81b221d4b to your computer and use it in GitHub Desktop.
Save manofstick/37f243fedaae203104c7dcc81b221d4b to your computer and use it in GitHub Desktop.
Example of string functions
module SomeNamespace.String
open System
open System.Globalization
let inline private safe s =
match s with
| null -> String.Empty
| _ -> s
let inline isNada s = String.IsNullOrEmpty s
let inline isBlank s = String.IsNullOrWhiteSpace s
let toString<'a> (item:'a) =
match box item with
| null -> String.Empty
| _ -> item.ToString ()
let trim (s:string) = (safe s).Trim ()
let trimEnd (s:string) = (safe s).TrimEnd ()
let trimStart (s:string) = (safe s).TrimStart ()
let contains text (s:string) = (safe s).Contains (safe text)
let startsWith text (s:string) = (safe s).StartsWith (safe text)
let inline replace oldText newText (s:^string) =
match s with
| null when isNada (toString oldText) -> toString newText
| null -> String.Empty
| _ -> (^string:(member Replace:^a -> ^a -> string) (s, oldText, newText))
let padLeft char totalWidth (s:string) = (safe s).PadLeft (totalWidth, char)
let padRight char totalWidth (s:string) = (safe s).PadRight (totalWidth, char)
let inline format style formattable =
match box formattable with
| null -> String.Empty
| _ -> (^formattable:(member ToString:string->string) (formattable,(safe style)))
let remove count index (s:string) = (safe s).Remove (index, count)
let insert text index (s:string) = (safe s).Insert (index, text)
let length (s:string) = (safe s).Length
let tryGet index s = if isNada s || index >= length s then None else Some s.[index]
let split (c:char) s = (safe s).Split [|c|]
let splitClean (c:char) s = (safe s).Split ([|c|], StringSplitOptions.RemoveEmptyEntries)
let splitStr (sp:string) s = (safe s).Split ([|sp|], StringSplitOptions.None)
let substring start count s = (safe s).Substring(start, count)
module private Culture =
let inline startsWith text s culture = (safe s).StartsWith (safe text, culture)
let inline toString<'a when 'a :> System.IFormattable> format (item:'a) culture =
match box item with
| null -> String.Empty
| _ -> item.ToString(safe format, culture)
let inline indexOf text pos s (culture:StringComparison) = (safe s).IndexOf (text, pos, culture)
let inline equals s1 s2 (comparer:StringComparer) = comparer.Equals (s1, s2)
module InvariantCulture =
let inline startsWith text s = StringComparison.InvariantCulture |> Culture.startsWith text s
let inline indexOf text pos s = StringComparison.InvariantCulture |> Culture.indexOf text pos s
let inline toString format item = CultureInfo.InvariantCulture |> Culture.toString format item
let inline equals lhs rhs = StringComparer.InvariantCulture |> Culture.equals lhs rhs
module InvariantCultureIgnoreCase =
let inline startsWith text s = StringComparison.InvariantCultureIgnoreCase |> Culture.startsWith text s
let inline indexOf text pos s = StringComparison.InvariantCultureIgnoreCase |> Culture.indexOf text pos s
let inline equals lhs rhs = StringComparer.InvariantCultureIgnoreCase |> Culture.equals lhs rhs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment