jsbeans.string

back to jsbeans index

jsbeans.string provides utility methods to manage and manipulate strings.
As the trim function is widely present in javascript libraries, it can be found in jsbeans.string too, but with some other useful methods.
Starts with
The one you aspected: checks if a specified string starts (same character sequence) with a given string.
:
:
:

code

	
Ends with
The opposite of jsbeans.string.startsWith: it checks if a string ends with a given one.
:
:
:

code

	
Empty/Null
Checks if a string is empty ("") or null. Can be called in both ways: jsbeans.string.isEmptyOrNull or jsbeans.string.isNullOrEmpty.
:


code

	
Trim
Apart from a common trim function, jsbeans has a trimLeft and a trimRight functions too.
:




code

	
Ellipsis
jsbeans.string.ellipsis truncate a text after a given length adding a defined string ("..." as default) at the end of text. No word is cut and the final length considers the ellipsis string.

:
:
:

code

		
Email encrypter
This is a way not to include emails address directly in the page.
Test for email myname@mydomain.com as an anchor: <a href="mailto:myname@mydomain.com" title="Write me">Write me</a>
File size
An useful method to format file size starting from byte to human readable format such as 1 MB, ...



code

	
Parse integers
Native javascript parseInt function has a weird behaviour because it tries to use different radixes (octal if parameter begins with '0', hexadecimal for '0x', decimal otherwise). In fact parseInt("0w") returns 0. (see here)
jsbeans.string.parseInt("0w") returns NaN.
As NOT for the native one jsbeans.string.parseInt removes all leading "0". As result jsbeans.string.parseInt("00001") returns 1.

code

	
From/to hexadecimal
These methods provide transformation of string to thier equivalent hexadecimal and viceversa.

code

	
Remove match
A simple way to remove part of string inside a given one.



code

	
Parse booleans
Trying to guess if given string can be converted to a boolean value.
'1', 'true' or 't' (both case insensitive) return true (as boolean).
Optionally a second argument can be passed and it will be used to match against the given string.



code

	
Color conversion (hex -> rgb and viceversa)
Converting hexdecimal format to their RGB equivalent and viceversa is a common issue
Red Green Blue   Hexdecimal


code

		
 
Format
Replaces positional placeholder in the form {n}, where 'n' is an integer, within a given string.
See HTML for JSON equivalent!
Please note: in this example, if no value is provided, an empty string will be passed with no additional checks!
String to format: My name is {0} and my email is {1}. I said: my name is {0}.
:
:

code