1)绝对值,随机:
var v1 = Math.abs(n);
var v2 = Math.abs(-n);
if(v1 == v2) alert('equals!!!'+' '+v1+' '+v2);
Math.random():返回一个介于0和1之间的伪随机数。
2)Js中像HashMap,HashTable的用法(key,value):
var d;
d= new ActiveXObject("Scripting.Dictionary");
d.Add("a","Athens"); //key,value
d.Add("b","bbbb");
d.Add("c","cccc");
alert(d.Item("a")); //通过key得到value
var arrKeys = new VBArray(d.Keys()).toArray(); //keys存放在arrKeys
var arrItems = new VBArray(d.Items()).toArray(); //Items存放在arrItems
var strThisKey,strThisItem;
var temp = new Array(d.Count);
for (intLoop = 0; intLoop < d.Count; intLoop++) {
strThisKey = arrKeys[intLoop];
strThisItem = arrItems[intLoop];
alert(strThisKey+' '+strThisItem);
temp[intLoop] = strThisItem;
}
for(k=0;k<temp.length;k++)
alert('temp: '+temp[k]);
3)数组的用法:
var my_array = new Array();
var str ;
for(i=0;i<10;i++)
{
my_array[i] = i;
str += my_array[i];
}
my_array.length 得到数组的长度
4)charCodeAt,charAt,concat用法
var str = "ABCDEFGHIJLMN";
var s,a,b,c;
s = str.charCodeAt(n-1);//返回的是unicode,A=65
s = str.charAt(n-1);//返回一个字符
a = new Array(1,2,3,4);
b = new Array(4,5,6);
c = a.concat(b);//长度为7
5)得到当前日期:
var now = new Date();
alert(now.getYear()+'-'+(now.getMonth()+1)+'-'+now.getDate()+'--'+now.getHours()+':'+now.getMinutes()+':'+now.getSeconds());
6)FileSystemObject处理文件
有两种主要的文件处理类型:1.创建、添加或删除数据,以及读取文件 2.移动、复制和删除文件
一、创建文件:
创建空文本文件(有时被叫做“文本流”)有三种方法。
i.CreateTextFile 方法
var fso, f1;
fso = new ActiveXObject("Scripting.FileSystemObject");
f1 = fso.CreateTextFile("c://testfile.txt", true);
ii.使用 FileSystemObject 对象的 OpenTextFile 方法,并设置 ForWriting 标志
var fso, ts;
var ForWriting= 2; //打开一个文件,供写入。如文件存在将以前的内容覆盖。
fso = new ActiveXObject("Scripting.FileSystemObject");
ts = fso.OpenTextFile("c://test.txt", ForWriting, true);
iii.使用 OpenAsTextStream 方法,并设置 ForWriting 标志
var fso, f1, ts;
var ForWriting = 2;
fso = new ActiveXObject("Scripting.FileSystemObject");
fso.CreateTextFile ("c://test1.txt");
f1 = fso.GetFile("c://test1.txt");
ts = f1.OpenAsTextStream(ForWriting, true);
二、添加数据到文件(写文件): 打开文本文件->写入数据->关闭文件
向打开的文本文件写数据,不用后续一个新行字符。 Write
向打开的文本文件写数据,后续一个新行字符。 WriteLine
向打开的文本文件写一个或多个空白行。 WriteBlankLines
代码:
function CreateFile()
{
var fso, tf;
fso = new ActiveXObject("Scripting.FileSystemObject");
tf = fso.CreateTextFile("c://testfile.txt", true);
// 写一行,并且带有新行字符。
tf.WriteLine("Testing 1, 2, 3.") ;
// 向文件写三个新行字符。
tf.WriteBlankLines(3) ;
// 写一行。
tf.Write ("This is a test.");
tf.Close();
}
三、读取文件
从文件读取指定数量的字符。 Read
读取一整行(一直到但不包括新行字符)。 ReadLine
读取文本文件的整个内容。 ReadAll
代码:
function ReadFiles()
{
var fso, f1, ts, s;
var ForReading = 1; //打开一个文件,只供读出。你不能向这个文件写入
fso = new ActiveXObject("Scripting.FileSystemObject");
f1 = fso.CreateTextFile("c://testfile.txt", true);
// 写一行。
Response.Write("Writing file");
f1.WriteLine("Hello World");
f1.WriteBlankLines(1);
f1.Close();
// 读取文件的内容。
Response.Write("Reading file");
ts = fso.OpenTextFile("c://testfile.txt", ForReading);
s = ts.ReadLine();
Response.Write("File contents = '" + s + "'");
ts.Close();
}
四、移动、复制和删除文件
移动文件 File.Move 或 FileSystemObject.MoveFile
复制文件 File.Copy 或 FileSystemObject.CopyFile
删除文件 File.Delete 或 FileSystemObject.DeleteFile
示例: 在驱动器 C 的根目录中创建一个文本文件,向其中写一些信息,然后把它移动到 /tmp 目录中,并在 /temp 中做一个备份,最后把它们从两个目录中删掉
function ManipFiles()
{
var fso, f1, f2, s;
fso = new ActiveXObject("Scripting.FileSystemObject");
f1 = fso.CreateTextFile("c://testfile.txt", true);
Response.Write("Writing file");
// 写一行。
f1.Write("This is a test.");
// 关闭文件。
f1.Close();
Response.Write("Moving file to c://tmp ");
// 获取 C 的根目录(C:/)中的文件的句柄。
f2 = fso.GetFile("c://testfile.txt");
// 把文件移动到 /tmp 目录。
f2.Move ("c://tmp//testfile.txt");
Response.Write("Copying file to c://temp ");
// 把文件复制到 /temp 目录。
f2.Copy ("c://temp//testfile.txt");
Response.Write("Deleting files ");
// 获得文件当前位置的句柄。
f2 = fso.GetFile("c://tmp//testfile.txt");
f3 = fso.GetFile("c://temp//testfile.txt");
// 删除文件。
f2.Delete();
f3.Delete();
Response.Write("All done!");
}
7)lastIndexOf,indexOf用法:在一个String对象中寻找第一次出现的子串。如果找不找则这个子串,就返回一个-1。
8)isNaN方法:确定一个值是不是保留的值NaN(非数)。如果是数字返回false,字母则true。
9)parseFloat(str):把一个串转变成浮点数。parseInt(str):把一个串转变成一个整数。
10)reverse方法:把Array对象中的元素进行倒转。
11)stringObject.slice(start,[end]):返回串的一部分。(截取字符串)
str.slice(0,-1):从第一个开始到倒数第二个串。
如:var str = "star008";则显示:star00
12)split方法:拆分成串数组。
例如: var temp,s;
s= "THis is a cs";
temp=s.split(" ");//以空格拆分,如果不是空格,则一个个拆分。
alert(temp); //显示: This,is,a,cs
13)substr方法:返回一个子串,这个子串在规定的位置开始,并且有规定的长度。
语法:stringvar.substr(start[,length]),从0开始
14)substring方法:在String对象中的指定位置检取子串。
语法:strVariable.substring(start,end),从0开始
15)正则表达式:http://edu.yesky.com/edupxpt/18/2143018.shtml
语法1:var regularexpression = /pattern/[switch]
语法2:var regularexpression = new RegExp"pattern"[switch]
switch包括:[1]、I(不管大小写) [2]、g(对所有出现的模式进行全局搜寻) [3]、gi(全局搜寻,不管大小写)
^输入开始或行开始 $输入结束或行结束 *0到多个 +1到多个 ?0到1个
x|y同x或y匹配。 {n}n是一个非负整数,n次相匹配 {n,m}最少同n倍匹配,最多同m倍匹配
[xyz]一个字符组,字符中任何一个字符匹配。 [^xyz]不同括起来的任何字符匹配
/d数字匹配 <==>[0-9] /D非数字匹配 <==>[^0-9] /n换行匹配 /r回车匹配