Home arrow JavaScript arrow Page 5 - JavaScript Objects: More on String Methods
JAVASCRIPT

JavaScript Objects: More on String Methods


Welcome back to my series covering JavaScript Objects. In the last issue we discussed the Object String properties and some of the Object String methods. In this episode we will continue our discussion of the Object String methods and how to use them.

Author Info:
By: James Payne
Rating: 5 stars5 stars5 stars5 stars5 stars / 7
January 22, 2008
TABLE OF CONTENTS:
  1. · JavaScript Objects: More on String Methods
  2. · The CharAt() Method
  3. · Working with the ConCat() Method
  4. · The FontColor() Method
  5. · FromCharCode() Method
  6. · The LastIndexOf() Method

print this article
SEARCH DEVARTICLES

JavaScript Objects: More on String Methods - FromCharCode() Method
(Page 5 of 6 )

You use the FromCharCode() method to translate Unicode to text. Here is the alphabet written in Unicode. Yep go ahead. Nerd it up.


<html>

<body>


<script type="text/javascript">

document.write("Here is the alphabet in Unicode <br /> <br />")

document.write(String.fromCharCode(65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90))


</script>


</body>

</html>

Here is the result:

  Here is the alphabet in Unicode

  ABCDEFGHIJKLMNOPQRSTUVWXYZ

Full Unicode charts are freely available on the web and are quite extensive.

The IndexOf() Method

The IndexOf() method is useful if you wish to know where a string within a string begins (its position in the string rather). Behold my next example, puny mortal:


<html>

<body>


<script type="text/javascript">


var myfeelings="I love Gorillas!"

document.write(myfeelings.indexOf("I") + "<br />")

document.write(myfeelings.indexOf("love") + "<br />")

document.write(myfeelings.indexOf("to eat") + "<br />")

document.write(myfeelings.indexOf("Gorillas"))


</script>


</body>

</html>

This creates a variable named myfeelings and stores the text "I love Gorillas" in it. It then checks for the position of the word "I," which it finds, and prints out its position (positions begin with 0). Then it looks for the word "love" and finds it begins at the 2 position (the l that starts the word is the second character in the string). Next it looks for the text "to eat" in the variable, and does not find it. Because it is not in the string, and therefore its position cannot be found, it results in the number -1. Any time info is not found, it will return -1.

Lastly, it searches for the word "Gorillas" (my all time favorite word) and finds it located in position 7. Here are the results of the program:

  0
  2
  -1
  7

Note that the IndexOf() method is case-sensitive. If I had told it to find "gorillas" in the string, it would not have worked and returned a -1 for its position.


blog comments powered by Disqus
JAVASCRIPT ARTICLES

- Project Nashorn to Make Java, JavaScript Wor...
- JavaScript Virus Attacks Tumblr Blogs
- Google Releases Stable Dart Version, JavaScr...
- Khan Academy Unveils New JavaScript Learning...
- Accessing Nitro? There`s an App for That
- JQuery 2.0 Leaving Older IE Versions Behind
- Fastest JavaScript Engine Might Surprise You
- Microsoft Adjusting Chakra for IE 10
- Brendan Eich: We Don`t Need Google Native Cl...
- An Overview of JavaScript Statements
- An Overview of JavaScript Operators
- Overview of JavaScript Variables
- More of the Top jQuery Social Plugins
- The Top jQuery Social Plugins
- More of the Top jQuery Slider Plugins

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 

Developer Shed Affiliates

 




© 2003-2017 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials