Slicing a String : String : Language Basics : JavaScript DHTML examples (example source code) Organized by topic

JavaScript DHTML
C++
JavaScript DHTML Home »  Language Basics   » [  String  ]   
 



Slicing a String

Please note that some example is only working under IE or Firefox.

/*
JavaScript Bible, Fourth Edition
by Danny Goodman 

Publisher: John Wiley & Sons CopyRight 2001
ISBN: 0764533428
*/

<HTML>
<HEAD>
<TITLE>String Slicing and Dicing, Part I</TITLE>
<SCRIPT LANGUAGE="JavaScript">
var mainString = "Electroencephalograph"
function showResults() {
    var form = document.forms[0]
    var param1 = parseInt(form.param1.options[form.param1.selectedIndex].value)
    var param2 = parseInt(form.param2.options[form.param2.selectedIndex].value)
    if (!param2) {
        form.result1.value = mainString.slice(param1)
    else {
        form.result1.value = mainString.slice(param1, param2)
    }
}
</SCRIPT>
</HEAD>
<BODY onLoad="showResults()">
<B>String slice() Method</B>
<HR>
Text used for the methods:<BR>
<FONT SIZE=+1><TT><B>Electroencephalograph<BR>
----5----5----5----5-</B></TT></FONT>
<TABLE BORDER=1>
<FORM>
<TR><TH>String Method</TH><TH>Method Parameters</TH><TH>Results</TH></TR>
<TR>
<TD>string.slice()</TD><TD ROWSPAN=VALIGN=middle>
(&nbsp;<SELECT NAME="param1" onChange="showResults()">
    <OPTION VALUE=0>0
    <OPTION VALUE=1>1
    <OPTION VALUE=2>2
    <OPTION VALUE=3>3
    <OPTION VALUE=5>5
</SELECT>,
<SELECT NAME="param2" onChange="showResults()">
    <OPTION >(None)
    <OPTION VALUE=5>5
    <OPTION VALUE=10>10
    <OPTION VALUE=-1>-1
    <OPTION VALUE=-5>-5
    <OPTION VALUE=-10>-10
</SELECT>&nbsp;</TD>
<TD><INPUT TYPE="text" NAME="result1" SIZE=25></TD>
</TR>
</FORM>
</TABLE>
</BODY>
</HTML>



           
       
Related examples in the same category
1.  Demo all String methods
2.  String utility: word count, replace and capitalize
3.  Strip Commas
4.  Text Range Search and Replace (IE only)
5.  Counting the Words in a Text String
6.  Reversing a String
7.  Trimming a String Using Regular Expressions
8.  String encode and decode
9.  Capitalizing the First Letter in Each Word of a String
10.   Playing with Strings
11.   Using the String Object's Link Method
12.  Using a for Loop to Reverse a String
13.   Concatenate JavaScript String
14.  String length: number of characters in a string.
15.  String fontcolor(): a string in a specified color
16.  String indexOf(): string position
17.  String Validation
18.  Using Quotes within Strings
19.  Using the String Object
20.  String toUpperCase
21.  Lab for string.replace() and string.search()
22.  A String Object Prototype
23.  Creating a Custom toString() Method
24.  Reading a Portion of a String
25.  Source Code for a Sample Page That Formats a String Object with the 'a' Tag
26.  Source Code for Our String-Formatting Script
27.  Adding a replace() Method to the String Object
28.   Creating a Function That Will Search and Replace in Strings
29.   Using the indexOf() Method to Find All Occurrences of the Letter e in a Sentence
30.  Methods and Properties of the String Object
31.  Using the indexOf() Method to Find All Occurrences of the Letter e in a Sentence
32.  String match(): returns the text if found
33.  String substr() and substring(): returns a specified part of a string
34.  String toLowerCase() and toUpperCase(): converts a string to lowercase and uppercase
35.  Converting Strings to Upper Case
36.  String encoder








Home| Contact Us
Copyright 2003 - 04 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.