Jel. String

Utility Methods for manipulating JavaScript’s built-in String class

Summary
Utility Methods for manipulating JavaScript’s built-in String class
gets the repeat of a string for a specified count
gets the specified number of characters from the end of a given string
gets the specified number of characters from the beginning of a given string
removes whitespace characters from the beginning of a given string
removes whitespace characters from the end of a given string
removes whitespace characters from the beginning and end of a given string
gets the float value of a given string in manner safe for arithmetic expressions
gets the integer value of a given string in a manner that is safe for arithmetic expressions
extracts the digits in a given string, returning the integer value of them joined together in sequence
checks if a given string is equal to another string, with the optional case-insensitive comparison
checks if a given string begins with another string, with the optional case-insensitive comparison
checks if a given string ends with another string, with the option of a case-insensitive comparison
breaks a given string up into a string of lowercase delimited words, where an uppercase letter in a given string denotes a new word (camel case)
converts a string of dash-delimited words into a camelized version
converts a string of words into the equivalent title cased word
remove the file extension from a given string
gets the file extension for a given string
converts a string of words which are camel-cased, dash-delimited, underscore delimted, or a combination into a string of dash-delimited words
converts a string of words which are camel-cased, dash-delimited, underscore delimted, or a combination into a style approprite for constant values, that is, a string of underscore-delimited uppercase words

Functions

repeat

repeat: function(str,
count)

gets the repeat of a string for a specified count

Arguments

strstring, the string to repeat
countinteger, the delimiter to use between words.  If not specified, a dash (“-”) is used

Returns

string

Examples

String.repeat("-", 5);     // "-----"
String.repeat("hello", 3); // "hellohellohello"

right

right: function(str,
length)

gets the specified number of characters from the end of a given string

Arguments

strString, a given string
lengththe number of characters to return

Example

Jel.String.right("wicked", 3); // "ked"

left

left: function(str,
length)

gets the specified number of characters from the beginning of a given string

Arguments

strString, a given string
lengththe number of characters to return

Example

Jel.String.left("wicked", 4); // "wick"

ltrim

ltrim: function(str)

removes whitespace characters from the beginning of a given string

Arguments

strString, a given string

Examples

Jel.String.lrim("  wicked");      // "wicked"
Jel.String.lrim(" wicked "); // "wicked "

rtrim

rtrim: function(str)

removes whitespace characters from the end of a given string

Arguments

strString, a given string

Examples

Jel.String.rtrim("wicked");       // "wicked"
Jel.String.rtrim(" wicked "); // " wicked"

trim

trim: function(str)

removes whitespace characters from the beginning and end of a given string

Arguments

strString, a given string

Example

Jel.String.trim("   wicked  "); // "wicked"

toFloat

toFloat: function(str)

gets the float value of a given string in manner safe for arithmetic expressions

Arguments

strString, a given string

Returns

floatthe float value of the string, if it can be converted into one
0.0if the string does not represent a float

Examples

Jel.String.toFloat("0.5");                                  // 0.5
Jel.String.toFloat("word"); // 0.0
Jel.String.toFloat("0.6") + Jel.String.toFloat("word"); // 0.6

toInt

toInt: function(str)

gets the integer value of a given string in a manner that is safe for arithmetic expressions

Arguments

strString, a given string

Returns

integerthe integer value of the string, if it can be converted into one
0if the string does not represent an integer

Examples

Jel.String.toInt("4");          // 4
Jel.String.toInt("word"); // 0
Jel.String.toInt(8 + "word"); // 8

extractInt

extractInt: function(str)

extracts the digits in a given string, returning the integer value of them joined together in sequence

Arguments

strString, a given string

Returns

integerthe integer value of the all of the digits joined together in a given string, if any are present
0otherwise

Examples

Jel.String.extractInt("4jkbn45");   // 445
Jel.String.extractInt("word"); // 0
Jel.String.extractInt("ff88f999"); // 88999

eq

equals: function(str,
strCompare,
ignoreCase)

checks if a given string is equal to another string, with the optional case-insensitive comparison

Arguments

strString, a given string
strComparestring, the string to compare to
ignoreCaseboolean, whether the comparison is case sensitive or not

Returns

trueif they are equal
falseotherwise

Examples

Jel.String.equals("clay", "blah");        // false
Jel.String.equals("word", "WORD", true); // true

startsWith

startsWith: function(str,
strCompare,
ignoreCase)

checks if a given string begins with another string, with the optional case-insensitive comparison

Arguments

strString, a given string
strComparestring, the string to compare to
ignoreCaseboolean, whether the comparison is case sensitive or not

Returns

trueif a given string begins with str
falseotherwise

Examples

Jel.String.startsWith("word", "w");                       // true
Jel.String.startsWith("field-container", "FIELD", false); // true
Jel.String.startsWith("word", "p"); // false

