Additional functions for working with Strings
Uppercase the first character of each word in a string
toTitleCase "this is a phrase" == "This Is A Phrase"
toTitleCase "hello, world" == "Hello, World"Make a string's first character lowercase.
decapitalize "This is a phrase" == "this is a phrase"
decapitalize "Hello, World" == "hello, World"Functions borrowed from the Rails Inflector class
Converts underscored or dasherized string to a camelized one.
camelize "-moz-transform" == "MozTransform"
Converts string to camelized string starting with an uppercase. All non word characters will be stripped out of the original string.
classify "some_class_name" == "SomeClassName" classify "myLittleCamel.class.name" == "MyLittleCamelClassName"
Returns a string joined by underscores after separating it by its uppercase characters. Any sequence of spaces or dashes will also be converted to a single underscore. The final string will be lowercased
underscore "SomeClassName" == "some_class_name" underscore "some-class-name" == "some_class_name" underscore "SomeClass name" == "some_class_name
Returns a string joined by dashes after separating it by its uppercase characters. Any sequence of spaces or underscored will also be converted to a single dash. The final string will be lowercased
dasherize "SomeClassName" == "-some-class-name" dasherize "some_class_name" = "some-class-name" dasherize "someClass name" = "some-class-name"
Converts an underscored, camelized, or dasherized string into one that can be read by humans. Also removes beginning and ending whitespace, and removes the postfix '_id'. The first character will be capitalized
humanize "this_is_great" == "This is great" humanize "ThisIsGreat" = "This is great" humanize "this-is-great" = "This is great" humanize "author_id" = "Author"
Replace all occurrences of the search string with the substitution string.
replace "Mary" "Sue" "Hello, Mary" == "Hello, Sue"Replace text within a portion of a string given a substitution string, a start index and an end index.
replaceSlice "Sue" 4 6 "Hi, Bob" == "Hi, Sue"
replaceSlice "elephants" 0 6 "snakes on a plane!" == "elephants on a plane!"
replaceSlice "under" 7 9 "snakes on a plane!" == "snakes under a plane!"Inserts a substring at the specified index.
insertAt "world" 6 "Hello " == "Hello world"Trims the whitespace of both sides of the string and compresses reapeated whitespace internally to a single whitespace char.
clean " The quick brown fox " == "The quick brown fox"Breaks a string into a list of strings of maximum the provided size.
break 10 "The quick brown fox" == ["The quick ", "brown fox"]
break 2 "" == [""]Breaks a string into a list of strings of maximum the provided size, without cutting words at the edge.
softBreak 6 "The quick brown fox" == ["The quick", " brown", " fox"]Chops a given string into parts of a given width, seperating them using a new line.
wrap 7 "My very long text" === "My very\nlong te\nxt"
wrap 100 "Too short" === "Too short"Separates a string into parts of a given width, using a given seperator.
Look at wrap if you just want to wrap using newlines.
wrapWith 7 "\n" "My very long text" === "My very\nlong text"
wrapWith 100 "\n" "Too short" === "Too short"Chops a given string into parts of a given width without breaking works apart, and then seperating them using a new line.
softWrap 7 "My very long text" === "My very\nlong text"
softWrap 3 "Hello World" === "Hello \nWorld"
softWrap 100 "Too short" === "Too short"Chops a given string into parts of a given width without breaking works apart, and then seperating them using the given separator.
softWrapWith 7 "..." "My very long text" === "My very...long text"
softWrapWith 3 "\n" Hello World" === "Hello \nWorld"
softWrapWith 100 "\t" "Too short" === "Too short"Adds quotes to a string.
quote "foo" == ""barfoobar""
Surrounds a string with another string.
surround "bar" "foo" == "barfoobar"
Tests if a string is empty or only contains whitespace
isBlank "" == True isBlank "\n" == True isBlank " " == True isBlank " a" == False
Returns the number of occurrences of a substring in another string
countOccurrences "Hello" "Hello World" == 1 countOccurrences "o" "Hello World" == 2
Trims the whitespace of both sides of the string and compresses reapeated whitespace internally to a single whitespace char.
clean " The quick brown fox " == "The quick brown fox"Removes quotes that surround a string.
unquote ""foo"" == "foo" unquote ""foo"bar""
Removes surrounding strings from another string.
unsurround "foo" "foobarfoo" == "bar"
Removes the least sequence of leading spaces or tabs on each line of the string, so that at least one of the lines will not have any leading spaces nor tabs and the rest of the lines will have the same amount of indentation removed.
unindent " Hello\n World " == "Hello\n World"
unindent "\t\tHello\n\t\t\t\tWorld" == "Hello\n\t\tWorld"Truncates the string at the specified length and appends three dots only if the tructated string + the 3 dots have exactly the desired lenght.
The resulting string will have at most the specified lenght
ellipsis 5 "Hello World" == "Hello..." ellipsis 10 "Hello World" == "Hello W..." ellipsis 10 "Hello" == "Hello" ellipsis 8 "Hello World" == "Hello World"
Truncates the string at the specified length and appends three dots only if the tructated string + the 3 dots have exactly the desired lenght.
In constrast to ellipsis, this method will produced unfinished words,
instead, it will find the closest complete word and apply the ellipsis from
there.
Additionally, it will remove any trailing whitespace and punctuation characters at the end of the truncated string.
The resulting stirng can in some cases exceed the specifed lenght, by at most three characters.
softEllipsis 5 "Hello, World" == "Hello..." softEllipsis 8 "Hello, World" == "Hello..." softEllipsis 15 "Hello, cruel world" == "Hello, cruel..." softEllipsis 10 "Hello" == "Hello"
Truncates the string at the specified lenght and adds the append string only if the combined lenght of the truncated string and the append string have exactly the desired lenght.
The resulting string will have at most the specified lenght
ellipsisWith 5 " .." "Hello World" == "Hello .." ellipsisWith 10 " .." "Hello World" == "Hello W..." ellipsisWith 10 " .." "Hello" == "Hello" ellipsisWith 8 " .." "Hello World" == "Hello World"
Removes all HTML tags from the string, preserving the text inside them. stripTags "a link" == "a link" stripTags "alert('hello world!') == "alert('hello world!')"
Converts a list of strings into a human formatted readable list toSentence [] == "" toSentence ["lions"] == "lions" toSentence ["lions", "tigers"] == "lions and tigers" toSentence ["lions", "tigers", "bears"] == "lions, tigers and bears"
Converts a list of strings into a human formatted readable list using an oxford comma toSentence [] == "" toSentence ["lions"] == "lions" toSentence ["lions", "tigers"] == "lions and tigers" toSentence ["lions", "tigers", "bears"] == "lions, tigers, and bears"
Searches a string from left to right for a pattern and returns a substring consisting of the characters in the string that are to the right of the pattern.
rightOf "_" "This_is_a_test_string" == "is_a_test_string"Searches a string from left to right for a pattern and returns a substring consisting of the characters in the string that are to the left of the pattern.
lefttOf "_" "This_is_a_test_string" == "This"Searches a string from right to left for a pattern and returns a substring consisting of the characters in the string that are to the right of the pattern.
rightOfBack "_" "This_is_a_test_string" == "string"Searches a string from right to left for a pattern and returns a substring consisting of the characters in the string that are to the right of the pattern.
leftOfBack "_" "This_is_a_test_string" == "This_is_a_test"
Make a string's first character uppercase