Coffee Script Strings - CoffeeScript

What are Strings in CoffeeScript?

The String object allows you to work with a sequence of characters. As in maximum of the programming languages, the Strings in CoffeeScript are acknowledged using quotes as –

On compiling, it will generate the succeeding JavaScript code.

String Concatenation

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

On compiling, it will generate the following JavaScript code.

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

String Interpolation

CoffeeScript also delivers a feature recognized as String interpolation to comprise variables in stings. This feature of CoffeeScript was stimulated from Ruby language.

String interpolation was completed using the double quotes "", a hash tag # and a pair of curly braces { }. The String is stated 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 underneath.

On compiling the above instance, it makes the succeeding JavaScript. Here you can witness the String interpolation is rehabilitated into normal concatenation using the + symbol.

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

The variable that is accepted as # {variable} is interpolated best if the string is enclosed between double charges " ". The use of single quotes ' ' in place of double quotes produces the line as it's far without interpolation. Consider the subsequent instance.

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

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

It generates the following output.

JavaScript String object

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

For the reason that we are able to use JavaScript libraries in our CoffeeScript code, we can use all those methods in our CoffeeScript packages.

String methods

Following is the listing of techniques of the String item of JavaScript. Click on the name of those strategies to get an instance demonstrating their usage in CoffeeScript.

S.No.

Method & Description

1

chart()

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

locale Compare()

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.

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

CoffeeScript Topics