Notes by James

Displaying keyword search results 1 - 12
Created by James on March 07, 2011 15:49:11    Last update: March 07, 2011 15:49:11
The JavaScript string match method matches the string against a regular expression and returns the matches. // returns ['av'] 'The JavaScript string ma...
Created by James on March 03, 2011 21:48:52    Last update: March 03, 2011 21:48:52
Just another way to left zero-pad a string. function zeroPad(s, length) { if (s.length ...
Created by James on March 03, 2011 20:50:51    Last update: March 03, 2011 20:51:54
This function parses the querystring and returns the query parameters as a map. It calls the string replace function with a function replacement and uses the side effects of the replacement function call to populate the parameter map. function getQueryParameters() { var result ...
Created by James on March 03, 2011 20:29:52    Last update: March 03, 2011 20:30:36
The general syntax for JavaScript string replace is: str.replace(regexp|substr, newSubStr|function[, |n... i.e., The substring to be replaced can be either a string or a regular expression (regex) The replacement string can be a string, or a function which returns the replacement. If the second parameter is a function, the function will be invoked after the match has been performed. The parameters passed to the function are: the matched (to be replaced) substring, the parenthesized submatch strings, offset of the match, and the whole string on which replace is being called. It can take an optional flags argument, but that's not standard . Examples: // replace apples with oranges // only the ...
Created by James on October 25, 2010 04:45:59    Last update: October 25, 2010 04:45:59
The test method for JavaScript regular expression tests if a string matches the regular expression. Below are some examples: /son/i.test('Jack Jake Jason'); // true /ja/.te...
Created by James on September 03, 2010 15:34:30    Last update: September 03, 2010 15:34:43
Combine the elements of a JavaScript array to a string by the join method: a = [ 'Eeny', 'meeny', 'miny', 'moe' ]; a.join(...
Created by James on June 29, 2010 19:11:54    Last update: July 23, 2010 21:23:24
import java.util.Random; public class Gener...
Created by James on July 06, 2010 19:35:00    Last update: July 06, 2010 19:35:00
Java has built-in functions to get the basename and dirname for a given file path, but the function names are not so self-apparent. import java.io.File; public class JavaFileD... Results: C:\tmp>java JavaFileDirNameBaseName Dirname: .....
Created by James on August 10, 2009 04:02:25    Last update: August 10, 2009 04:16:31
Function Description Examples length This is actually a property. 'abcd'.length == 4 charAt(int) Return character at given index 'abcd'.charAt(2) == 'c' (index starts from 0) indexOf(string) Return first index of given string 'abcdcdcd'.indexOf('cd') == 2 lastIndexOf(string) Return last index of given string 'abcdcdcd'.lastIndexOf('cd') == 6 substring(beginIndex, endIndex) Return substring starting from beginIndex and ending at endIndex 'abcdcdcd'.substring(1, 3) == 'bc' 'abcdcdcd'.substring(1, 300) == 'bcdcdcd' split(string) Split string into array of strings using given string as delimiter '/home/jsmith/download/js/'.split('/') == ['', 'home', 'jsmith', 'download', 'js', ''] toLowerCase() Convert string to lower case 'AbCd'.toLowerCase() == 'abcd' toUpperCase() Convert string to upper case 'AbCd'.toUpperCase() == 'ABCD'
Created by James on August 09, 2009 03:57:04    Last update: August 09, 2009 03:57:04
Contrary to some online documentation, JavaScript does not seem to support the functions startsWith , endsWith etc. This snippet adds these functions. String.prototype.endsWith = function (a) { ...
Created by James on May 23, 2009 19:46:01    Last update: May 23, 2009 19:49:46
This topic comes up once in a while: I need to generate a random string in JavaScript to get around browser (IE) cacheing. Method 1 (not really random, but a new value every millisecond): function randomString() { return '' + n... Method 2 (generate a random alpha-numerical string): function randomString(length) { var chars =...
Created by James on May 03, 2009 20:05:03    Last update: May 03, 2009 20:05:03
If you create two new String objects by calling the String constructor, the two objects are not equal: a = new String('a'); b = new String('a'); ... An object created with the String constructor is of type object , while a literal string object is of type string ! But you can coerce the object back to string by using the concatenation operator: alert(typeof(a)); // object ale...