The JavaScript Programming Language

Strings

String is a sequence of 16-bit values e.g. sequence of unicode charcters. The length of a string is 16-bit number (216  =  65 536).

In ECMAScript 3 strings must be written in a sigle line. ECMAScript 5 allows writing multiline strings.

//multiline with \
x = 'First column\
    second column\
    third column';

//newline \n
y = 'first row\nsecond row';

 

Example: 030string_multiline.js

 

 

Concatenate strings

Use + operator to concatenate two or more strings.

Example: 032string_concatenate.js

Example2: 032string_concatenate2.js

 

If you try to use - operator NaN will be returned.

Example: 033string_minus.js

 

 

String length property

Access the string length with 'some string'.length .

//get the string length

var x, len;

x = 'Some string!'
len = x.length;

console.log(typeof x); //string
console.log(typeof len); //number
console.log(len); //12

 

Example: 031string_length.js

 

 

 String methods

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String 

1. str.charAt(index) returns the specified character from a string        035stringmethods_charAt.js

2. str.concate(str1, str2, ...) concate str1, str2 into str. Equivalent to str+str1+str2      035stringmethods_concate.js

3. str.indexOf(str1)  returns the index (position) of str1 inside str. If str1 is not found inside str returns -1.     035stringmethods_indexOf.js

4. str.match()    035stringmethods_match.js

str.match(/regex/) returns first match of the string

str.match(/regex/i) returns first match of the string (not case sensitive)

str.match(/regex/g) returns all matches inside string

//usage of regex memory e.g. string inside ()

var str, x;

str = "SOME simple Sample string";

//regex memory is inside (). Memory is returned as array element
x = str.match(/sim(\w+)/i); //case sensitive


console.log(x); // [ 'simple', 'ple', index: 5, input: 'SOME simple Sample string' ]

console.log(x[1]); // $1 = ple

 

 5. str.replace(regexp|substr, replacement)  replaces one or all (/g) matches with replacement      035stringmethods_replace.js

 var str, x, xg, xi;

str = "simple example of simple string BIG big";

x = str.replace('simple', 'not simple'); //just first matche
xg = str.replace(/s\w+e/g, 'not simple'); //all matches
xi = str.replace(/BiG/i, 'SmaLL'); //not case sensitive


console.log(x); // not simple example of simple string BIG big
console.log(xg); // not simple example of not simple string BIG big
console.log(xi); // simple example of simple string SmaLL big

 

 

6. str.search(regex)  returns position of regex inside str. Simmilar as indexOf but indexOf don't use regexs.    035stringmethods_search.js

 

7. str.split(separator) returns array of separated strings    035stringmethods_split.js

 

8. str.toLowerCase()    str.toUpperCase()    035stringmethods_toUpperCase.js

 

9. num.toString()   converts number into string     035stringmethods_toString.js

 

10. str.trim()   removes spaces from left end right     035stringmethods_trim.js

 

 

 Regular expressions

 To test regular expression use test method. The method returns true if match exist or false if not.

regex.test(str)        036string_regexTest.js