home *** CD-ROM | disk | FTP | other *** search
- /****************************************************************************\
- * *
- * String.js -- A collection of string manipulation functions *
- * *
- * This contains several useful functions for string manipulation. All this *
- * can be done with the String and StringEx objects, but this makes it just *
- * a little easier. To load a function library in IntraBuilder use this *
- * type of command: *
- * *
- * _sys.scripts.load(_sys.env.home() + "apps\\shared\\string.js"); *
- * *
- * Functions included are: *
- * *
- * alltrim(<str>) - removes leading and trailing spaces *
- * escapeChar(<str>,<char>) - places an escape indicator "\" before each *
- * <char> in <str> and returns the new string. *
- * ltrim(<str>) - removes spaces on the left side of the string *
- * pad(<str>,<len>) - adds enough spaces to string to make it's length <len> *
- * rtrim(<str>) - removes spaces on the right side of the string *
- * str(<val>,<len>,<dec>) - converts a number to a string of length <len>, *
- * having <dec> decimal places. <len> defaults to *
- * length of number, <dec> defaults to 0. *
- * *
- * Updated 11/11/96 by IntraBuilder Samples Group *
- * $Revision: 1.1 $ *
- * *
- * Copyright (c) 1996, Borland International, Inc. All rights reserved. *
- * *
- \****************************************************************************/
-
- function alltrim( str ) {
- var stringEx = new StringEx(str);
- stringEx.string = stringEx.rightTrim();
- return (stringEx.leftTrim());
- }
-
- function escapeChar(str,chr)
- {
- var offset = last = 0;
- var returnVal = "";
- while(str.indexOf(chr,last) >= 0) {
- offset = str.indexOf(chr,last);
- returnVal += str.substring(last, offset) + "\\" + chr;
- last = offset+1;
- }
- returnVal += str.substring(last,str.length);
- return returnVal;
- }
-
- function ltrim( str ) {
- var stringEx = new StringEx(str);
- return (stringEx.leftTrim());
- }
-
- function pad(str, len) {
- var s = new StringEx(str);
- s.string = s.string + s.replicate(" ",len);
- return(s.left(len))
- }
-
- function rtrim( str ) {
- var stringEx = new StringEx(str);
- return (stringEx.rightTrim());
- }
-
- /* str( <expN> [,<len> [,<dec>]] )
-
- where expN is any numeric value
- len is the length of the character string
- to return. If a len is not passed, then
- the return string is only as large as
- the number itself.
- dec is the number of decimal places in the
- return string. The default is 0.
-
- returns str() returns a character string containing
- the number passed.
- */
-
- function str(num,len,dec) {
- // set the default values, depending on how many
- // parameters where passed.
- if (str.arguments.length == 0) {
- return "";
- }
- else if (str.arguments.length == 1) {
- len = 0;
- dec = 0;
- }
- else if (str.arguments.length == 2) {
- dec = 0;
- }
-
- // initialize needed objects
- var return_str = new StringEx();
- var stringEx = new StringEx();
- var math = new Math();
-
- // check the number of decimals
- return_str.string = ("" + math.round(num*math.pow(10,dec))/math.pow(10,dec));
-
- // remove leading/trailing spaces
- return_str.string = return_str.leftTrim();
- return_str.string = return_str.rightTrim();
-
- // set the correct number of decimal places
- if (return_str.indexOf(".") == 0) {
- return_str = return_str + ".";
- }
- return_str.string = return_str.string + stringEx.replicate("0",dec);
- return_str.string = return_str.left(return_str.indexOf('.') + (dec==0?0:1) + dec); //return_str.string,
-
- // add leading spaces to get correct length. If
- // actual length is greater then len, return overflow
- if (len == 0) {
- // Do nothing. The string is fine.
- }
- else if (return_str.length > len) {
- return_str.string = stringEx.replicate("*",len);
- }
- else {
- return_str.string = stringEx.replicate(" ",len) + return_str.string;
- return_str.string = return_str.right(len); //return_str.string,
- }
-
- // that's it
- return (return_str.string);
- }
-
-
-
-