Coffeescript 简明教程

CoffeeScript - Strings

String 对象允许你使用一系列字符。在大多数编程语言中,在 CoffeeScript 中字符串使用引号声明,如下所示:−

The String object lets you work with a series of characters. As in most of the programming languages, the Strings in CoffeeScript are declared using quotes as −

my_string = "Hello how are you"
console.log my_string

在编译时,它会生成以下 JavaScript 代码。

On compiling, it will generate the following JavaScript code.

// Generated by CoffeeScript 1.10.0
(function() {
  var my_string;

  my_string = "Hello how are you";

  console.log(my_string);

}).call(this);

String Concatenation

我们可以使用“+”符号连接两个字符串,如下所示。

We can concatenate two strings using the "+" symbol as shown below.

new_string = "Hello how are you "+"Welcome to Tutorialspoint"
console.log new_String

在编译时,它会生成以下 JavaScript 代码。

On compiling, it will generate the following JavaScript code.

// Generated by CoffeeScript 1.10.0
(function() {
  var new_string;

  new_string = "Hello how are you " + "Welcome to Tutorialspoint";

  console.log(new_String);

}).call(this);

如果你执行上述示例,你可以观察到连接后的字符串,如下所示。

If you execute the above example, you can observe the concatenated String as shown below.

Hello how are you Welcome to Tutorialspoint

String Interpolation

CoffeeScript 还提供一个称为 String interpolation 的功能,用于在字符串中包含变量。CoffeeScript 的该功能受 Ruby 语言启发。

CoffeeScript also provides a feature known as String interpolation to include variables in stings. This feature of CoffeeScript was inspired from Ruby language.

字符串内插使用双引号 "" 、一个井号 # 和一对花括号 { } 完成。字符串在双引号中声明,要内插的变量包装在花括号中,花括号之前加上井号,如下所示。

String interpolation was done using the double quotes "", a hash tag # and a pair of curly braces { }. The String is declared in double quotes and the variable that is to be interpolated is wrapped within the curly braces which are prefixed by a hash tag as shown below.

name = "Raju"
age = 26
message ="Hello #{name} your age is #{age}"
console.log message

在编译上述示例时,它会生成以下 JavaScript。此处你可以观察到,字符串内插使用 + 符号转换为正常的连接。

On compiling the above example, it generates the following JavaScript. Here you can observe the String interpolation is converted into normal concatenation using the + symbol.

// Generated by CoffeeScript 1.10.0
(function() {
  var age, message, name;

  name = "Raju";

  age = 26;

  message = "Hello " + name + " your age is " + age;

  console.log(message);

}).call(this);

如果你执行上述 CoffeeScript 代码,它会给你以下输出。

If you execute the above CoffeeScript code, it gives you the following output.

Hello Raju your age is 26

只有当字符串用双引号 " " 括起来时,才会内插作为 #{variable} 传递的变量。使用单引号 ' ' (而不是双引号)会产生没有内插的文本行。请考虑以下示例。

The variable that is passed as #{variable} is interpolated only if the string is enclosed between double quotes " ". Using single quotes ' ' instead of double quotes produces the line as it is without interpolation. Consider the following example.

name = "Raju"
age = 26
message ='Hello #{name} your age is #{age}'
console.log message

如果我们使用单引号而不是双引号在插值中,你将得到以下输出。

If we use single quotes instead of double quotes in interpolation, you will get the following output.

Hello #{name} your age is #{age}

CoffeeScript 允许多行字符串而不连接它们,如下所示。

CoffeeScript allows multiple lines in Strings without concatenating them as shown below.

my_string = "hello how are you
Welcome to tutorialspoint
Have a nice day."
console.log my_string

它生成以下输出。

It generates the following output.

hello how are you Welcome to tutorialspoint Have a nice day.

JavaScript String Object

JavaScript 的 String 对象允许你处理一系列字符。此对象提供给你许多方法,可在字符串上执行各种操作。

The String object of JavaScript lets you work with a series of characters. This object provides you a lot of methods to perform various operations on Stings.

由于我们可以在 CoffeeScript 代码中使用 JavaScript 库,因此我们可以在 CoffeeScript 程序中使用所有这些方法。

Since we can use JavaScript libraries in our CoffeeScript code, we can use all those methods in our CoffeeScript programs.

String Methods

以下是 JavaScript 的 String 对象方法的列表。点击这些方法的名称来获取展示它们在 CoffeeScript 中的用法示例。

Following is the list of methods of the String object of JavaScript. Click on the name of these methods to get an example demonstrating their usage in CoffeeScript.

S.No.

Method & Description

1

charAt()Returns the character at the specified index.

2

charCodeAt()Returns a number indicating the Unicode value of the character at the given index.

3

concat()Combines the text of two strings and returns a new string.

4

indexOf()Returns the index within the calling String object of the first occurrence of the specified value, or -1 if not found.

5

lastIndexOf()Returns the index within the calling String object of the last occurrence of the specified value, or -1 if not found.

6

localeCompare()Returns a number indicating whether a reference string comes before or after or is the same as the given string in sort order.

7

match()Used to match a regular expression against a string.

8

search()Executes the search for a match between a regular expression and a specified string.

9

slice()Extracts a section of a string and returns a new string.

10

split()Splits a String object into an array of strings by separating the string into substrings.

11

substr()Returns the characters in a string beginning at the specified location through the specified number of characters.

12

toLocaleLowerCase()The characters within a string are converted to lower case while respecting the current locale.

13

toLocaleUpperCase()The characters within a string are converted to upper case while respecting the current locale.

14

toLowerCase()Returns the calling string value converted to lower case.

15

toUpperCase()Returns the calling string value converted to uppercase.