String

JavaScript lacks a couple of obvious features which the String namespace provides.

Trim
Use the Trim function to remove whitespaces from the beginning and end of a string.

var str = " hello world    ";
str = Fit.String.Trim(str); // Returns "hello world"


UpperCaseFirst
Easily make sure a string starts with a capital letter.

var name = "james";
name = Fit.String.UpperCaseFirst(name); // Returns "James"

StripHtml
Remove any HTML contained in string to get the raw text representation

var str = "<b>Hi James</b>, <br><br>Welcome to our website.";
str = Fit.String.StripHtml(str); // Returns "Hi James, Welcome to our website."

Encode and Decode HTML
Use EncodeHtml to encode special HTML characters into HTML entities, and DecodeHtml to decode HTML entities into actual characters.

var html = "<b>Hello world</b>";
alert(html); // Displays "<b>Hello world</b>"

var encoded = Fit.String.EncodeHtml(html);
alert(html); // Displays "&lt;b&gt;Hello world&lt;/b&gt;

var decoded = Fit.String.EncodeHtml(encoded);
alert(decoded); // Displays "<b>Hello world</b>"


Hash
The Hash function allows us to generate a Java compatible hash code (integer) based on a given string. Be aware that the hash generated is not quaranteed to be unique.

var val = "Hello world";
var hash = Fit.String.Hash(val); // Returns -832992604
 
Fit.UI is open source (LGPL) - download or fork it on GitHub - website powered by Sitemagic CMS