Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Equivalent of VB's Right$() function? 1

Status
Not open for further replies.

techkate

Programmer
Feb 23, 2004
118
US
I'm new to Javascript, please bear with me. I've done some searching for this, haven't quite found what I was looking for. Looked in the FAQ's here too.

I have a filename, let's say, of 'MyFile.xls'.

I want to strip off the filename and preserve it, so I would have two variables:

string1 = 'MyFile'
string2 = '.xls'


I will append a timestamp to string1 (I've looked this up on the web and it seems pretty simple so I think I can manage it):

string1 = string1 + timestamp


Then I want to append string2 back onto string1:

string1 = string1 + string2


Could anyone help me with this or point me in the right direction please? I've found a few websites that touch JavaScript parsing, but mostly only splitting. Not more complex situations like finding the first decimal from the right. I wish JScript had VB's parsing functions.

Thanks for your time.
Kate
 
You'll want to use the lastIndexOf method combined with the substr method (or substring if that suits you better, I prefer substr):
Code:
<script type="text/javascript">

function blah(str) {
   var pos = str.lastIndexOf(".");
   if (pos != -1) {
      var string1 = str.substr(0, pos);
      var string2 = str.substr(pos, str.length - pos);
      var timestamp = "03/21/2006";
      string1 = string1 + timestamp;
      alert(string1 + string2);
   }
   else {
      alert("decimal could not be found");
   }
}

</script>
<input type="text" id="txt" />
<input type="button" value="Click me" onclick="blah(document.getElementById('txt').value)" />

-kaht

[small] <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <.</B>[/small]
[banghead] [small](He's back)[/small]
 

Thanks, kaht, that worked like a charm!


Yeah, it's a non-nutritive cereal varnish. It's semi-permeable. It's not osmotic.
What it does is it coats and seals the flake, prevents the milk from penetrating it."

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top