[turboid] javascript solutions

Home     Contact     Search     About me
Strings
20 Feb 2012

New String Functions for JavaScript

Finally including tokenizer, trim and replace functions

The JavaScript String Object provides quite good functions for analysing and manipulating string values. Yet there are some useful functions existing in other languages which we don't find in JavaScript. That's the niche where Turboid comes in.

Why do other languages know tokenizer or functions like trim(), which you can cut white spaces at the beginning and the end of each string away with? We don't know, but with Turboid you extend JavaScript with such a function. Another example for the usefulness of Turboid concerning strings is the following one. If you want to replace a word in a string at each place it occurs at, you indeed only have to use regular expressions this way:

	alert("Cats and birds and cows and mice.".replace(/and/g, "or"));

But not everybody is familiar with regular expressions, and it requires some time until one has learnt all the possibilities regular expressions offer. This is why Turboid provides the function replaceStrings(searchTerm, replacingTerm, sentence).

Below you find a list of Turboid string analyzing and manipulating functions:


replaceStrings(searchTerm, replacingTerm, sentence)

Replaces searchTerm by replacingTerm in sentence. If written replaceString() (without "s"), it replaces only the first occurence. The function is case sensitive. sentence would be returned unchanged if searchTerm has not been found. In order to work without case sensitivity, use replaceWords() or replaceWord().


replaceAt(pos, replacement, sentence)

Overwrites a part of sentence by replacement at pos (number value), which is the index of the character which the overwriting is supposed to start at. The part being overwritten has the length of the string passed in replacement.


trim(str)

Cuts away white spaces at the beginning and the end of str.


shortenSpaces(str)

Reduces each multiple white space to a single white space. Useful especially for user inputs.


tokenize(str, delimiter_1 [, delimiter_2, delimiter_3, ...])

Devides str by the passed delimiter(s) into parts and returns them as an array. This is one of the most useful string functions.


capitalize(str)

Returns a string (str) with the first letter set to upper case.


isCapitalized(str)

Returns whether str is upper case or not.


toCamelCase(str)

Converts CSS and CSS-like property names (e.g. "border-width") to camel case ("borderWidth"). This is useful because one has sometimes to switch between CSS (hyphens) and JavaScript (camel case) notation of CSS property names.


toCSSName(str)

Converts JavaScript notation of CSS property names to CSS notation.


randomString(length)

Generates a random string, with length as the number of letters.


hasMailFormat(emailaddress)

Returns whether a string (emailadress) is written in e-mail format, e.g. "someperson@xyz.org".


count(searchTerm, sentence)

Returns the number of occurences of searchTerm in sentence.


In order to use these extra functions, simply download the free Turboid framework and put it into the directory of your HTML file, then write this line into the head section of your HTML file, and that's it:

	

Alternatively, you can use the smaller Turboid core along with Turboid varTools if you don't want to download the whole framework.


Comments

Writer wrote on 2012-03-01 at 11:23:20 h:

Indeed, very useful functions.

Add comment:

Name: (required)
E-Mail: (required, remains invisible)
Website:
Comment:
Please check all (anti-spam):