一、String.charAt(index)

作用:获取字符串指定索引位置的字符

注意:index的值是0~(字符串长度-1)之间的值

<script type="text/javascript">
var str ="my name is javascript";
var c2 = str.charAt(2);
var c3 = str.charAt(3);
var c6 = str.charAt(6);
var c11 = str.charAt(11);
var c15 = str.charAt(15);
console.info("第3个位置的字符是"+c2);
console.info("第4个位置的字符是"+c3);
console.info("第7个位置的字符是"+c6);
console.info("第12个位置的字符是"+c11);
console.info("第16个位置的字符是"+c15);
</script> 运行结果是:
第3个位置的字符是
第4个位置的字符是n
第7个位置的字符是e
第12个位置的字符是j
第16个位置的字符是s

二、String.substring(start[,end])和String.substr(start[,length])

1、String.substring(start[,end])):返回指定开始位置和结束位置之间的字符串

参数所代表的意义

start:字符串起始位置,必选项,是一个非负数。

end:字符串终止位置,可选项,是一个非负数

结果

截取的结果是从start~end-1之间的长度为end-start的字符串,即截取的子串中不包含最后end位置的字符

参数的限制

当start=end时,截取的子串是”“

当start>end时,截取之前会自动交换位置

当start或end为负数时,会自动替换为0

当end不填时会截取从start开始到字符串结尾处的子串

2、String.substri(start[,length])):返回从start开始,长度为length的字符串

参数所代表的意义

start:字符串起始位置,必选项,非负数

length:要截取的子串的长度,可选项,非负数

结果

截取的是从start位置开始,长度为length的子串

参数的限制

当start为负数时,start=str.length+start

当length为0时返回的子串是""

当length不选时返回start到字符串结尾处的子串

下面是代码示例:

 <script type="text/javascript">
testSubstr();
function testSubstr(){
var str = "0123456789";
console.info(str.substring(0));//
console.info(str.substring(3));//
console.info(str.substring(10));//""
console.info(str.substring(12));//""
console.info(str.substring(-2));//
console.info(str.substring(0,5));//
console.info(str.substring(0,9));//
console.info(str.substring(0,10));//
console.info(str.substring(2,7));//
console.info(str.substring(0,-2));//""
console.info(str.substring(3,-4));//
console.info(str.substring(-2,-6));//"" console.info(str.substr(0));//
console.info(str.substr(3));//
console.info(str.substr(10));//""
console.info(str.substr(12));//""
console.info(str.substr(-2));//
console.info(str.substr(0,5));//
console.info(str.substr(0,9));//
console.info(str.substr(0,10));//
console.info(str.substr(2,7));//
console.info(str.substr(0,-2));//""
console.info(str.substr(3,-4));//""
console.info(str.substr(-2,-6));//""
}
</script>

三、typeof(var)函数

作用:检测给定变量的数据类型

JavaScript中的数据类型是弱类型,一个变量var可以用来存放各种数据类型。

下面通过代码来测试:

 function testTypeof(){
console.info(typeof("hello")); //string
console.info(typeof(11)); //number
console.info(typeof(true)); //boolean
console.info(typeof(document)); //object
console.info(typeof(click)); //undefined
console.info(typeof(function head(){})); //function
}
testTypeof(); 浏览器控制台的执行结果如下:
string
number
boolean
object
undefined
function
   function testTypeof(){
console.info("------包含括号的方式------");
console.info(typeof("hello")); //string
console.info(typeof(11)); //number
console.info(typeof(true)); //boolean
console.info(typeof(document)); //object
console.info(typeof(click)); //undefined
console.info(typeof(function head(){})); //function
console.info("------不包含括号的方式------");
console.info(typeof "world"); //string
console.info(typeof 23); //number
console.info(typeof false); //boolean
console.info(typeof onclick); //object
console.info(typeof click); //undefined
console.info(typeof function foot(){}); //function
} testTypeof(); 浏览器控制台输出结果:
------包含括号的方式------ base1.js:48
string base1.js:49
number base1.js:50
boolean base1.js:51
object base1.js:52
undefined base1.js:53
function base1.js:54
------不包含括号的方式------ base1.js:55
string base1.js:56
number base1.js:57
boolean base1.js:58
object base1.js:59
undefined base1.js:60
function

