Jel. StringUtility 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 |
repeat| repeat: function( | str, | | count | ) |
|
gets the repeat of a string for a specified count Arguments| str | string, the string to repeat | | count | integer, the delimiter to use between words. If not specified, a dash (“-”) is used |
Returnsstring ExamplesString.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| str | String, a given string | | length | the number of characters to return |
ExampleJel.String.right("wicked", 3); // "ked"
left| left: function( | str, | | length | ) |
|
gets the specified number of characters from the beginning of a given string Arguments| str | String, a given string | | length | the number of characters to return |
ExampleJel.String.left("wicked", 4); // "wick"
ltrimremoves whitespace characters from the beginning of a given string Arguments| str | String, a given string |
ExamplesJel.String.lrim(" wicked"); // "wicked" Jel.String.lrim(" wicked "); // "wicked "
rtrimremoves whitespace characters from the end of a given string Arguments| str | String, a given string |
ExamplesJel.String.rtrim("wicked"); // "wicked" Jel.String.rtrim(" wicked "); // " wicked"
trimremoves whitespace characters from the beginning and end of a given string Arguments| str | String, a given string |
ExampleJel.String.trim(" wicked "); // "wicked"
toFloatgets the float value of a given string in manner safe for arithmetic expressions Arguments| str | String, a given string |
Returns| float | the float value of the string, if it can be converted into one | | 0.0 | if the string does not represent a float |
ExamplesJel.String.toFloat("0.5"); // 0.5 Jel.String.toFloat("word"); // 0.0 Jel.String.toFloat("0.6") + Jel.String.toFloat("word"); // 0.6
toIntgets the integer value of a given string in a manner that is safe for arithmetic expressions Arguments| str | String, a given string |
Returns| integer | the integer value of the string, if it can be converted into one | | 0 | if the string does not represent an integer |
ExamplesJel.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| str | String, a given string |
Returns| integer | the integer value of the all of the digits joined together in a given string, if any are present | | 0 | otherwise |
ExamplesJel.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| str | String, a given string | | strCompare | string, the string to compare to | | ignoreCase | boolean, whether the comparison is case sensitive or not |
Returns| true | if they are equal | | false | otherwise |
ExamplesJel.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| str | String, a given string | | strCompare | string, the string to compare to | | ignoreCase | boolean, whether the comparison is case sensitive or not |
Returns| true | if a given string begins with str | | false | otherwise |
ExamplesJel.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| str | String, a given string | | strCompare | string, the string to compare to | | ignoreCase | boolean, whether the comparison is case sensitive or not |
Returns| true | if a given string ends with str | | false | otherwise |
ExamplesJel.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| str | String, the string to decamelize | | delimiter | String, the delimiter to use between words. If not specified, a dash (“-”) is used |
Returns| string | the final delimited words string |
ExamplesJel.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| str | the string to camelize | | delimiter | the delimiter to use between words. If not specified, a dash (“-”) is used |
Returns| string | the final camel cased words string |
Creditbased on camelize in Prototype.js 1.5 © 2005-2007 Sam Stephenson ExampleJel.String.camelize("field-validator"); // "fieldValidator"
titleCaseconverts a string of words into the equivalent title cased word Arguments| str | String, the string of words to convert |
Returns| string | the final title-cased words string |
ExampleJel.String.titleCase("field validator"); // "Field Validator"
removeExtension| removeExtension: function( | str | ) |
|
remove the file extension from a given string ArgumentsReturns| string | the 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) |
ExampleJel.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 ArgumentsReturns| string | the file extension, or the empty string if none is found |
ExampleJel.String.getExtension("index.php"); // "php" Jel.String.getExtension("index.alternate.php"); // "php" Jel.String.getExtension("index"); // "" (no extension)
normalizeconverts a string of words which are camel-cased, dash-delimited, underscore delimted, or a combination into a string of dash-delimited words Arguments| str | String, the string of words to convert |
Returns| string | the final dash-delimited words string |
ExampleJel.String.normalize("fieldValidator_1"); // "field-validator-1"
constantconverts 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| str | String, the string of words to convert |
Returns| string | the final uppercase underscore-delimited words string |
ExampleJel.String.normalize("fieldValidator_1"); // "FIELD_VALIDATOR_1"
|