Strings



A string is a collection of letters (or characters, to be more precise). We can create a string literal by writing a group of characters inside quote marks like this:
"hello"


Strings Constructur Function

You can also create a string object using the following constructor function:
new String("hello") 


This will create a new string that is the same as the string literal "hello", although it will be classed as an object rather than a primitive value. For this reason it is preferable to use the string literal notation ... not to mention it requires less typing to use literals!

We can also use single quote marks if we prefer:
'hello'

If you want to use double quote marks inside a string literal, you need to use single quote marks to enclose the string. And if you want to use an apostrophe in your string, you need to employ double quote marks to enclose the string:
"It’s me" 

Another option is to do what’s called escaping the quotation mark. You place a backslash before the apostrophe so that it appears as an apostrophe inside the string instead of terminating the string:

'It\'s me' 

Escaping Characters

The backslash is used to escape special characters in strings such as:single quote marks \'double quote marks \"end of line \ncarriage return \rtab \t

If you want to actually write a backslash, you need to escape it with another backslash:
"This is a backslash \\"
<< "This is a backslash \"
  

1 Response to "Strings"