JavaScript DHTML/Data Type/String
Содержание
- 1 Adding trim function to String
- 2 Back slash string character
- 3 Backspace string character
- 4 Capitalize Words
- 5 Check the first letter of a name
- 6 Combine indexOf and substring methods to get the anchor part of a url
- 7 Compare string in locale format
- 8 Concat String
- 9 Count Words
- 10 Create anchor from a string
- 11 Double quote string character
- 12 Escape double quote
- 13 Get char at
- 14 Get char Code
- 15 Get Locale Lower Case string (toLocaleLowerCase)
- 16 Get Locale Upper Case string (toLocaleUpperCase)
- 17 Get Lower case String (toLowerCase)
- 18 Get small String
- 19 Get sub string (substr())
- 20 Get super script (sup)
- 21 Get Upper Case string (toUpperCase)
- 22 Including a special character in a string
- 23 Make string bold
- 24 Make String link
- 25 Match string
- 26 Return strike String
- 27 Return string value from a function
- 28 Search the string and counts the number of e"s
- 29 Single quote string character
- 30 Slice a string
- 31 Split a string
- 32 String big
- 33 String blink
- 34 String escape
- 35 String font Color
- 36 String font Size()
- 37 String from Char Code
- 38 String index Of
- 39 String in italics
- 40 String last Index Of
- 41 String length
- 42 String replace
- 43 String sub() Example
- 44 Tab string character
- 45 "unescape()" Example
- 46 Use String.indexOf method to validate an email address
- 47 Use substring method to get the sub string
- 48 Validate a time value input with string.indexOf method
Adding trim function to String
<html>
<head>
<title>Adding trim function to String</title>
</head>
<body>
<script type="text/javascript">
String.prototype.trim = function() {
return (this.replace(/^[\s\xA0]+/, "").replace(/[\s\xA0]+$/, ""));
}
var sObj = new String(" This is the string ");
sTxt = sObj.trim();
document.writeln("--" + sTxt + "--");
</script>
</body>
</html>
Back slash string character
<html>
<head>
</head>
<body>
<script type="text/javascript">
// Inline Character Constants
var TAB = "\t";
var CR = "\r";
var LF = "\n";
var CRLF = "\r\n";
var FF = "\f";
var DQUOTE = "\"";
var SQUOTE = "\"";
var BACKSLASH = "\\";
var BACKSPACE = "\b";
document.write("Column1" + TAB + "Column2" + TAB + "Column3")
</script>
</body>
</html>
Backspace string character
<html>
<head>
</head>
<body>
<script type="text/javascript">
// Inline Character Constants
var TAB = "\t";
var CR = "\r";
var LF = "\n";
var CRLF = "\r\n";
var FF = "\f";
var DQUOTE = "\"";
var SQUOTE = "\"";
var BACKSLASH = "\\";
var BACKSPACE = "\b";
document.write("Column1" + TAB + "Column2" + TAB + "Column3")
</script>
</body>
</html>
Capitalize Words
<HTML>
<HEAD>
<SCRIPT>
function capWords(str){
words = str.split(" ");
for (i=0 ; i < words.length ; i++){
testwd = words[i];
firLet = testwd.substr(0,1);
rest = testwd.substr(1, testwd.length -1)
words[i] = firLet.toUpperCase() + rest
}
document.theForm.results.value = words.join(" ");
}
</SCRIPT>
</HEAD>
<BODY>
<FORM name="theForm">
Enter a text string:
<TEXTAREA name=inStr rows=5 cols=90>
</TEXTAREA>
<INPUT type=button value="Capitalize Words" onClick="capWords(document.theForm.inStr.value)";>
<INPUT type=button value="Clear" onClick="document.theForm.inStr.value=""";>
<TEXTAREA name=results rows=5 cols=90>
</TEXTAREA>
</FORM>
</BODY>
</HTML>
Check the first letter of a name
<HTML>
<HEAD>
<SCRIPT language="JavaScript">
var the_name=window.prompt("What is your name?","");
first_char=the_name.charAt(0);
</SCRIPT>
</HEAD>
<BODY>
<SCRIPT language="JavaScript">
if (first_char=="S"){
document.write("Start with letter S");
}else{
document.write("Not start with an uppercase S");
}
</SCRIPT>
</BODY>
</HTML>
Combine indexOf and substring methods to get the anchor part of a url
<html>
<head>
<title>Splitting a string into substrings</title>
<script type="text/javascript" >
var url = "http://www.a.ru/j#ch_3";
var hash = url.indexOf("#");
var anchor = url.substring(hash + 1, url.length);
document.write(anchor);
</script>
</head>
<body>
</body>
</html>
Compare string in locale format
<html>
<body>
<button onclick="var myS = new String("Sample String");
alert(myS.localeCompare("Sample String"));">String locale Compare</button>
</body>
</html>
Concat String
<html>
<body>
<button onclick="var myS = new String("Sample String");
alert(myS.concat("Sample String 2"));">String: CONCAT</button>
</body>
</html>
Count Words
<HTML>
<HEAD>
<SCRIPT>
function countWords(str){
var count = 0;
words = str.split(" ");
for (i=0 ; i < words.length ; i++){
if (words[i] != "")
count += 1;
}
document.theForm.results.value = count;
}
</SCRIPT>
</HEAD>
<BODY>
<FORM name="theForm">
Enter a text string:
<TEXTAREA name=inStr rows=5 cols=90>
</TEXTAREA>
<INPUT type=button value="Count Words" onClick="countWords(document.theForm.inStr.value)";>
<INPUT type=button value="Clear" onClick="document.theForm.inStr.value=""";>
<TEXTAREA name=results rows=5 cols=90>
</TEXTAREA>
</FORM>
</BODY>
</HTML>
Create anchor from a string
<html>
<body>
<button onclick="var myS = new String("Sample String");
alert(myS.anchor("myAnchor"));">
string: ANCHOR
</button>
Double quote string character
<html>
<head>
</head>
<body>
<script type="text/javascript">
// Inline Character Constants
var TAB = "\t";
var CR = "\r";
var LF = "\n";
var CRLF = "\r\n";
var FF = "\f";
var DQUOTE = "\"";
var SQUOTE = "\"";
var BACKSLASH = "\\";
var BACKSPACE = "\b";
document.write("Column1" + TAB + "Column2" + TAB + "Column3")
</script>
</body>
</html>
Escape double quote
<HTML>
<BODY>
<H1>
<SCRIPT>
var s = "\"\" \"\"\"";
document.write(s);
</SCRIPT>
</H1>
</BODY>
</HTML>
Get char at
<html>
<body>
<button onclick="var myS = new String("Sample String");
alert(myS.charAt(3));">String: CHARAT</button>
</body>
</html>
Get char Code
<html>
<body>
<button onclick="var myS = new String("Sample String");
alert(myS.charCodeAt(3));">String: CHARCODEAT</button>
</body>
</html>
Get Locale Lower Case string (toLocaleLowerCase)
<html>
<body>
<button onclick="var myS = new String("www.wbex.ru");
alert(myS.toLocaleLowerCase());">
String toLocaleLowerCase</button>
</body>
</html>
Get Locale Upper Case string (toLocaleUpperCase)
<html>
<body>
<button onclick="var myS = new String("www.wbex.ru");
alert(myS.toLocaleUpperCase());">String toLocaleUpperCase</button>
</body>
</html>
Get Lower case String (toLowerCase)
<html>
<body>
<button onclick="var myS = new String("www.wbex.ru"); alert(myS.toLowerCase());">
String toLowerCase
</button>
</body>
</html>
Get small String
<html>
<body>
<button onclick="var myS = new String("www.wbex.ru");
document.write("regular string"+myS.small());">
SMALL
</button>
www.wbex.ru
</body>
</html>
Get sub string (substr())
<html>
<body>
<button onclick="var myS = new String("www.wbex.ru"); alert(myS.substr(4,7));">
substr
</button>
</body>
</html>
Get super script (sup)
<html>
<body>
<button onclick="var myS = new String("www.wbex.ru");
document.write("regular string"+myS.sup());">String sup</button>
</body>
</html>
Get Upper Case string (toUpperCase)
<html>
<body>
<button onclick="var myS = new String("www.wbex.ru"); alert(myS.toUpperCase());">
toUpperCase
</button>
</body>
</html>
Including a special character in a string
<html>
<head>
<title>Including a special character in a string</title>
<script type="text/javascript">
var a = "First line.\nSecond line.";
alert("String with a newline special character: " + a);
</script>
</head>
<body>
</body>
</html>
Make string bold
<html>
<body>
<button onclick="var myS = new String("Sample String");
document.write("regular string"+myS.bold());">String: BOLD</button>
</body>
</html>
Make String link
<html>
<body>
<button onclick="var myS = new String("wbex.ru Home Page");
alert(myS.link("http://www.wbex.ru/"));">String LINK</button>
</body>
</html>
Match string
<html>
<body>
<button onclick="var myS = new String("Sample String");
alert(myS.match(/sam/i));">String MATCH</button>
</body>
</html>
Return strike String
<html>
<body>
<button onclick="var myS = new String("String Text");
document.write("regular string"+myS.strike());">String strike</button>
</body>
</html>
Return string value from a function
<HTML>
<HEAD>
<SCRIPT language="JavaScript">
function getText(t1, t2)
{
var t = t1+" "+t2;
return t;
}
function setText()
{
var t=getText("Hi","there!");
document.write(t);
}
var regtext=getText("A ","B!");
window.alert(regtext);
</SCRIPT>
</HEAD>
<BODY>
<SCRIPT language="JavaScript">
setText();
</SCRIPT>
</BODY>
</HTML>
Search the string and counts the number of e"s
<html>
<head>
</head>
<body>
<script type="text/javascript">
var pos = 0;
var num = -1;
var i = -1;
var myString = "this is a test";
while (pos != -1) {
pos = myString.indexOf("e", i + 1);
num += 1;
i = pos;
}
alert(num + " e");
</script>
</body>
</html>
Single quote string character
<html>
<head>
</head>
<body>
<script type="text/javascript">
// Inline Character Constants
var TAB = "\t";
var CR = "\r";
var LF = "\n";
var CRLF = "\r\n";
var FF = "\f";
var DQUOTE = "\"";
var SQUOTE = "\"";
var BACKSLASH = "\\";
var BACKSPACE = "\b";
document.write("Column1" + TAB + "Column2" + TAB + "Column3")
</script>
</body>
</html>
Slice a string
<html>
<body>
<button onclick="var myS = new String("www.wbex.ru"); alert(myS.slice(4,7));">
SLICE STRING
</button>
<button onclick="var myA = new Array(10,11,12,13,14,15,11,12,13,14); alert(myA.slice(1,3));">
SLICE ARRAY
</button>
</body>
</html>
Split a string
<html>
<body>
<script language="javascript">
function function1(){
var myString = "www. wbex .ru";
var myArray = myString.split(" ");
alert(myArray.length);
}
</script>
<button onclick="function1();">String split</button>
</body>
</html>
String big
<html>
<body>
<button onclick="var myS = new String("Sample String");
document.write("regular string"+myS.big());">String: BIG</button>
</body>
</html>
String blink
<html>
<body>
<button onclick="var myS = new String("Sample String");
document.write(myS.blink());">String: BLINK</button>
</body>
</html>
String escape
<html>
<body>
<button onclick="alert(escape("Hello World!"));">
Escape function: escape("Hello World!")
</button>
<button onclick="alert(unescape("Hello%20World%21"));">
Unescape function: unescape("Hello%20World%21")
</button>
</body>
</html>
String font Color
<html>
<body>
<button onclick="var myS = new String("Sample String");
document.write("regular string"+myS.fontcolor("blue"));">
String: FONTCOLOR</button>
</body>
</html>
String font Size()
<html>
<body>
<button onclick="var myS = new String("Sample String");
document.write("regular string"+myS.fontsize(7));">
String: FONTSIZE</button>
</body>
</html>
String from Char Code
<html>
<body>
<button onclick="alert(String.fromCharCode(79,80));">String: from Char Code</button>
</body>
</html>
String index Of
<html>
<body>
<button onclick="var myS = new String("Sample String");
alert(myS.indexOf("String"));">string indexOf</button>
</body>
</html>
String in italics
<html>
<body>
<button onclick="var myS = new String("Sample String");
document.write("regular string"+myS.italics());">String: ITALICS</button>
</body>
</html>
String last Index Of
<html>
<body>
<button onclick="var myS = new String("Sample String String");
alert(myS.lastIndexOf("String"));">String lastIndexOf</button>
</body>
</html>
String length
<html>
<body>
<p id="myP">How many characters in this sample text?</p>
<button onclick="function1();">Click me to find out</button>
<script language="JavaScript">
function function1() {
var m = "How many characters in this sample text?".length;
alert(m);
}
</script>
</body>
</html>
String replace
<html>
<body>
<button onclick="var myS = new String("Sample String");
alert(myS.replace(/sam/i,"Text"));">String replace</button>
</body>
</html>
String sub() Example
<html>
<body>
<button onclick="var myS = new String("www.wbex.ru");
document.write("regular string"+myS.sub());">
String sub
</button>
</body>
</html>
Tab string character
<html>
<head>
</head>
<body>
<script type="text/javascript">
// Inline Character Constants
var TAB = "\t";
var CR = "\r";
var LF = "\n";
var CRLF = "\r\n";
var FF = "\f";
var DQUOTE = "\"";
var SQUOTE = "\"";
var BACKSLASH = "\\";
var BACKSPACE = "\b";
document.write("Column1" + TAB + "Column2" + TAB + "Column3")
</script>
</body>
</html>
"unescape()" Example
<html>
<body>
<button onclick="alert(escape("www.wbex.ru"));">Escape function</button>
<button onclick="alert(unescape("www.wbex.ru%20www.wbex.ru%21"));">Unescape function</button>
</body>
</html>
Use String.indexOf method to validate an email address
<HTML>
<HEAD>
<SCRIPT language="JavaScript">
function get_add(){
var email_add=window.prompt("What is your e-mail address?","");
if (email_add.indexOf("@") == -1) {
window.alert("No "@" symbol in your address");
}
if (email_add.indexOf(".") == -1){
window.alert("No "." symbol in your address");
}
if ( (email_add.indexOf("@") != -1) && (email_add.indexOf(".") != -1) )
{
window.alert("Thanks!");
}
}
</SCRIPT>
</HEAD>
<BODY>
<FORM>
<INPUT type="button" value="Click to enter an e-mail address" onClick="get_add();">
</BODY>
</HTML>
Use substring method to get the sub string
<html>
<head>
<title>Splitting a string into substrings</title>
<script type="text/javascript" >
var a = "Bytes and bits";
var b = a.substring(10, 13);
document.write(b);
</script>
</head>
<body>
</body>
</html>
Validate a time value input with string.indexOf method
<HTML>
<HEAD>
<SCRIPT language="JavaScript">
function check_it()
{
var thetext=document.myForm.the_time.value;
if (thetext.indexOf(":") == -1)
{
window.alert("No colon(:)");
return false;
}
else
{
return true;
}
}
</SCRIPT>
</HEAD>
<BODY>
<FORM name="myForm" action="#" onSubmit="return check_it();">
Time:<INPUT type="text" name="the_time">
<INPUT type="submit" value="Submit">
</BODY>
</HTML>