Teaser Posted September 22, 2010 Posted September 22, 2010 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
webman Posted September 22, 2010 Posted September 22, 2010 This should work: var file = "00715...~1.xls"; var betterfile = file.substring(0, file.lastIndexOf('~')); Read here for more information: JavaScript - Strings 1
Teaser Posted September 22, 2010 Author Posted September 22, 2010 cheers mate I'll give it a go now and let you know how I get on.
sonofsanta Posted September 22, 2010 Posted September 22, 2010 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.
Teaser Posted September 22, 2010 Author Posted September 22, 2010 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; }
tom_newton Posted September 22, 2010 Posted September 22, 2010 I presume you mean strreturned? As that's a variable containing the string I presume eworkGetField returns. Try: var strreturned = eworkGetField("clpApprovedData"); strreturned = strreturned.substring(0, strreturned.lastIndexOf('~')); if(strorginal.toUpperCase()!= strreturned.toUpperCase()) Which is what webman suggested, inserted into your code.
Teaser Posted September 22, 2010 Author Posted September 22, 2010 thanks a million guys... it worked first time out again many many thanks and much appreciated
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now