2 people like it.

Strip HTML tags from a string (efficiently)

Uses StringBuilder to achieve performance.

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
let stripHtmlTags (input: seq<char>) =
    input
    |> Seq.fold
        (fun (flag,sb:(System.Text.StringBuilder)) c -> 
            match c with 
            |'<' -> true,sb
            |'>' -> false,sb
            |n -> 
                if flag then flag,sb
                else (flag, sb.Append(n))
        )
        (false,System.Text.StringBuilder())
    |> (fun (_,sb) -> sb.ToString())
val stripHtmlTags : input:seq<char> -> string
val input : seq<char>
Multiple items
val seq : sequence:seq<'T> -> seq<'T>

--------------------
type seq<'T> = System.Collections.Generic.IEnumerable<'T>
Multiple items
val char : value:'T -> char (requires member op_Explicit)

--------------------
[<Struct>]
type char = System.Char
module Seq

from Microsoft.FSharp.Collections
val fold : folder:('State -> 'T -> 'State) -> state:'State -> source:seq<'T> -> 'State
val flag : bool
val sb : System.Text.StringBuilder
namespace System
namespace System.Text
Multiple items
type StringBuilder =
  interface ISerializable
  new : unit -> unit + 8 overloads
  member Append : value: char * repeatCount: int -> StringBuilder + 23 overloads
  member AppendCore : value: StringBuilder * startIndex: int * count: int -> StringBuilder
  member AppendFormat : format: string * arg0: obj -> StringBuilder + 7 overloads
  member AppendFormatHelper : provider: IFormatProvider * format: string * args: ParamsArray -> StringBuilder
  member AppendHelper : value: string -> unit
  member AppendJoin : separator: string * [<ParamArray>] values: obj [] -> StringBuilder + 5 overloads
  member AppendJoinCore<'T> : separator: nativeptr<char> * separatorLength: int * values: IEnumerable<'T> -> StringBuilder + 1 overload
  member AppendLine : unit -> StringBuilder + 1 overload
  ...

--------------------
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 c : char
val n : char
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)
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/8aE
Posted:1 month ago
Author:shazmodan
Tags: html , string , stringbuilder