+ Post New Thread
Results 1 to 7 of 7
Coding Thread, JAVASCRIPT TO TRIM STRING in Coding and Web Development; Hi All, Ho can I trim a string so that all characters after a specific value are dropped. E.g I ...
  1. #1

    Join Date
    Jul 2010
    Posts
    25
    Thank Post
    1
    Thanked 0 Times in 0 Posts
    Rep Power
    0

    JAVASCRIPT TO TRIM STRING

    Hi All,

    Ho can I trim a string so that all characters after a specific value are dropped. E.g I want to DROP the "~1.xls" from the following string. 0000715 RECORDATI PHARMACEUTICALS LTD 07 2010~1.xls. So I want the code to lookup "2010" in the string and delete all charaters after that.

    Thanks

    T

  2. #2

    webman's Avatar
    Join Date
    Nov 2005
    Location
    North East England
    Posts
    8,413
    Thank Post
    642
    Thanked 964 Times in 664 Posts
    Blog Entries
    2
    Rep Power
    327
    This should work:

    Code:
    var file = "00715...~1.xls";
    var betterfile = file.substring(0, file.lastIndexOf('~'));
    Read here for more information: JavaScript - Strings

  3. Thanks to webman from:

    tom_newton (22nd September 2010)

  4. #3

    Join Date
    Jul 2010
    Posts
    25
    Thank Post
    1
    Thanked 0 Times in 0 Posts
    Rep Power
    0
    cheers mate I'll give it a go now and let you know how I get on.

  5. #4

    sonofsanta's Avatar
    Join Date
    Dec 2009
    Location
    Lincolnshire, UK
    Posts
    5,125
    Thank Post
    916
    Thanked 1,512 Times in 1,033 Posts
    Blog Entries
    47
    Rep Power
    653
    I forget the exact JavaScript phrasing, but just do a string find to locate the starting position of the phrase "2010", and then use a trim operation to chop off everything from that point (plus or minus however many characters you need to shift by). It takes a bit of trial and error with the +-, but it's pretty straightforward logic.

    Mercifully it's a few years since I had to type out some JS though, so can't give you the code directly.

  6. #5

    Join Date
    Jul 2010
    Posts
    25
    Thank Post
    1
    Thanked 0 Times in 0 Posts
    Rep Power
    0
    Thanks All.. am not sure what the syntax should be. Please have a look at my code below. Its the variable "clpApprovedData" that I need to apply this trim on.


    function IncorrectAttachment()
    {
    var actionName = eworkGetField("txtActionName");
    var strorginal = eworkGetField("clpGeneratedData");
    var strreturned = eworkGetField("clpApprovedData");

    if(strorginal.toUpperCase()!= strreturned.toUpperCase())
    {
    alert("strorginal=" + strorginal + "strreturned=" + strreturned);
    return false;
    }
    return true;
    }

  7. #6


    tom_newton's Avatar
    Join Date
    Sep 2006
    Location
    Leeds
    Posts
    4,485
    Thank Post
    867
    Thanked 854 Times in 675 Posts
    Rep Power
    197
    I presume you mean strreturned? As that's a variable containing the string I presume eworkGetField returns.

    Try:
    Code:
    var strreturned = eworkGetField("clpApprovedData");
    
    strreturned =  strreturned.substring(0,  strreturned.lastIndexOf('~'));
    
    if(strorginal.toUpperCase()!= strreturned.toUpperCase())
    Which is what webman suggested, inserted into your code.

  8. #7

    Join Date
    Jul 2010
    Posts
    25
    Thank Post
    1
    Thanked 0 Times in 0 Posts
    Rep Power
    0
    thanks a million guys... it worked first time out again many many thanks and much appreciated

SHARE:
+ Post New Thread

Similar Threads

  1. extract first letter from a string -vbs
    By CyberNerd in forum Scripts
    Replies: 3
    Last Post: 10th December 2009, 04:29 PM
  2. How to work out domain string (LDAP I think)
    By garethedmondson in forum Windows
    Replies: 16
    Last Post: 9th September 2009, 11:15 PM
  3. Trim URL with PHP
    By Hightower in forum Web Development
    Replies: 19
    Last Post: 11th February 2009, 09:53 AM
  4. Mysql - pre-pend to string
    By danIT in forum Coding
    Replies: 3
    Last Post: 9th January 2009, 06:17 PM

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •