| home / programming / java_core / 2 | [previous] [next] |
|
|
|
<head><title>String Manipulation</title></head> <h2>Working with String Manipulation Methods</h2> <script language="JavaScript"> 1 var str1 = new String("The merry, merry month of June..."); document.write("In the string:<em> "+ str1 ); 2 document.write("</em> the first 'm' is at position " + 3 document.write("The last 'm' is at position " + 4 document.write("<em>str1.substr(4,5)</em> returns<em> " + |
You may have to do more than just find a substring within a string, you may need to extract that substring. For example, we found the @ in the e-mail address, now we may want to get just the user name or the server name or domain name. To do this, JavaScript provides methods such as splice(), split(), charAt(), substr(), and substring().
|
|
|---|
|
<html><head><title>Extracting Substrings</title> <script language="JavaScript"> 1 var straddr = "DanielSavage@dadserver.org";
document.write("<br>His name is<em> " + 3 var namesarr = straddr.split("@" ); 4 document.write( "The user name is<em> " + namesarr[0] + 5 document.write( "and the mail server is<em> " + namesarr[1] + 6 document.write( "The first character in the string is <em>" + straddr.charAt(0)+ "</em>.<br>"); 7 document.write( "and the last character in the string is <em>" |
|
|
|---|
A string is assigned an e-mail address. |
|
| home / programming / java_core / 2 | [previous] [next] |
| ||||||||||||||||||||
Created: March 27, 2003
Revised: November 19, 2003
URL: http://webreference.com/programming/java_core/2