Mark Wilson I am the creator of TopXML. I am available for international and local (Australia) contracts. I am a Solution Architect/Business Analyst. I have worked in IT in several countries (NZ, Australia, South Africa, UK) building and training teams for government and very large non-governmental organizations. I am ex-Microsoft Consulting Services. I wrote the first book on Microsoft XML published in 2000 called XML Programming with VB and ASP. Most recently I have been building tools for the SEO industry. Ask me for a 37 point SEO health-checkup for your website.
First posted :
03/24/2008
Times viewed :
5065
JavaScript String Object Functions
Introduction
The String object contains 28 functions, which can be used
to manipulate the string. In this section you will see these 28 functions in
some small examples. Please take a look at the string object, if you need more
information about that. All functions are used with the String object. The
following examples demonstrate this.
The concat function can be used to concat a string to
another one.
<html>
<head>
<script type="text/javascript">
var str =
new String("Hello")
str =
str.concat(" World")
document.write(str)
</script>
</head>
<body></body>
</html>
Output
Hello World
JavaScript fromCharCode Function
This function can be used to return a string from a number
of Unicode character values. This function cannot be called with a string
object, instead use String.formCharCode.
<html>
<head>
<script type="text/javascript">
str =
String.fromCharCode(65, 66, 67)
document.write(str)
</script>
</head>
<body></body>
</html>
Output
ABC
JavaScript split Function
The split function can be used to split a string into an
array of sub strings.
<html>
<head>
<script type="text/javascript">
str = new
String("0,1,2,3")
str =
str.split(",")
document.write(str[0]+"<br/>")
document.write(str[1]+"<br/>")
document.write(str[2]+"<br/>")
document.write(str[3]+"<br/>")
</script>
</head>
<body></body>
</html>
Output
0
1
2
3
JavaScript slice Function
The slice function can be used to slice a specific section
and return a new string.
<html>
<head>
<script type="text/javascript">
str = new
String("Hello World")
str =
str.slice(5,11)
document.write(str)
</script>
</head>
<body></body>
</html>
Output
World
JavaScript substring Function
The function substring can be used to get a specific section
from a string. It basically works the same as the slice function.
<html>
<head>
<script type="text/javascript">
str = new
String("Hello World")
str =
str.substring(5,11)
document.write(str)
</script>
</head>
<body></body>
</html>
Output
World
JavaScript substr Function
The function substr can be used to get a specific section
from a string. It works the same as slice and substring.
<html>
<head>
<script type="text/javascript">
str = new
String("Hello World")
str =
str.substr(5,11)
document.write(str)
</script>
</head>
<body></body>
</html>
Output
World
JavaScript match Function
The match function can be used to match a regular expression
against a string. It returns an array of the matches.
<html>
<head>
<script type="text/javascript">
str = new
String("Hello World")
str =
str.match("H")
for(i=0;i<str.length;i++)
{
document.write(str[i]+"<br/>")
}
</script>
</head>
<body></body>
</html>
Output
H
JavaScript replace Function
The function replace can be used to replace a string with
another string. A regular expression can be used to find the string.
<html>
<head>
<script type="text/javascript">
var str =
new String("Hello World")
document.write(str+"<br/>")
var regEx =
new RegExp ('World', 'gi') ;
str =
str.replace(regEx, 'User')
document.write(str)
</script>
</head>
<body></body>
</html>
Output
Hello World
Hello User
JavaScript search Function
This function can be used to in conjunction with a
regular expression to search for a specific format.
<html>
<head>
<script type="text/javascript">
strWrongEmail = new String("sonu_sonu.com")
emailRegEx = /^[^@]+@[^@]+.[a-z]{2,}$/i
if(strWrongEmail.search(emailRegEx) == -1){
document.write("Email is not valid")
}
else
document.write("Email is valid")
</script>
</head>
<body></body>
</html>
Output
Email is not valid
JavaScript toLowerCase Function
This function can be used to convert the string to lower
case.
<html>
<head>
<script type="text/javascript">
str = new
String("HELLO WORLD")
str =
str.toLowerCase()
document.write(str)
</script>
</head>
<body></body>
</html>
Output
hello world
JavaScript toUpperCase Function
This function is used to convert the string to upper case.
<html>
<head>
<script type="text/javascript">
str = new
String("hello world")
str =
str.toUpperCase()
document.write(str)
</script>
</head>
<body></body>
</html>
Output
HELLO WORLD
JavaScript fontcolor Function
The fontcolor function can be used to wrap the string in the
<font> tag, which changes the color of the string.
<html>
<head>
<script type="text/javascript">
str = new
String("hello world")
str =
str.fontcolor("red")
document.write(str)
</script>
</head>
<body></body>
</html>
Output
hello world
JavaScript fontsize Function
The fontsize function can be used to change the font size of
the string.