- CoffeeScript - SQLite
- CoffeeScript - MongoDB
- CoffeeScript - jQuery
- CoffeeScript - Ajax
- CoffeeScript - Classes and Inheritance
- CoffeeScript - Regular Expressions
- CoffeeScript - Exception Handling
- CoffeeScript - Math
- CoffeeScript - Date
- CoffeeScript - Splat
- CoffeeScript - Ranges
- CoffeeScript - Objects
- CoffeeScript - Arrays
- CoffeeScript - Strings
- CoffeeScript - Functions
- CoffeeScript - Comprehensions
- CoffeeScript - Loops
- CoffeeScript - Conditionals
- CoffeeScript - Operators and Aliases
- CoffeeScript - Variables
- CoffeeScript - Data Types
- CoffeeScript - Syntax
- CoffeeScript - command-line utility
- CoffeeScript - Environment
- CoffeeScript - Overview
- CoffeeScript - Home
CoffeeScript Useful Resources
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
CoffeeScript - Strings
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
On compipng, 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
On compipng, 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 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
On compipng 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);
If you execute the above CoffeeScript code, it gives you the following output.
Hello Raju your age is 26
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 pne 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 allows multiple pnes 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
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.
Since we can use JavaScript pbraries in our CoffeeScript code, we can use all those methods in our CoffeeScript programs.
String Methods
Following is the pst of methods of the String object of JavaScript. Cpck on the name of these methods to get an example demonstrating their usage in CoffeeScript.
S.No. | Method & Description |
---|---|
1 | Returns the character at the specified index. |
2 | Returns a number indicating the Unicode value of the character at the given index. |
3 | Combines the text of two strings and returns a new string. |
4 | Returns the index within the calpng String object of the first occurrence of the specified value, or -1 if not found. |
5 | Returns the index within the calpng String object of the last occurrence of the specified value, or -1 if not found. |
6 | Returns a number indicating whether a reference string comes before or after or is the same as the given string in sort order. |
7 | Used to match a regular expression against a string. |
8 | Executes the search for a match between a regular expression and a specified string. |
9 | Extracts a section of a string and returns a new string. |
10 | Sppts a String object into an array of strings by separating the string into substrings. |
11 | Returns the characters in a string beginning at the specified location through the specified number of characters. |
12 | The characters within a string are converted to lower case while respecting the current locale. |
13 | The characters within a string are converted to upper case while respecting the current locale. |
14 | Returns the calpng string value converted to lower case. |
15 | Returns the calpng string value converted to uppercase. |