7 people like it.

String.split

String split function that skips quoted strings, useful as a simple CSV parser

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
14: 
module internal String =
    let split separator (s:string) =
        let values = ResizeArray<_>()
        let rec gather start i =
            let add () = s.Substring(start,i-start) |> values.Add
            if i = s.Length then add()
            elif s.[i] = '"' then inQuotes start (i+1) 
            elif s.[i] = separator then add(); gather (i+1) (i+1) 
            else gather start (i+1)
        and inQuotes start i =
            if s.[i] = '"' then gather start (i+1)
            else inQuotes start (i+1)
        gather 0 0
        values.ToArray()
module String

from Microsoft.FSharp.Core
val internal split : separator:char -> s:string -> string []

Full name: Script.String.split
val separator : char
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
val values : System.Collections.Generic.List<string>
type ResizeArray<'T> = System.Collections.Generic.List<'T>

Full name: Microsoft.FSharp.Collections.ResizeArray<_>
val gather : (int -> int -> unit)
val start : int
val i : int
val add : (unit -> unit)
System.String.Substring(startIndex: int) : string
System.String.Substring(startIndex: int, length: int) : string
System.Collections.Generic.List.Add(item: string) : unit
property System.String.Length: int
val inQuotes : (int -> int -> unit)
System.Collections.Generic.List.ToArray() : string []
Raw view Test code New version

More information

Link:http://fssnip.net/iK
Posted:11 years ago
Author:Phillip Trelford
Tags: string , csv