- This topic is empty.
Viewing 4 posts - 1 through 4 (of 4 total)
Viewing 4 posts - 1 through 4 (of 4 total)
- The forum ‘JavaScript’ is closed to new topics and replies.
The forums ran from 2008-2020 and are now closed and viewable here as an archive.
Home › Forums › JavaScript › Cut string with jQuery
Hi guys. I have a string with long name –'c\fakepath\my_image_name.png'
and I need to cut it to – 'my_image_name.png'
. Can somebody have a hint how can I do it with jQuery. Thanks for reply.
You can do this:
//Set the string as a variable
var filePath = 'c\fakepath\my_image_name.png'
//Split the string into an array by splitting it at the '\' character
var arr = filePath.split('\');
//Get the value for the last item in the array
var final = arr[arr.length-1];
@noahgelman: You need to escape the back-slash I think? filePath.split('\\')
Anyway @Kuzyo, here’s a third solution to add to your arsenal of string-splitting:
// Creates a substring containing whatever follows the last occurrence of '\'
var fileName = filePath.substring(filePath.lastIndexOf('\\')+1);
The above assumes that ‘filePath’ contains the string you want to filter.
@Chromawoods off the top of my head I don’t know. Probably I guess. I don’t normally split \ characters