home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 8 / CDACTUAL8.iso / progs / ite / ite10d1.exe / DATA.Z / STRING.JS < prev    next >
Encoding:
JavaScript  |  1996-09-07  |  4.6 KB  |  113 lines

  1. /****************************************************************************\
  2. *                                                                            *
  3. * String.js  --  A collection of string manipulation functions               *
  4. *                                                                            *
  5. * This contains several useful functions for string manipulation. All this   *
  6. * can be done with the String and StringEx objects, but this makes it just   *
  7. * a little easier. To load a function library in IntraBuilder use this       *
  8. * type of command:                                                           *
  9. *                                                                            *
  10. *     _sys.scripts.load(_sys.env.home() + "apps\\shared\\string.js");        *
  11. *                                                                            *
  12. * Functions included are:                                                    *
  13. *                                                                            *
  14. * alltrim(<str>)   - removes leading and trailing spaces                     *
  15. * ltrim(<str>)     - removes spaces on the left side of the string           *
  16. * pad(<str>,<len>) - adds enough spaces to string to make it's length <len>  *
  17. * rtrim(<str>)     - removes spaces on the right side of the string          *
  18. * str(<val>,<len>,<dec>) - converts a number to a string of length <len>,    *
  19. *                          having <dec> decimal places. <len> defaults to    *
  20. *                          length of number, <dec> defaults to 0.            *
  21. *                                                                            *
  22. * Updated 7/11/96 by IntraBuilder Samples Group                              *
  23. * $Revision:   1.0  $                                                       *
  24. *                                                                            *
  25. * Copyright (c) 1996, Borland International, Inc. All rights reserved.       *
  26. *                                                                            *
  27. \****************************************************************************/
  28.  
  29. function alltrim( str ) {
  30.    var stringEx = new StringEx(str);
  31.    stringEx.string = stringEx.rightTrim();
  32.    return (stringEx.leftTrim());
  33. }
  34.  
  35. function ltrim( str ) {
  36.    var stringEx = new StringEx(str);
  37.    return (stringEx.leftTrim());
  38. }
  39.  
  40. function pad(str, len) {
  41.    var s = new StringEx(str);
  42.    s.string = s.string + s.replicate(" ",len);
  43.    return(s.left(len))   
  44. }
  45.  
  46. function rtrim( str ) {
  47.    var stringEx = new StringEx(str);
  48.    return (stringEx.rightTrim());
  49. }
  50.  
  51. /*   str( <expN> [,<len> [,<dec>]] )
  52.  
  53.      where expN  is any numeric value
  54.            len   is the length of the character string
  55.                  to return. If a len is not passed, then
  56.                  the return string is only as large as
  57.                  the number itself.
  58.            dec   is the number of decimal places in the
  59.                  return string. The default is 0.
  60.  
  61.      returns     str() returns a character string containing
  62.                  the number passed.
  63. */
  64.  
  65. function str(num,len,dec) {
  66.    // set the default values, depending on how many
  67.    // parameters where passed.
  68.    if (str.arguments.length == 0) {
  69.       return "";
  70.    }
  71.    else if (str.arguments.length == 1) {
  72.       len = 0;
  73.       dec = 0;
  74.    }
  75.    else if (str.arguments.length == 2) {
  76.       dec = 0;
  77.    }
  78.  
  79.    // initialize needed objects
  80.    var return_str = new StringEx();
  81.    var stringEx   = new StringEx();
  82.    var math       = new Math();
  83.  
  84.    // check the number of decimals
  85.    return_str.string = ("" + math.round(num*math.pow(10,dec))/math.pow(10,dec));
  86.    
  87.    // remove leading/trailing spaces
  88.    return_str.string = return_str.leftTrim();
  89.    return_str.string = return_str.rightTrim();
  90.  
  91.    // set the correct number of decimal places
  92.    if (return_str.indexOf(".") == 0) {
  93.       return_str = return_str + ".";
  94.    }
  95.    return_str.string = return_str.string + stringEx.replicate("0",dec);
  96.    return_str.string = return_str.left(return_str.indexOf('.') + (dec==0?0:1) + dec); //return_str.string,
  97.  
  98.    // add leading spaces to get correct length. If
  99.    // actual length is greater then len, return overflow
  100.    if (len == 0) {
  101.       // Do nothing. The string is fine.
  102.    } 
  103.    else if (return_str.length > len) {
  104.       return_str.string = stringEx.replicate("*",len);
  105.    } 
  106.    else {
  107.       return_str.string = stringEx.replicate(" ",len) + return_str.string;
  108.       return_str.string = return_str.right(len); //return_str.string,
  109.    }
  110.  
  111.    // that's it
  112.    return (return_str.string);
  113. }