Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Need function to take string and remove anything after last "/"

Posted on 2005-05-12
5
Medium Priority
?
535 Views
Last Modified: 2011-10-03
I need function to take string and remove anything after last "/" (backslash). There may or may not be something after it??
0
Comment
Question by:894359
  • 3
  • 2
5 Comments
 
LVL 32

Expert Comment

by:Batalf
ID: 13990482
I assume you mean a normal slash "/" and not backslash !?

This is one example of how that could be done:

<script>
var string = 'This is just a simple test/ with some slashes/ after each other / this text should be stripped away';
var stringArray = string.split('/');    // Create array
stringArray.pop();  // remove last item from array
var tmpString = stringArray.join('/');  // Convert array into string
string = tmpString;
document.write(string);  // Output the string withoug the part after the last slash.

</script>

kind regards
Batalf
0
 
LVL 32

Expert Comment

by:ldbkutty
ID: 13990488
<script>
testStr = 'This is my / test / string';
newStr = testStr.substr(0, testStr.lastIndexOf('/'));
alert(newStr);
</script>
0
 
LVL 32

Accepted Solution

by:
ldbkutty earned 2000 total points
ID: 13990528
Slightly better (works even if the testStr does not have the slash):

<script>
testStr = 'This is my / test';
if(testStr.indexOf('/') != -1)  testStr = testStr.substr(0, testStr.lastIndexOf('/'));
alert(testStr);
</script>
0
 
LVL 32

Expert Comment

by:Batalf
ID: 13990533
ldbkutty,

Yes of course. I didn't think of the lastIndexOf() function:-) Well done!

Batalf
0
 
LVL 32

Expert Comment

by:ldbkutty
ID: 13990559
Thanks Batalf :=)
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses how to create an extensible mechanism for linked drop downs.
In this blog, we’ll look at how improvements to Percona XtraDB Cluster improved IST performance.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

580 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question