Common JavaScript Manual/Data types - Strings
StringsEdit
Let's make variable with string as value, we will use string literal. Strings can be enclosed in double and single quotes but I advise using double quotes because in most laguages strings enclosed in double quotes and chars in single quotes.
str = "Hello, ";
ConcatenationEdit
We can concatenate strings using '+' operator. Let's add to string in variable 'a' string "world".
str = str + "world!"; // Hello, world!
Length of stringEdit
Any string variable have 'length' property that containg number of chars in string.
str.length; // 13
SplitEdit
String.split([separator]) - returns array of substrings that was obtained by separate String by separator. If separator not defined then separator = ","
names = "Artem,Michail,Nicholas,Alexander";
listOfNames = names.split(','); //Split string to list of strings
twoNames = names.split(',',2); //Split with limit
Getting substrings and charEdit
We can get substring from string using functions substr(index,len) and substring(indexA,indexB). First return substring from 'index' with length equal to len. Second return substring from indexA to indexB.
a = "Hello world!";
first = a.substr(0,5); //Hello
second = a.substring(6,11); // world
We can also get a char at some position using charAt function.
a = "Hello world!";
b = a.charAt(2); //e
Position of substringEdit
We can also get a position of some substring in string using indexOf and lastIndexOf functions. Both functions give two arguments but only first required it's a string that function search and second argument it's position from that function search string. indexOf return first position of substring and lastIndexOf return las position of substring. If substring not found both functions return -1
a = "Hello world";
b = a.indexOf('o',5); // 7
c = a.lastIndexOf('l'); // 9