Javascript Functions Back Slash!

A

adam_a_knight

Can someone tell me how do i find the last occurrence of the "\"
character in a file name.

retVal = "\data\sheet\file.gif"
lastPos = retVal.lastIndexOf("\",0);
This code keeps erroring !!!

Thanks..
 
V

Vincent van Beveren

Can someone tell me how do i find the last occurrence of the "\"
character in a file name.

retVal = "\data\sheet\file.gif"
lastPos = retVal.lastIndexOf("\",0);
This code keeps erroring !!!

the lastPos is correct. The problem is in the first statement.

The \ is the symbol for escaping. Supose you want to put
"starbuck's" in a string using single quotes to surround it.
This is impossible to do:

s = 'starbuck's'

Will error, and you can see why. So you have the escape the character.

s = 'starbuck\'s'

Escaping only works to create certain special characters for exampple:

\" = "
\' = '
\n = enter
\t = tab
\\ = \

You should be able to figure out the rest.

Good luck,
Vincent
 
R

Robert

Can someone tell me how do i find the last occurrence of the "\"
character in a file name.

retVal = "\data\sheet\file.gif"
lastPos = retVal.lastIndexOf("\",0);
This code keeps erroring !!!

Thanks..

While I do not know where you got the file specification from, you are
using the IE proprietary format for the file name.

The internet format would be:

retVal = "/data/sheet/file.gif"

This format is supported by all web browsers.

Robert
 
T

Thomas 'PointedEars' Lahn

Can someone tell me how do i find the last occurrence of the "\"
character in a file name.

retVal = "\data\sheet\file.gif"
lastPos = retVal.lastIndexOf("\",0);
This code keeps erroring !!!

Your Exclamation Mark key is borken. [psf 2.3]

Firstly, "Does not work" is a useless error description. [psf 4.11]
Read the FAQ.

Secondly, "\" is the escape character in string literals, use

var retVal = "\\data\\sheet\\file.gif";
var lastPos = retVal.lastIndexOf("\\");

instead. However, you should thirdly pay attention to Robert's comment.


PointedEars
 
E

Evertjan.

Thomas 'PointedEars' Lahn wrote on 26 jun 2004 in comp.lang.javascript:
Your Exclamation Mark key is borken. [psf 2.3]

Town in Germany, should be with a capital B?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top