4 people like it.

Change identifier case

Converts string to Pascal or camel case. Useful mostly for identifiers. Uses case changes to determine word boundaries.

 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: 
33: 
34: 
35: 
36: 
module Identifier =

    open System

    let changeCase toCamelCase (str : string) =
        if String.IsNullOrEmpty(str) then str
        else
            let isUpper = Char.IsUpper
            let n = str.Length
            let builder = new System.Text.StringBuilder()
            let append (s:string) = builder.Append(s) |> ignore
            let rec loop i j =
                let k = 
                    if i = n || (isUpper str.[i] && (not (isUpper str.[i - 1]) 
                                || ((i + 1) <> n && not (isUpper str.[i + 1])))) 
                        then
                        if j = 0 && toCamelCase then
                            append (str.Substring(j, i - j).ToLower())
                        elif (i - j) > 2 then
                            append (str.Substring(j, 1))
                            append (str.Substring(j + 1, i - j - 1).ToLower())
                        else
                            append (str.Substring(j, i - j))
                        i
                    else
                        j
                if i = n then builder.ToString()
                else loop (i + 1) k
            loop 1 0

    type System.String with
        member x.ToPascalCase() = changeCase false x
        member x.ToCamelCase() = changeCase true x

    printfn "%s" ("HTMLParser".ToPascalCase()) //prints: HtmlParser
    printfn "%s" ("HTMLParser".ToCamelCase()) //prints: htmlParser
namespace System
val changeCase : toCamelCase:bool -> str:string -> string

Full name: Script.Identifier.changeCase
val toCamelCase : bool
val str : string
Multiple items
val string : value:'T -> string

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

--------------------
type string = String

Full name: Microsoft.FSharp.Core.string
Multiple items
type String =
  new : value:char -> string + 7 overloads
  member Chars : int -> char
  member Clone : unit -> obj
  member CompareTo : value:obj -> int + 1 overload
  member Contains : value:string -> bool
  member CopyTo : sourceIndex:int * destination:char[] * destinationIndex:int * count:int -> unit
  member EndsWith : value:string -> bool + 2 overloads
  member Equals : obj:obj -> bool + 2 overloads
  member GetEnumerator : unit -> CharEnumerator
  member GetHashCode : unit -> int
  ...

Full name: System.String

--------------------
String(value: nativeptr<char>) : unit
String(value: nativeptr<sbyte>) : unit
String(value: char []) : unit
String(c: char, count: int) : unit
String(value: nativeptr<char>, startIndex: int, length: int) : unit
String(value: nativeptr<sbyte>, startIndex: int, length: int) : unit
String(value: char [], startIndex: int, length: int) : unit
String(value: nativeptr<sbyte>, startIndex: int, length: int, enc: Text.Encoding) : unit
String.IsNullOrEmpty(value: string) : bool
val isUpper : (char -> bool)
type Char =
  struct
    member CompareTo : value:obj -> int + 1 overload
    member Equals : obj:obj -> bool + 1 overload
    member GetHashCode : unit -> int
    member GetTypeCode : unit -> TypeCode
    member ToString : unit -> string + 1 overload
    static val MaxValue : char
    static val MinValue : char
    static member ConvertFromUtf32 : utf32:int -> string
    static member ConvertToUtf32 : highSurrogate:char * lowSurrogate:char -> int + 1 overload
    static member GetNumericValue : c:char -> float + 1 overload
    ...
  end

Full name: System.Char
Char.IsUpper(c: char) : bool
Char.IsUpper(s: string, index: int) : bool
val n : int
property String.Length: int
val builder : Text.StringBuilder
namespace System.Text
Multiple items
type StringBuilder =
  new : unit -> StringBuilder + 5 overloads
  member Append : value:string -> StringBuilder + 18 overloads
  member AppendFormat : format:string * arg0:obj -> StringBuilder + 4 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:char[] * destinationIndex:int * count:int -> unit
  member EnsureCapacity : capacity:int -> int
  member Equals : sb:StringBuilder -> bool
  ...

Full name: System.Text.StringBuilder

--------------------
Text.StringBuilder() : unit
Text.StringBuilder(capacity: int) : unit
Text.StringBuilder(value: string) : unit
Text.StringBuilder(value: string, capacity: int) : unit
Text.StringBuilder(capacity: int, maxCapacity: int) : unit
Text.StringBuilder(value: string, startIndex: int, length: int, capacity: int) : unit
val append : (string -> unit)
val s : string
Text.StringBuilder.Append(value: char []) : Text.StringBuilder
   (+0 other overloads)
Text.StringBuilder.Append(value: obj) : Text.StringBuilder
   (+0 other overloads)
Text.StringBuilder.Append(value: uint64) : Text.StringBuilder
   (+0 other overloads)
Text.StringBuilder.Append(value: uint32) : Text.StringBuilder
   (+0 other overloads)
Text.StringBuilder.Append(value: uint16) : Text.StringBuilder
   (+0 other overloads)
Text.StringBuilder.Append(value: decimal) : Text.StringBuilder
   (+0 other overloads)
Text.StringBuilder.Append(value: float) : Text.StringBuilder
   (+0 other overloads)
Text.StringBuilder.Append(value: float32) : Text.StringBuilder
   (+0 other overloads)
Text.StringBuilder.Append(value: int64) : Text.StringBuilder
   (+0 other overloads)
Text.StringBuilder.Append(value: int) : Text.StringBuilder
   (+0 other overloads)
val ignore : value:'T -> unit

Full name: Microsoft.FSharp.Core.Operators.ignore
val loop : (int -> int -> string)
val i : int
val j : int
val k : int
val not : value:bool -> bool

Full name: Microsoft.FSharp.Core.Operators.not
String.Substring(startIndex: int) : string
String.Substring(startIndex: int, length: int) : string
Text.StringBuilder.ToString() : string
Text.StringBuilder.ToString(startIndex: int, length: int) : string
val x : String
member String.ToPascalCase : unit -> string

Full name: Script.Identifier.ToPascalCase
member String.ToCamelCase : unit -> string

Full name: Script.Identifier.ToCamelCase
val printfn : format:Printf.TextWriterFormat<'T> -> 'T

Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.printfn
Raw view Test code New version

More information

Link:http://fssnip.net/28
Posted:13 years ago
Author:Daniel Robinson
Tags: strings , capitalization