NEEH HELP...simple string manipulation

G

Goran Petrovic

I wrote a vb function, but i need it i jaavscript..i know java sintax but am
not sure how to make it work. this extracts teh file name from a path...i
thought strcpy..or something..but dont really know. Please help

function GetWriterName

str=document.location

for i = len(str) to 1 step -1
if mid(str,i,1)="/" then
str= mid( mid(str,i+1,100),1,len( mid(str,i+1,100))-4)
str="../../../aspx/quotes"+str+".aspx"
GetWriterName="../../../aspx/quotes"+str
exit function
end if
next

end function


Goran
 
P

Philip Ronan

I wrote a vb function, but i need it i jaavscript..i know java sintax but am
not sure how to make it work. this extracts teh file name from a path...i
thought strcpy..or something..but dont really know. Please help

To extract the filename from the current location, you could use something
like this:

thisPage = location.pathname.substr(location.pathname.lastIndexOf('/')+1);

Is that what you were after?

Phil
 
L

Lasse Reichstein Nielsen

Goran Petrovic said:
I wrote a vb function, but i need it i jaavscript..i know java sintax but am

It will help you slightly, but Javascript is not Java. Their syntaxes
are similar, but not equal.
not sure how to make it work. this extracts teh file name from a path...i
thought strcpy..or something..but dont really know. Please help
if mid(str,i,1)="/" then
str= mid( mid(str,i+1,100),1,len( mid(str,i+1,100))-4)
str="../../../aspx/quotes"+str+".aspx"
GetWriterName="../../../aspx/quotes"+str

Do you really mean this? The result will be

"../../../aspx/quotes../../../aspx/quotes"+filename+".aspx;

I don't even think there is a "/" in front of the filename.
I guess one of them are superflous.

A solution:
---
function getWriterName() {
var str = location.pathname; // easier than working with full href.
for (var i = str.length ; i > 0 ; i--) {
if (str.charAt(i)=="/") {
str = str.substring(i+1); // no second argument means until end of string
return "../../../aspx/quotes/"+str+".aspx";
}
}
}
---
Now for the easier way:
---
function getWriterName() {
var str = location.pathname;
var i = str.lastIndexOf("/");
return "../../../aspx/quotes"+str.substring(i)+".aspx";
}
 

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
474,077
Messages
2,570,566
Members
47,202
Latest member
misc.

Latest Threads

Top