简体   繁体   中英

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);

try this

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

or (regex)

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

在此处输入图片说明

In case you wish to use substring:

var str="C:\\FakePath\\3.jpg";
var imgName = str.substring(12);
logopath = encodeURIComponent( logopath ).replace( /.+FakePath%0/, '' )

'\\ 3'被解释为八进制转义序列,它指向不可打印的ASCII字符。

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.

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM