Fahad Abid Janjua
Fahad Abid Janjua

Reputation: 1024

Truncating a String in Javascript

I know this is absurd but I am stuck at it

I have a filepath using fileupload in asp classic

The filepath is C:\FakePath\3.jpg

I want to retrieve it in a variable so that it would only give me 3.jpg

substring() and substr() doesn't include 3 I don't know why

logopath = C:\FakePath\3.jpg;
logopath = logopath.substring(10);

Upvotes: 0

Views: 224

Answers (5)

naota
naota

Reputation: 4718

If you'd like to solve it in classic ASP, plesae try this.

<%
dim aryPath
aryPath = Split("C:\FakePath\3.jpg","\")
Response.Write aryPath(2)
%>

Hope it could be helpful.

Upvotes: 1

Shadow Wizard
Shadow Wizard

Reputation: 66388

Use such code:

function FileChanged(input) {
    var fullPath = input.value;
    var index = fullPath.lastIndexOf("\\");
    var fileName = (index < 0) ? fullPath : fullPath.substr(index + 1);
    alert(fileName);
}​

The two middle lines are what you need: they will take the value after the last slash. This way it doesn't matter what is the path, it will always return only the file name.

Live test case.

Upvotes: 1

MikeM
MikeM

Reputation: 13631

logopath = encodeURIComponent( logopath ).replace( /.+FakePath%0/, '' )

'\3' is being interpreted as an octal escape sequence which points to a non-printable ASCII character.

Upvotes: 1

Royi Namir
Royi Namir

Reputation: 148524

try this

'C:\\FakePath\\3.jpg'.split('\\').pop(); // "3.jpg"

or (regex)

'C:\\FakePath\\3.jpg'.replace(/^.*\\/, '');   // "3.jpg"

enter image description here

Upvotes: 2

TJ-
TJ-

Reputation: 14363

In case you wish to use substring:

var str="C:\\FakePath\\3.jpg";
var imgName = str.substring(12);

Upvotes: 1

Related Questions