Playing with Strings : String : Language Basics : JavaScript DHTML examples (example source code) Organized by topic

JavaScript DHTML
C++
PHP


JavaScript DHTML  »  Language Basics   » [  String  ]   
 



Playing with Strings

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

/*

Learn How to Program Using Any Web Browser
by Harold Davis 

Apress CopyRight 2004

ISBN: 1590591135
*/
<HTML>
<HEAD>
<TITLE>String Play</TITLE>
<SCRIPT
function capWords(str){ 
   // break into an array of words, 
   // using space as the delimiter 
   var words = str.split(" ")
   for (var i=; i < words.length ; i++){ 
      // inner loop -- do the capitalizing 
      var testwd = words[i]
      var firLet = testwd.substr(0,1)//lop off first letter 
      var rest = testwd.substr(1, testwd.length -1
      words[i= firLet.toUpperCase() + rest 
   
   document.theForm.results.value = words.join(" ")

function countWords(str){ 
   var count = 0
   // break into an array of words, 
   // using space as the delimiter 
   // words.length won't work because of spaces 
  var words = str.split(" ")
    for (i=; i < words.length ; i++){ 
       // inner loop -- do the count 
       if (words[i!= ""
          count += 1
    

   document.theForm.results.value = 
      "There are " 
      count + 
      " words in the text string you entered!"

function revWords(str){ 
   // break into an array of words, 
   // using space as the delimiter 
   var words = str.split(" ")
   var j = words.length - 1
   var backWords = new Array()
   for (i=; i < words.length ; i++){ 
      backWords[j= words[i]
      j--;
   
   document.theForm.results.value = backWords.join(" ")

function revString(str) { 
   var retStr = ""
   for (i=str.length - ; i > - ; i--){ 
      retStr += str.substr(i,1)
   
   return retStr; 

</SCRIPT>
</HEAD>
<BODY>
<H1> 
Playing with strings! 
</H1>
<FORM name="theForm">
<TABLE>
<tr>
<td colspan=5
Enter a text string: 
</td>
</tr>
<tr>
<td colspan=5>
<TEXTAREA name=inStr rows=cols=90>
</TEXTAREA>
</td>
</tr>
<tr>
<td>
<INPUT type=button value="Capitalize Words" 
   onClick="capWords(document.theForm.inStr.value)";>
</td>
<td>
<INPUT type=button value="Count Words" 
onClick="countWords(document.theForm.inStr.value)";>
</td>
<td>
<INPUT type=button value="Reverse Words" 
   onClick="revWords(document.theForm.inStr.value)";>
</td>
<td>
<INPUT type=button value="Reverse String" 
   onClick="document.theForm.results.value = 
revString(document.theForm.inStr.value)";>
</td>
<td>
<INPUT type=button value="Clear" 
onClick='document.theForm.inStr.value=""';>
</td>
</tr>
<tr>
<td colspan=5>
<br>
<hr> 
Results<br>
</td>
</tr>
<tr>
<td colspan=5>
<TEXTAREA name=results rows=cols=90>
</TEXTAREA>
</td>
</tr>
<tr>
<td colspan=5>
<INPUT type=button name="theButton" value="Clear Results" 
   onClick='document.theForm.results.value=""';>
</td>
</tr>
</TABLE>
</FORM>
</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.   Using the String Object's Link Method
11.  Using a for Loop to Reverse a String
12.   Concatenate JavaScript String
13.  String length: number of characters in a string.
14.  String fontcolor(): a string in a specified color
15.  String indexOf(): string position
16.  String Validation
17.  Using Quotes within Strings
18.  Using the String Object
19.  String toUpperCase
20.  Lab for string.replace() and string.search()
21.  Slicing a String
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.