String in Javascript The String object is used for storing and manipulating text. A string simply stores a series of characters like "John Doe". A string can be any text inside quotes. You can use single or double quotes: Example: var carname="My Name is Khan"; You can access each character in a string with its position Example: var character=carname[7]; String Length <!DOCTYPE html> <html> <body> <script> var txt = "Hello World!"; document.write("<p>" + txt.length + "</p>"); var txt="ABCDEFGHIJKLMNOPQRSTUVWXYZ"; document.write("<p>" + txt.length + "</p>"); </script> </body> </html> Finding a String in a String The indexOf() method returns the position (as a number) of the first found occurrence of a specified text inside a string: Example var str="Hello world, welcome to the universe welcome."; var n=str.indexOf("welcome...
Comments
Post a Comment