String utility: word count, replace and capitalize : String « Language Basics « JavaScript DHTML

JavaScript DHTML
1. Ajax Layer
2. Data Type
3. Date Time
4. Development
5. Document
6. Event
7. Event onMethod
8. Form Control
9. GUI Components
10. HTML
11. Javascript Collections
12. Javascript Objects
13. Language Basics
14. Node Operation
15. Object Oriented
16. Page Components
17. Security
18. Style Layout
19. Table
20. Utilities
21. Window Browser
Microsoft Office Word 2007 Tutorial
Java
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
C# / C Sharp
C# / CSharp Tutorial
ASP.Net
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
PHP
Python
SQL Server / T-SQL
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
JavaScript DHTML » Language Basics » String 
String utility: word count, replace and capitalize

/*
JavaScript Application Cookbook
By Jerry Bradenbaugh

Publisher: O'Reilly 
Series: Cookbooks
ISBN: 1-56592-577-7
*/ 

<HTML>

<HEAD>

<TITLE>strings.js Example</TITLE>

<SCRIPT LANUAGE="JavaScript1.1">
// strings.js

function wordCount(str) {
  var wordArray = new Array();
  str = prepStr(str);
  var tempArray = str.split(' ').sort();
  var count = 1;

  // Iterate through all the words
  for (var i = 0; i < tempArray.length; i++) {

    // If an array element with the same name as
    // the current word exists, increment its value by 1
    if (wordArray[tempArray[i]]) {
      wordArray[tempArray[i]]++;
      }

    // Otherwise, assign the array element to the 
    /// name of the word, and give it a value of 1
    else wordArray[tempArray[i]] 1}
    }
    var arrStr = '';

    // Create table rows that display the word and the frequency
    for (word in wordArray) {
      if (word != "") {
        arrStr += '<TR><TD>' + word + '</TD><TD>' + wordArray[word'</TD></TR>';
        count++;
        }
      }
    return '<TABLE BORDER=CELLPADDING=10><TR><TD WIDTH=300 VALIGN=TOP ROWSPAN=' + 
      count + '><B>Original Formatted Text</B><BR><I>' + str + 
      '</I><TD><B>Word</B><TD><B>Freqency</B></TR>' + arrStr + '</TABLE>';
  }

// Define a function to format strings for easier manipulation
function prepStr(str) {
  str = str.toLowerCase();
  str = str.replace(/['"-]/g, "");
  str = str.replace(/\W/g, " ");
  str = str.replace(/\s+/g, " ");
  return str;
  }

function camelCaps(str, theCase) {
  var tempArray = str.split(' ');

  // Make the first character of each word upper- or lowercase
  // depending on the value of theCase
  for (var i = 0; i < tempArray.length; i++) {
    if (theCase) {
      tempArray[i= tempArray[i].charAt(0).toUpperCase() + tempArray[i].substring(1);
      }
    else {
      tempArray[i= tempArray[i].charAt(0).toLowerCase() + tempArray[i].substring(1);
      }
    }
  return tempArray.join(' ');
  }

var order = true;

function reorder(str) {
  str = prepStr(str);
  str = str.replace(/\d/g, "");
  order = !order;

  // Use the sort() method for traditional sorts
  // Use the reverse() method in conjunction for reverse sorts()
  if(!order) { str = str.split(' ').sort().join(' ')}
  else str = str.split(' ').sort().reverse().join(' ')}
  return str.replace(/^\s+/, "");
  }
</SCRIPT>
<SCRIPT LANGUAGE="JavaScript1.1">
<!--

function doCount(txt) {
  words = wordCount(txt);  
  self.location.href = "javascript: '<HTML>' + words + '</HTML>'"
  }

//-->
</SCRIPT>
</HEAD>
<BODY BGCOLOR=WHITE>
<FORM>
Enter some words in the TEXTAREA below. Then choose <B>Count</B> for a word count: <BR>
<TEXTAREA ROWS=10 COLS=25 WRAP=PHYSICAL>
</TEXTAREA><BR>
<INPUT TYPE=BUTTON VALUE="Count" onClick="doCount(this.form.elements[0].value);">
<INPUT TYPE=RESET>
</FORM>

<BR><BR>
<FORM>
Enter some words in the TEXTAREA below. Then choose <B>Upper</B> or <B>Lower</B> to change the case: <BR>
<TEXTAREA ROWS=10 COLS=25 WRAP=PHYSICAL>
</TEXTAREA><BR>
<INPUT TYPE=BUTTON VALUE="Upper" 
onClick="this.form.elements[0].value=camelCaps(this.form.elements[0].value, true);">
<INPUT TYPE=BUTTON VALUE="Lower" 
onClick="this.form.elements[0].value=camelCaps(this.form.elements[0].value, false);">
<INPUT TYPE=RESET>
</FORM>

<BR><BR>
<FORM>
Enter some words in the TEXTAREA below. Then choose <B>Sort</B> to sort the text: <BR>
<TEXTAREA ROWS=10 COLS=25 WRAP=PHYSICAL>
</TEXTAREA><BR>
<INPUT TYPE=BUTTON VALUE=" Sort " 
onClick="this.form.elements[0].value=reorder(this.form.elements[0].value);">
<INPUT TYPE=RESET>
</FORM>
</BODY>
</HTML>

           
       
Related examples in the same category
1. Demo all String methods
2. Strip Commas
3. Text Range Search and Replace (IE only)
4. Counting the Words in a Text String
5. Reversing a String
6. Trimming a String Using Regular Expressions
7. String encode and decode
8. Capitalizing the First Letter in Each Word of a String
9. Playing with Strings
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. String match(): returns the text if found
32. String substr() and substring(): returns a specified part of a string
33. String toLowerCase() and toUpperCase(): converts a string to lowercase and uppercase
34. Converting Strings to Upper Case
35. String encoder
w_w_w__.j_av___a___2___s_.__c__o__m__ | Contact Us
Copyright 2003 - 08 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.