I am dividing Strings in three parts.
- Part 1. This is Part 1
- Part 2
- Part 3
This section has the details of following topics:
| constructor | length | prototype |
| charAt | concat | indexOf |
In simple terms, Strings is a collection of characters. We can create an object of String class and perform many functions on it.
Syntax for creating a new String object and assigning value to it.
var variableName = new String(value);
String Constructor: String()
It is used to create an object of String type. We can also assign an initial value to that String object.
Below is an example of creating a String object, assigning a string value to it using a constructor and fetching that value.
var str=new String("Hello");
console.log(str.valueOf());
Below is the output of above code

length
Returns the length of the string. Counts the white spaces as well. This will return the number.
Syntax
variable.Length
Below is a sample code
var str = new String("Hello World!!");
console.log("Length of String: " + str.length);
Below is the output of above code

charAt()
This method will return the character at a specified index. The index starts with 0(zero) and the index of the last character is string.length – 1. Index which is out of range returns nothing, not even undefined.
Syntax
variable.charAt(index);
Below is a sample code
var str = new String("Hello World!!");
console.log("1st Character: " + str.charAt(0));
console.log("3rd character: " + str.charAt(2));
console.log("6th Character, which is space: " + str.charAt(5));
console.log("7th character: " + str.charAt(6));
console.log("Index which is out of range: " + str.charAt(13));
Below is the output of above code

concat()
This method merges two or more strings into a one single string.
Syntax
string1.concat(string2, string3, ....);
Below is a sample code
var var1:string = "Hello ";
var var2:string = "Friends!! ";
var var3:string = "Good Morning.";
var result = var1.concat(var2);
var result1 = var1.concat(var2,var3);
console.log("var1 + var2: " + result);
console.log("var1 + var2 + var3: " + result1);
Below is the output of above code

indexOf
This method returns the index of a specified value within a string. Search will start from the starting of the string. variable is the string in which we are searching for the value. searchString is the value which we are searching in the variable. fromIndex is an integer which specify the starting index for the search. It will return -1 if the string is not found.
Syntax
variable.indexOf(searchString, [fromIndex])
Below is an example
var str1 = new String("This is a sample string sample");
console.log("Index of i: " + str1.indexOf('i'));
console.log("Index of s: " + str1.indexOf('s', 8));
console.log("Index of is: " + str1.indexOf("is"));
console.log("Index of sample: " + str1.indexOf("sample"));
console.log("Index of sample: " + str1.indexOf("sample", 14));
