3 people like it.

Tidy up text using composed functions

Some simple functions to tidy up text and quote it, so that it's suitable to go into a CSV.

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
14: 
15: 
16: 
17: 
18: 
19: 
20: 
21: 
22: 
23: 
24: 
25: 
26: 
27: 
28: 
29: 
30: 
31: 
32: 
/// Translate any directional single quotes to ordinary ones
let fixsingleq (s : string) =
    s.Replace('’', '\'').Replace('‘', '\'')

/// Translate any directional double quotes to ordinary ones
let fixdblq (s : string) =
    s.Replace('“', '\"').Replace('”', '\"')

/// Translate any directional quotes to ordinary ones
let fixspecialq =
    fixsingleq >> fixdblq

/// Double any double-quotes
let dblq (s : string) =
    s.Replace("\"", "\"\"")

/// Enclose in double-quotes
let quote (s : string) = sprintf "\"%s\"" (s.Trim())

/// Tabs to spaces
let tabtospace (s : string) = 
    s.Replace('\t', ' ')

/// Remove multiple spaces
let rec singlespace (s : string) = 
    if s.Contains("  ") then
        singlespace (s.Replace("  ", " "))
    else
        s

/// Clean and quote
let cq = tabtospace >> singlespace >> fixspecialq >> dblq >> quote 
val fixsingleq : s:string -> string

Full name: Script.fixsingleq


 Translate any directional single quotes to ordinary ones
val s : string
Multiple items
val string : value:'T -> string

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

--------------------
type string = System.String

Full name: Microsoft.FSharp.Core.string
System.String.Replace(oldValue: string, newValue: string) : string
System.String.Replace(oldChar: char, newChar: char) : string
val fixdblq : s:string -> string

Full name: Script.fixdblq


 Translate any directional double quotes to ordinary ones
val fixspecialq : (string -> string)

Full name: Script.fixspecialq


 Translate any directional quotes to ordinary ones
val dblq : s:string -> string

Full name: Script.dblq


 Double any double-quotes
val quote : s:string -> string

Full name: Script.quote


 Enclose in double-quotes
val sprintf : format:Printf.StringFormat<'T> -> 'T

Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.sprintf
System.String.Trim() : string
System.String.Trim([<System.ParamArray>] trimChars: char []) : string
val tabtospace : s:string -> string

Full name: Script.tabtospace


 Tabs to spaces
val singlespace : s:string -> string

Full name: Script.singlespace


 Remove multiple spaces
System.String.Contains(value: string) : bool
val cq : (string -> string)

Full name: Script.cq


 Clean and quote
Raw view Test code New version

More information

Link:http://fssnip.net/9E
Posted:13 years ago
Author:Kit Eason
Tags: composition operator; text; csv