Showing posts with label 15. JavaScript String Object. Show all posts
Showing posts with label 15. JavaScript String Object. Show all posts

15. JavaScript String Object

Javascript String Object

String object
Examples

The string object is used to manipulate a stored piece of text

String Object

length property of the string object to find the length of a string

var txt="Hello World!";
document.write(txt.length);

output: 12

toUpperCase() method of the string object to convert a string to upper letters

var text="Hello World!";
document.write(txt.toUpperCase());

output:HELLO WORLD!


<html>
<body>
<script type="text/javascript">
var txt="Hello World!";
document.write(txt.length);
</script>
</body>
</html>

ouput:12

<html>
<body>
<script type="text/javascript">
var txt="Hello World!";
document.write("<p>Big:"+txt.big()+"</p>);
document.write("<p>Small:"+txt.small()+"</p>);

document.write("<p>Bold:"+txt.bold()+"</p>);
document.write("<p>Italic:"+txt.italics()+"</p>);

document.write("<p>Fixed:"+txt.fixed()+"</p>);
document.write("<p>Strike:"+txt.strike()+"</p>);

document.write("<p>Font Color:"+txt.fontcolor()+"</p>);
document.write("<p>Font Size:"+txt.fontsize()+"</p>);

document.write("<p>Sub Script:"+txt.sub()+"</p>);
document.write("<p>Super Script:"+txt.sup()+"</p>);

document.write("<p>Link:"+txt.link("http://www.w3schools.com")+"</p>);
document.write("<p>Blink:"+txt.blink()+"(does not work in IE, chrom, or safari)</p>);
</script>
</body>
</html>

<html>
<body>
<script type="text/javascript">
var txt="Hello World!";
document.write(txt.toLowerCase()+"<br/>");
document.write(txt.toUpperCase());
</script>
</body>
</html>

how to search for a specified value within a string.

<html>
<body>
<script type="text/javascript">
var str="Hello World!";
document.write(str.match("World")+"<br/>");
document.write(str.match("world")+"<br/>");
document.write(str.match("worlld")+"<br/>");
document.write(str.match("world!")+"<br/>");
</script>
</body>
</html>

how to replace a specified value with another value in a string.

<html>
<body>
<script type="text/javascript">
var str="Visit Microsoft!";
document.write(str.replace("Microsoft","w3schools"));
</script>
</body>
</html>

how to find the position of the first occurence of a specified value in a string.

<html>
<body>
<script type="text/javascript">
document.write(str.indexof("Hello")+"<br/>");
document.write(str.indexof("World")+"<br/>");
document.write(str.indexof("world")+"<br/>");
</script>
</body>
</html>

My Favorite Books

  • C and Data Structures by Ashok N. kamthane
  • Web Technologies by A. A. Puntambekar
  • Learn HTML and CSS with W3Schools
  • Learn JavaScript and Ajax with W3Schools
  • HTML5 Black Book: Covers Css3, Javascript,XML, XHTML, Ajax, PHP And Jquery
  • HTML5 Application Development Fundamentals: MTA Exam 98-375
  • .NET 4.0 Programming 6-In-1, Black Book
  • SQL Server 2008 R2 Black Book
  • Asp.net 4.0 Projects Covers: net 3.5 And .net 4.0 Codes, Black Book
  • UNIX Shell Programming 1 Edition by Yashavant Kanetkar
  • UNIX and Shell Programming 1 Edition by Richard F. Gilberg, Behrouz A. Forouzan
  • Computer Networks by Andrew S. Tanenbaum
  • Multiple Choice questions in computer science by Timothy J Williams