函数 字符串

复制代码
 1 函数:封装了某一块功能(封装函数的目的是可以使这个函数可以重复利用)
 2 函数四要素:1.返回类型 2.函数名 3.参数列表 4.函数体
 3 函数的基本写法:
 4 1.强类型语言
 5 返回值类型[string] 函数名[Show]( 参数[int a]多个的话就是函数列表)
 6 {
 7 函数体
 8 }
 9 2.弱类型语言
10 代表它是个函数[function] 函数名(参数列表)
11 {
12 函数体
13 }
14 
15 function(){} //匿名函数
16 
17 函数的例子:
18 
19 //1.调用下面那个函数:函数名加括号
20 Show();
21 //输出一句话:没有任何效果,只是封装了,想用这个功能只能调用它,任何位置都可以调用它
22 function Show()
23 {
24 alert("这是一个函数");
25 }
26 
27 //2.有参数的函数
28 function Show(a) //形参,形式参数
29 {
30 alert(a);
31 }
32 //调用:一样没有函数类型,因此这里的参数类型可以是任何类型
33 Show("你好"); //实参,实际参数
34 
35 //3.有多个参数的函数
36 function Sum(a,b) //形参的名字可以随便去,因为是虚假的
37 {
38 alert(a+b);
39 }
40 //调用
41 Sum(20,10);
42 
43 //4.有返回值的参数
44 function Sum(a,b)
45 {
46 return a+b; //返回用return,不是输出了alert
47 }
48 
49 var a = Sum(11,12); //Sum(11,12)这个其实相当于就是返回了23,交给个变量存起来;
50 alert(a);
51 
52 
53 js中函数经常用于事件;其他的语言就是用在封装上
54 
55 js中常见的函数:
56 
57 //随机数:生成器需要一个种子,就是当前系统时间 Math.random()
58 例如:alert(Math.random()); //0-1之间的随机数
59 alert(Math.random()*n); //0-n之间的随机数
复制代码
复制代码
//日期时间函数(需要用变量调用): 
1 var b= new Date (); 2 alert(new Date ()); //当前的时间 3 alert(b.getTime()); //获取时间戳 4 alert(b.getFullYear()); //获取年份 5 alert(b.getMonth()+1); //获取月份 6 alert(b.getDate()); //获取天 7 alert(b.getHours()); //获取小时 8 alert(b.getMinutes()); //获取分钟 9 alert(b.getSeconds()); //获取秒数 10 alert(b.getDay()); //获取星期几 11 alert(b.getMilliseconds()); //获取毫秒
复制代码
复制代码
//数学函数(用Math来调用): 
1 alert(Math.abs(-100)); //返回数的绝对值。 2 alert(Math.ceil(1.9)); //对小数进行上舍入。 3 alert(Math.floor(11.9)); //对数进行下舍入。 4 alert(Math.round(11.1)); //把数四舍五入为最接近的整数 5 alert(Math.max(2,5)); //返回 x 和 y 中的最高值。 6 alert(Math.min(2,5)); //返回 x 和 y 中的最低值。 7 alert(Math.pow(10,2)); //返回 x 的 y 次幂。 100 8 alert(Math.sqrt(4)); //返回数的平方根 9 alert(Math.random()); //返回 0 ~ 1 之间的随机数。 **** 10 alert(Math.random()*n); //返回 0 ~ n 之间的随机数。 ****
复制代码
复制代码

//字符串函数(用变量来调用):
indexOf:返回字符串中一个子串第一处出现的索引(从左到右搜索)。如果没有匹配项,返回 -1

1 var str ="happy"; 2 alert(str.indexOf("p")); //返回第一处的p的索引是2 3 var str ="happy"; 4 alert(str.indexOf("e")); //返回的是-1,就是没有找到e;
复制代码
复制代码
harAt:返回指定位置的字符。//var get_char = a.charAt(0);//get_char = "h"
1 match:检查一个字符串匹配一个正则表达式内容,如果没有匹配返回 null。//var re = new RegExp(/^w+$/);//var is_alpha1 = a.match(re);//is_alpha1 = "hello"//var is_alpha2 = b.match(re);//is_alpha2 = null
2 
3 substring:返回字符串的一个子串,传入参数是起始位置和结束位置。//var sub_string2 = a.substring(1,4);//sub_string2 = "ell"
4 var str="happy";
5 str1=str.substring(2,5);
6 alert(str1);

var get_char="happy"; alert(get_char.charAt(3)); //和indexOf差不多,只不过这个是指定位置找字符,前者是指定字符找位置
1 lastIndexOf:返回字符串中一个子串最后一处出现的索引(从右到左搜索),如果没有匹配项,返回 -1 。//var index1 = lastIndexOf('l');//index1 = 3
2         var str= "happy";
3       alert(str.lastIndexOf("a"));
4 //这个和indexOf相反,这个是从右向左找,前者是从左向右找



1 substr ********
2 //返回字符串的一个子串,传入参数是起始位置和长度...//var sub_string1 = a.substr(1);//sub_string1 = "ello"//var sub_string2 = a.substr(1,4);//sub_string2 = "ello"
3 
4 var sub_string = "hello";
5 sub_string1=sub_string.substr(1,3);
6 alert(sub_string1);



1 replace *******
2 //替换字符串,第一个参数代表被替换的字符串,第二个参数代表替换的字符串..//a.replace("he","aa")
3 
4 var a="hello";
5 alert(a.replace("h","l"));



复制代码
 1 search
 2 //执行一个正则表达式匹配查找。如果查找成功,返回字符串中匹配的索引值。否则返回 -1 。//var index1 = a.search(re);//index1 = 0  //var index2 = b.search(re);
 3 //index2 = -1
 4 
 5 var str="hello";
 6 str=str.search("o");
 7 alert(str);//返回的索引号4
 8 var str="hello";
 9 str=str.search("a");
10 alert(str); //返回的就是-1,因为没有找到a
复制代码



split ******
//通过将字符串划分成子串,将一个字符串做成一个字符串数组。 //var arr1 = a.split("");//arr1 = [h,e,l,l,o]

var arr1 = "hello";
alert(arr1.split([]));



toLowerCase
//将整个字符串转成小写字母。//var lower_string = a.toLowerCase();//lower_string = "hello"

var str="HEElo";
alert(str.toLowerCase());



1 toUpperCase
2 
3 //将整个字符串转成大写字母。//var upper_string = a.toUpperCase();//upper_string = "HELLO"
4 
5 var str="hapPP";
6 alert(str.toUpperCase());


 
复制代码
原文地址:https://www.cnblogs.com/xieyulin/p/7070362.html