endsWith

endsWith: function(str,
strCompare,
ignoreCase)

checks if a given string ends with another string, with the option of a case-insensitive comparison

Arguments

strString, a given string
strComparestring, the string to compare to
ignoreCaseboolean, whether the comparison is case sensitive or not

Returns

trueif a given string ends with str
falseotherwise

Examples

Jel.String.endsWith("word", "w");                           // false
Jel.String.endsWith("field-container", "CONTAINER", false); // true

decamelize

decamelize: function(str,
delimiter)

breaks a given string up into a string of lowercase delimited words, where an uppercase letter in a given string denotes a new word (camel case)

Arguments

strString, the string to decamelize
delimiterString, the delimiter to use between words.  If not specified, a dash (“-”) is used

Returns

stringthe final delimited words string

Examples

Jel.String.decamelize("fieldValidator");   // "field-validator"
Jel.String.decamelize("helloThere", " "); // "hello there"
Jel.String.decamelize("getValue", "_"); // "get_value"

camelize

camelize: function(str,
delimiter)

converts a string of dash-delimited words into a camelized version

Arguments

strthe string to camelize
delimiterthe delimiter to use between words.  If not specified, a dash (“-”) is used

Returns

stringthe final camel cased words string

Credit

based on camelize in Prototype.js 1.5 © 2005-2007 Sam Stephenson

Example

Jel.String.camelize("field-validator"); // "fieldValidator"

titleCase

titleCase: function(str)

converts a string of words into the equivalent title cased word

Arguments

strString, the string of words to convert

Returns

stringthe final title-cased words string

Example

Jel.String.titleCase("field validator"); // "Field Validator"

removeExtension

removeExtension: function(str)

remove the file extension from a given string

Arguments

strString

Returns

stringthe final string with extension removed. note that only the final period will be regarded as the start of the file extension (see example 2 below)

Example

Jel.String.removeExtension("index.php"); // "index"
Jel.String.removeExtension("index.alternate.php"); // "index.alternate"

getExtension

getExtension: function(str)

gets the file extension for a given string

Arguments

strString

Returns

stringthe file extension, or the empty string if none is found

Example

Jel.String.getExtension("index.php"); // "php"
Jel.String.getExtension("index.alternate.php"); // "php"
Jel.String.getExtension("index"); // "" (no extension)

normalize

normalize: function(str)

converts a string of words which are camel-cased, dash-delimited, underscore delimted, or a combination into a string of dash-delimited words

Arguments

strString, the string of words to convert

Returns

stringthe final dash-delimited words string

Example

Jel.String.normalize("fieldValidator_1"); // "field-validator-1"

constant

constant: function(str)

converts a string of words which are camel-cased, dash-delimited, underscore delimted, or a combination into a style approprite for constant values, that is, a string of underscore-delimited uppercase words

Arguments

strString, the string of words to convert

Returns

stringthe final uppercase underscore-delimited words string

Example

Jel.String.normalize("fieldValidator_1"); // "FIELD_VALIDATOR_1"
repeat: function(str,
count)
gets the repeat of a string for a specified count
right: function(str,
length)
gets the specified number of characters from the end of a given string
left: function(str,
length)
gets the specified number of characters from the beginning of a given string
ltrim: function(str)
removes whitespace characters from the beginning of a given string
rtrim: function(str)
removes whitespace characters from the end of a given string
trim: function(str)
removes whitespace characters from the beginning and end of a given string
toFloat: function(str)
gets the float value of a given string in manner safe for arithmetic expressions
toInt: function(str)
gets the integer value of a given string in a manner that is safe for arithmetic expressions
extractInt: function(str)
extracts the digits in a given string, returning the integer value of them joined together in sequence
equals: function(str,
strCompare,
ignoreCase)
checks if a given string is equal to another string, with the optional case-insensitive comparison
startsWith: function(str,
strCompare,
ignoreCase)
checks if a given string begins with another string, with the optional case-insensitive comparison
endsWith: function(str,
strCompare,
ignoreCase)
checks if a given string ends with another string, with the option of a case-insensitive comparison
decamelize: function(str,
delimiter)
breaks a given string up into a string of lowercase delimited words, where an uppercase letter in a given string denotes a new word (camel case)
camelize: function(str,
delimiter)
converts a string of dash-delimited words into a camelized version
titleCase: function(str)
converts a string of words into the equivalent title cased word
removeExtension: function(str)
remove the file extension from a given string
getExtension: function(str)
gets the file extension for a given string
normalize: function(str)
converts a string of words which are camel-cased, dash-delimited, underscore delimted, or a combination into a string of dash-delimited words
constant: function(str)
converts a string of words which are camel-cased, dash-delimited, underscore delimted, or a combination into a style approprite for constant values, that is, a string of underscore-delimited uppercase words