var string_1 = 'Hello !'; var string_2 = "Hello !";
You can declare a string by this string constructor.
var str = String('Hello !'); >>> Hello !
When you want to quote any or some words inside a string quotation then you have to use escape notation ( \ )
|\’ \’||Quote any or some words inside a string quotation|
|\\||For using a backslash|
String Comparison (Lexicographic Order)
Lexicographical order essentially means “dictionary order”. In simpler terms, a < b if the first character of a comes before the first character of b in the ASCII table, or if the first characters of a and b are equal.
'alpha' < 'alpaq'; // false, ‘h’ is greater than ‘a’ 'alpha' > 'Alpha'; // true, ‘a’ is greater than ‘A’ 'alpha' == 'ALPHA'; // false, ‘a’ is greater than ‘A’ ['zeta', 'Zeta', 'ZETA', 1, '1', ''].sort(); // [ '', 1, '1', 'ZETA', 'Zeta', 'zeta' ]
var a = 'I am'; var b = 'Mahdi'; var c = a.concat(' ', b); >>> I am Mahdi (variable Name).substr(from where to start, how much character); c.substr(5, 4); >>> Mahd c.charAt(6); //showing character at position 6. >>> a a.toUpperCase(); // will convert every character to upper Case >>> I AM c.toLowerCase(); // will convert every character to lower Case >>> i am mahdi ' Mahdi '.trim(); // remove every unwanted space character from start and end. >>> Mahdi 'My name is Mahdi'.split('by which character'); // slit this string by that character. >>> ‘My’, ‘name’, ‘is’, ‘Mahdi’
This method is used to see if your desired string starts with a specific character or character set. It always returns a Boolean value true or false:
var name = 'My name is Mahdi'; console.log(name.startsWith('name'));
This method is used to see if your desired string ends with a specific character or character set. It always returns a Boolean value true or false:
var name = 'My name is Mahdi'; console.log(name.endsWith('Mahdi'));
This method is used to see if your desired string has a specific character or character set. It always returns a Boolean value true or false:
var name = 'My name is Mahdi'; console.log(name.includes('Thomas'));
This method is used to repeat a string multiple times:
var rpt = 'Hello World '; console.log(rpt.repeat(4));
Hello World Hello World Hello World Hello World
Length of a String
var ln = 'Hello World'; console.log(Ln.length);
Strings are also important for working with APIs and parsing data from external sources, such as JSON data. Many APIs return data in the form of strings, and you may need to manipulate the string data to extract the information you need.
In addition, strings can be used to represent complex data structures, such as arrays and objects, in a compact and easy-to-read format using JSON notation. This makes it easy to exchange data between web applications and APIs.