- CoffeeScript 教程
- CoffeeScript - 主页
- CoffeeScript - 概览
- CoffeeScript - 环境
- CoffeeScript - 命令行实用工具
- CoffeeScript - 语法
- CoffeeScript - 数据类型
- CoffeeScript - 变量
- CoffeeScript - 运算符和别名
- CoffeeScript - 条件语句
- CoffeeScript - 循环语句
- CoffeeScript - 理解
- CoffeeScript - 函数
- CoffeeScript 面向对象
- CoffeeScript - 字符串
- CoffeeScript - 数组
- CoffeeScript - 对象
- CoffeeScript - 范围
- CoffeeScript - 展开运算符
- CoffeeScript - 日期
- CoffeeScript - 数学
- CoffeeScript - 异常处理
- CoffeeScript - 正则表达式
- CoffeeScript - 类和继承
- CoffeeScript 高级
- CoffeeScript - Ajax
- CoffeeScript - jQuery
- CoffeeScript - MongoDB
- CoffeeScript - SQLite
- CoffeeScript 实用资源
- CoffeeScript - 快速指南
- CoffeeScript - 实用资源
- CoffeeScript - 讨论
CoffeeScript 字符串 - substr()
说明
此方法用于返回字符串所需的部分字符串。它接受一个整数值,表示部分字符串的起始值和字符串的长度,并返回所需的部分字符串。如果起始值是负数,则 substr() 方法将其用作字符串结尾的字符索引。
语法
下面给出了 JavaScript 的 substr() 方法的语法。我们可以在 CoffeeScript 代码中使用相同的方法。
string.substr(start[, length])
示例
以下示例演示了在 CoffeeScript 代码中使用 JavaScript 的 substr() 方法。将此代码保存在名为 string_substr.coffee 的文件中
str = "Apples are round, and apples are juicy."; console.log "The sub string having start and length as (1,2) is : " + str.substr 1,2 console.log "The sub string having start and length as (-2,2) is : " + str.substr -2,2 console.log "The sub string having start and length as (1) is : " + str.substr 1 console.log "The sub string having start and length as (-20, 2) is : " + str.substr -20,2 console.log "The sub string having start and length as (20, 2) is : " + str.substr 20,2;
打开 命令提示符 并编译 .coffee 文件,如下所示。
c:\> coffee -c coffee string_substr.coffee
在编译时,它会给你以下 JavaScript。
// Generated by CoffeeScript 1.10.0 (function() { var str; str = "Apples are round, and apples are juicy."; console.log("The sub string having start and length as (1,2) is : " + str.substr(1, 2)); console.log("The sub string having start and length as (-2,2) is : " + str.substr(-2, 2)); console.log("The sub string having start and length as (1) is : " + str.substr(1)); console.log("The sub string having start and length as (-20, 2) is : " + str.substr(-20, 2)); console.log("The sub string having start and length as (20, 2) is : " + str.substr(20, 2)); }).call(this);
现在,再次打开 命令提示符 并运行 CoffeeScript 文件,如下所示。
c:\> coffee string_substr.coffee
在执行时,CoffeeScript 文件会产生以下输出。
The sub string having start and length as (1,2) is : pp The sub string having start and length as (-2,2) is : y. The sub string having start and length as (1) is : pples are round, and apples are juicy. The sub string having start and length as (-20, 2) is : nd The sub string having start and length as (20, 2) is : d
coffeescript_strings.htm
广告