Welcome to Bucaro TecHelp!

Bucaro TecHelp
Maintain Your Computer and Use it More Effectively
to Design a Web Site and Make Money on the Web

About Bucaro TecHelp About BTH User Agreement User Agreement Privacy Policy Privacy Site Map Site Map Contact Bucaro TecHelp Contact RSS News Feeds News Feeds

Victims of Sandy Hook

Stop the Slaughter of Innocents. Congress is bought and paid for by gun lunatics and gun promotion groups. If you want to live in a safe America, help buy Congress back for America. Send a donation to Mayors Against Illegal Guns, 909 Third Avenue, 15th Floor New York, NY 10022

Extract Part of a String

Use the String object's .slice method to extract part of a string. The syntax for the .slice method is shown below.

string.slice(start,end)

Where start is the character location to begin the extraction, the first character in a string being position 0, and end being the location to end the extraction, everything up to, but not including, the character at the end location being included in the returned substring.

In the example below, the .slice method is used to extract the document name part of a URL string, not including the file extension.

<script>
var urlStr = "domain.ext/directory/filename.ext";
var docStr = urlStr.slice(21,29);
alert(docStr);
</script>

The end parameter is optional in the .slice method. If the end parameter is not included, all the characters to the end of the string are extracted. In the example below, the .slice method is used to extract the document name part of a URL string, including the file extension.

<script>
var urlStr = "domain.ext/directory/filename.ext";
var docStr = urlStr.slice(21);
alert(docStr);
</script>

To extract part of the end of a string, use a negative number equal to the number of characters you want to extract. In the example below, the .slice method is used with a negative number to extract only file extension, not including the dot.

<script>
var urlStr = "domain.ext/directory/filename.ext";
var docStr = urlStr.slice(-3);
alert(docStr);
</script>

One problem with the String object's .slice method is that you have to know beforehand the location of the substring that you want to extract. Lets assume from the previous examples that I didn't know the location of the filename in the URL. In the example below I use the String object's .lastIndexOf method to locate the last slash and last dot characters in the URL string, then I use that information in the .slice method to extract the filename, not including the file extension.

<script>
var urlStr = "domain.ext/directory/filename.ext";
var loc1 = urlStr.lastIndexOf("/");
var loc2 = urlStr.lastIndexOf(".");
var docStr = urlStr.slice(loc1+1,loc2);
alert(docStr);
</script>

RSS Feed RSS Feed



Follow Stephen Bucaro Follow @Stephen Bucaro


Web Design Sections

Fire HD
[Site User Agreement] [Privacy Policy] [Site map] [Search This Site] [Contact Form]
Copyright©2001-2016 Bucaro TecHelp 13771 N Fountain Hills Blvd Suite 114-248 Fountain Hills, AZ 85268