项目中的那些事---JavaScript的更多相关文章

  1. 记录下项目中常用到的JavaScript/JQuery代码二(大量实例)

    记录下项目中常用到的JavaScript/JQuery代码一(大量实例) 1.input输入框监听变化 <input type="text" style="widt ...

  2. 1000多个项目中的十大JavaScript错误以及如何避免

    通过统计数据库中的1000多个项目,我们发现在 JavaScript 中最常出现的错误有10个.下面会向大家介绍这些错误发生的原因以及如何防止. 对于这些错误发生的次数,我们是通过收集的数据统计得出的 ...

  3. 项目中的那些事---Java反射的应用

    最近工作中遇到一个这样的问题: 为某个项目中的所有接口做一个测试工具,使用java Swing技术,该项目有不同的版本,不是所有版本中的接口都是相同的,而我做的工具需要兼容所有版本. 于是就引入了这样 ...

  4. 记录下项目中常用到的JavaScript/JQuery代码一(大量实例)

    一直没有系统学习Javascript和Jquery,每次都是用到的时候去搜索引擎查,感觉效率挺低的.这边把我项目中用的的记录下,想到哪写哪,有时间再仔细整理. 当然,由于我主要是写后端java开发,而 ...

  5. 项目中的那些事---下载pdf文件

    最近做了一个下载pdf文档的需求,本以为使用HTML5中<a>标签的属性download就能简单搞定,不料IE竟然不支持这一简单粗暴的H5新特性,而是直接在网页中打开, 于是各种搜索之后得 ...

  6. 项目中的那些事---PHP函数

    总结工作中遇到的php函数: 1.查找:strpos("str", "substr"): 查找substr字符串在str字符串中出现的位置 第一个参数是:被查找 ...

  7. mui项目中如何使用原生JavaScript代替jquery来操作dom 转自【B5教程网】:http://www.bcty365.com/content-146-3661-1.html

    最近在用mui写页面,当然了在移动App里引入jq或zepto这些框架,肯定是极不理性的.原生JS挺简单,为何需要jq?jq的成功当时是因为ie6.7.8.9.10.chrome.ff这些浏览器不兼容 ...

  8. mui项目中如何使用原生JavaScript代替jquery来操作dom

    最近在用mui写页面,当然了在移动App里引入jq或zepto这些框架,肯定是极不理性的.原生JS挺简单,为何需要jq?jq的成功当时是因为ie6.7.8.9.10.chrome.ff这些浏览器不兼容 ...

  9. Jquery和Javascript 实际项目中写法基础-弹出窗和弹出层 (4)

     一.实际项目中有很多如下界面效果.    二.该效果可以归结为弹出窗或者弹出层来实现的,为什么这么说?看如下代码:      <!DOCTYPE html> <html> & ...

随机推荐

  1. sessionapplicationStruts2中访问web元素

    本文是一篇关于sessionapplication的帖子 取得Map类型request,session,application,实在类型 HttpServletRequest, HttpSession ...

  2. 【WinForm】C# 发送Email

    发送Email  的条件 1.SmtpClient SMTP 协议    即 Host 处理事务的主机或IP地址     //smtp.163.com      UseDefaultCredentia ...

  3. Codeforces Round #330 (Div. 1) A. Warrior and Archer 贪心 数学

    A. Warrior and Archer Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/594 ...

  4. 关于Android与pc通信时中文乱码的分析和解决

    初步实现了Android与pc服务器的通信之后,又碰到了传说中令人头疼不已的中文乱码问题.既然出现了乱码,那么原因自然是协议不通了.我们知道eclipse中默认的编码标准是GBK,而安卓程序开发所默认 ...

  5. HDU 1498 50 years, 50 colors (行列匹配+最小顶点覆盖)

    题目:点击打开链接 题意:每个格子有不同颜色的气球用不同数字表示,每次可选某一行              或某一列来戳气球.每个人有K次机会.求最后哪些气球不能在             k次机会内 ...

  6. 使用Java高速实现进度条

    基于有人问到如何做进度条,以下给个简单的做法: 主要是使用JProgressBar(Swing内置javax.swing.JProgressBar)和SwingWorker(Swing内置javax. ...

  7. LINUX 运维命令

    查看3306端口被什么程序占用 [root@DB13 ~]# lsof -i : COMMAND PID USER FD TYPE DEVICE SIZE NODE NAME mysqld mysql ...

  8. The internals of Python string interning

    JUNE 28TH, 2014Tweet This article describes how Python string interning works in CPython 2.7.7. A fe ...

  9. c#线程问题(1)

    delegate : public delegate void ParameterizedThreadStart(Object obj) public delegate void ThreadStar ...

  10. 利用 Composer 一步一步构建自己的 PHP 框架(二)——构建路由

    本教程示例代码见 https://github.com/johnlui/My-First-Framework-based-on-Composer 上一篇中我们已经建立了一个空的 Composer 项目 ...