CoffeeScript-字符串



String对象使您可以处理一系列字符。与大多数编程语言一样,CoffeeScript中的字符串使用引号声明为-

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

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

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

  my_string = "Hello how are you";

  console.log(my_string);

}).call(this);

字符串串联

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

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

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

// 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);

如果执行上面的示例,则可以观察到串联的String,如下所示。

Hello how are you Welcome to Tutorialspoint

字符串插值

CoffeeScript还提供称为字符串插值的功能,以在字符串中包含变量。 CoffeeScript的此功能受Ruby语言启发。

字符串插值是使用双引号“” ,井号标签和一对花括号{}完成的。字符串用双引号声明,要插值的变量包装在花括号中,花括号带有一个哈希标记,如下所示。

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

在编译上面的示例时,它将生成以下JavaScript。在这里,您可以看到使用+将String插值转换为普通串联符号。

// 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代码,它将提供以下输出。

Hello Raju your age is 26

仅当字符串包含在双引号“”之间时,才对作为#{variable}传递的变量进行插值。使用单引号代替双引号会产生没有插值的行。考虑以下示例。

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

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

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

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

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

它生成以下输出。

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

JavaScript字符串对象

JavaScript的String对象使您可以处理一系列字符。该对象为您提供了许多在Stings上执行各种操作的方法。

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

字符串方法

以下是JavaScript的String对象的方法列表。单击这些方法的名称,以获取示例说明在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.

Made with ❤️ in Chengdu. Copyright reserved 2019-2022.

蜀ICP备20006366号-1