By: aathishankaran in JavaScript Tutorials on 2007年03月29日 [フレーム]
Strings are a fundamental part of any programming language. Strings, which are a set of alpha-numeric characters, can be either a literal string, such as "Push the envelope" or a variable representing a string, such as the Phrase. A string can also be treated as an object, complete with its own suite of methods and properties.
Use the following procedure to create a string object.
var str_obj =new String([message])
Example:
<SCRIPT>
var str = new String("My name is Richard" )
alert (str. length)
</SCRIPT>
Because strings are one of the primary types of data you have to work with, it is critical to have some way of extracting data from strings and obtaining information about strings. JavaScript has the properties and methods
Method/Property
Description
length
Returns an integer representing the value of size of the string.
charAt (pos)
Returns the character at the specified index.
indexOf (searchText [,startPos])
Returns the index of the first occurrence of SearchText.
lastIndexOf(searchText [,endPos])
Returns the index of last occurrence of searchText
substring (startPos, endPos)
Returns the substring of the string starting at startpos and ending at endPos
You can use the String object's length property to determine the size of a string.
For example, the following code returns 17:
"Crazy Legs Nelson".length
The following code returns 16:
var str = "This is the day."
len = str.length
You can search for text within strings by using indexOf() and lastIndexOf(). Use these methods when you want to search for a particular character or substring within a string and return the position (or index) of the occurrence within the string. Whereas indexOf() starts at the left of the string and moves right, lastIndexOfO does the same operation but starts at the left. Both indexOf() and lastIndexOfO start at the 0 position for the first character encountered, and both return a value of -1 if the search text is not found. For example, the following code re-turns a value of 3:
"Time and time again".indexOf("e")
On the other hand, the following code returns a value of 12:
"Time and time again".lastIndexOf("e")
Both methods have an optional second parameter that enables you to specify here in the string you want to start the search. For example, the script shown n Listing 14.1 searches through the variable graf and counts the number of occurrences of the letter 'e'.
<SCRIPT>
pos = 0
num = -1
i = -1
var graf = "While nearly everyone agrees on the principle of reuse,
the priority we give it varies wildly."
while (pos != -1)
{
pos = graf.indexOf("e",i+1)
num += 1
i = pos
}
document.write(graf)
document.write("<p><p>")
document.write("There were" + num + " e' s in that paragraph.")
</SCRIPT> This policy contains information about your privacy. By posting, you are declaring that you understand this policy:
This policy is subject to change at any time and without notice.
These terms and conditions contain rules about posting comments. By submitting a comment, you are declaring that you agree with these rules:
Failure to comply with these rules may result in being banned from submitting further comments.
These terms and conditions are subject to change at any time and without notice.
Most Viewed Articles (in JavaScript )
Dynamically modify the option set in Dynamics 365 forms
promise and .then() in JavaScript
reduce() and filter() in JavaScript
History and evolution of Javascript
Using parseInt() and parseFloat() in JavaScript to convert data types to Numbers
Show how many characters remaining in a html text box using javascript
Latest Articles (in JavaScript)
© 2023 Java-samples.com
Tutorial Archive: Data Science React Native Android AJAX ASP.net C C++ C# Cocoa Cloud Computing EJB Errors Java Certification Interview iPhone Javascript JSF JSP Java Beans J2ME JDBC Linux Mac OS X MySQL Perl PHP Python Ruby SAP VB.net EJB Struts Trends WebServices XML Office 365 Hibernate
Latest Tutorials on: Data Science React Native Android AJAX ASP.net C Cocoa C++ C# EJB Errors Java Certification Interview iPhone Javascript JSF JSP Java Beans J2ME JDBC Linux Mac OS X MySQL Perl PHP Python Ruby SAP VB.net EJB Struts Cloud Computing WebServices XML Office 365 Hibernate