1 people like it.

Separate string with numbers by thousands

Inserts a space every 3 characters in a string starting from the end of the string.

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
14: 
15: 
16: 
let separateByThousands (text: string) =
    let sb = System.Text.StringBuilder ()

    let rec insertSpaceEvery3Chars textPos =
        if textPos < 0 then
            ()
        else if textPos % 3 = 0 && textPos > 0 then
            sb.Append (" ") |> ignore
            sb.Append (text.[textPos]) |> ignore
            insertSpaceEvery3Chars (textPos - 1)
        else
            sb.Append (text.[textPos]) |> ignore
            insertSpaceEvery3Chars (textPos - 1)

    insertSpaceEvery3Chars (text.Length - 1) // Start from the end.
    sb.ToString ()
val separateByThousands : text:string -> string
val text : string
Multiple items
val string : value:'T -> string

--------------------
type string = System.String
val sb : System.Text.StringBuilder
namespace System
namespace System.Text
Multiple items
type StringBuilder =
  new : unit -> StringBuilder + 5 overloads
  member Append : value:string -> StringBuilder + 23 overloads
  member AppendFormat : format:string * arg0:obj -> StringBuilder + 7 overloads
  member AppendJoin : separator:string * [<ParamArray>] values:obj[] -> StringBuilder + 5 overloads
  member AppendLine : unit -> StringBuilder + 1 overload
  member Capacity : int with get, set
  member Chars : int -> char with get, set
  member Clear : unit -> StringBuilder
  member CopyTo : sourceIndex:int * destination:Span<char> * count:int -> unit + 1 overload
  member EnsureCapacity : capacity:int -> int
  ...
  nested type ChunkEnumerator

--------------------
System.Text.StringBuilder() : System.Text.StringBuilder
System.Text.StringBuilder(capacity: int) : System.Text.StringBuilder
System.Text.StringBuilder(value: string) : System.Text.StringBuilder
System.Text.StringBuilder(value: string, capacity: int) : System.Text.StringBuilder
System.Text.StringBuilder(capacity: int, maxCapacity: int) : System.Text.StringBuilder
System.Text.StringBuilder(value: string, startIndex: int, length: int, capacity: int) : System.Text.StringBuilder
val insertSpaceEvery3Chars : (int -> unit)
val textPos : int
System.Text.StringBuilder.Append(value: System.ReadOnlyMemory<char>) : System.Text.StringBuilder
   (+0 other overloads)
System.Text.StringBuilder.Append(value: System.ReadOnlySpan<char>) : System.Text.StringBuilder
   (+0 other overloads)
System.Text.StringBuilder.Append(value: char []) : System.Text.StringBuilder
   (+0 other overloads)
System.Text.StringBuilder.Append(value: obj) : System.Text.StringBuilder
   (+0 other overloads)
System.Text.StringBuilder.Append(value: uint64) : System.Text.StringBuilder
   (+0 other overloads)
System.Text.StringBuilder.Append(value: uint32) : System.Text.StringBuilder
   (+0 other overloads)
System.Text.StringBuilder.Append(value: uint16) : System.Text.StringBuilder
   (+0 other overloads)
System.Text.StringBuilder.Append(value: decimal) : System.Text.StringBuilder
   (+0 other overloads)
System.Text.StringBuilder.Append(value: float) : System.Text.StringBuilder
   (+0 other overloads)
System.Text.StringBuilder.Append(value: float32) : System.Text.StringBuilder
   (+0 other overloads)
val ignore : value:'T -> unit
property System.String.Length: int with get
System.Text.StringBuilder.ToString() : string
System.Text.StringBuilder.ToString(startIndex: int, length: int) : string
Raw view Test code New version

More information

Link:http://fssnip.net/88J
Posted:1 year ago
Author:shazmodan
Tags: string manipulation