C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
JavaScript StringThe JavaScript string is an object that represents a sequence of characters. There are 2 ways to create string in JavaScript
1) By string literalThe string literal is created using double quotes. The syntax of creating string using string literal is given below: var stringname="string value"; Let's see the simple example of creating string literal. <script> var str="This is string literal"; document.write(str); </script> Output: This is string literal 2) By string object (using new keyword)The syntax of creating string object using new keyword is given below: var stringname=new String("string literal"); Here, new keyword is used to create instance of string. Let's see the example of creating string in JavaScript by new keyword. <script> var stringname=new String("hello javascript string"); document.write(stringname); </script> Output: hello javascript string JavaScript String MethodsLet's see the list of JavaScript string methods with examples.
1) JavaScript String charAt(index) MethodThe JavaScript String charAt() method returns the character at the given index. <script> var str="javascript"; document.write(str.charAt(2)); </script> Output: v 2) JavaScript String concat(str) MethodThe JavaScript String concat(str) method concatenates or joins two strings. <script> var s1="javascript "; var s2="concat example"; var s3=s1.concat(s2); document.write(s3); </script> Output: javascript concat example 3) JavaScript String indexOf(str) MethodThe JavaScript String indexOf(str) method returns the index position of the given string. <script> var s1="javascript from TheDeveloperBlog indexof"; var n=s1.indexOf("from"); document.write(n); </script> Output: 11 4) JavaScript String lastIndexOf(str) MethodThe JavaScript String lastIndexOf(str) method returns the last index position of the given string. <script> var s1="javascript from TheDeveloperBlog indexof"; var n=s1.lastIndexOf("java"); document.write(n); </script> Output: 16 5) JavaScript String toLowerCase() MethodThe JavaScript String toLowerCase() method returns the given string in lowercase letters. <script> var s1="JavaScript toLowerCase Example"; var s2=s1.toLowerCase(); document.write(s2); </script> Output: javascript tolowercase example 6) JavaScript String toUpperCase() MethodThe JavaScript String toUpperCase() method returns the given string in uppercase letters. <script> var s1="JavaScript toUpperCase Example"; var s2=s1.toUpperCase(); document.write(s2); </script> Output: JAVASCRIPT TOUPPERCASE EXAMPLE 7) JavaScript String slice(beginIndex, endIndex) MethodThe JavaScript String slice(beginIndex, endIndex) method returns the parts of string from given beginIndex to endIndex. In slice() method, beginIndex is inclusive and endIndex is exclusive. <script> var s1="abcdefgh"; var s2=s1.slice(2,5); document.write(s2); </script> Output: cde 8) JavaScript String trim() MethodThe JavaScript String trim() method removes leading and trailing whitespaces from the string. <script> var s1=" javascript trim "; var s2=s1.trim(); document.write(s2); </script> Output: javascript trim 9) JavaScript String split() Method<script> var str="This is JavaTpoint website"; document.write(str.split(" ")); //splits the given string. </script>
Next TopicJavaScript Date
|