STRINGS || STRING OBJECTS || STRING METHODS IN JAVA SCRIPT

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] hello friends welcome back to our channel so in today's session we will discuss about strings in javascript so first we will go with the strings so how to represent a string and how to create a string and then how to create the string object so we have already seen the number object right similarly we can create a string object and then we'll go with the string methods so there are several methods available in string okay so we'll go with all those methods right now coming to the strings so a string is what the number of characters any number of characters so enclosed in the quotations that can be represented as a string so we can go with the numeric strings or non-numeric strings that means the string may also include the digits okay it may include the digits alphabets or any special symbols but it should be enclosed in quotations either it may be a double quotations or a single quotations for example some s is equal to welcome this is not a string so if you enclose this in between a double quotes then it will be a string similarly s is equal to 10 it is a number and if this 10 is enclosed in a double course then it will be a string so not only the double quotes we can also enclosed in single quotation we can also enclosed in single quotation so anything is problem no problem okay so whatever the thing we are enclosing in between the quotations that we will call it as a strings and how to create a string object so how to create this thing simply use the declaration keyword that is that some s is equal to here use this one so welcome so this is the string which is available in yes variable yes right so this is how we can create a string now how to create a string object so similar to our number object the string object can be created with the help of new keyword new keyword so if you use the same thing where some s is equal to nu of string of welcome so the value is same here you can also the value is same but here yes is an object so in this case yes is a string so that's the difference by using the new keyword we can create a string object we can create a string object and also we are having in these strings we are having a lot of methods string methods built-in methods so we can use all those methods in order to do some operations on these things okay right so we'll say the string methods one by one and before going to the string method there is a property for this thing called the length property so there is a property called length property in order to find the string length we can go with s dot length yes dot length so it will return the length of the string length of string in yes so here yes dot means length of string yes will be later okay so it will count all the characters and it will return so here the count is 7 so the result for this one is seven okay so this is the length property length property in order to find the length of screen now we'll go with methods one by one so i will show you the syntax of these methods and i will demonstrate each and every method by executing a small html code right so now i will show you the syntaxes and then we'll move on with the code okay first we'll go with the methods we'll go with the methods the first one is index of index of so here one is a capital letter o will be the capital letter index of so here the syntax is string dot index of here you have to pass the string or a character so whatever it may be so it can pass anything so it will find and it will return it will return the index it will return the index of the first occurrence of this thing or a character so here you can pass either character or a substring so it will search for this substring or a character in this string okay so if it is available it will return the index value from where it was started if if the string is not available in here automatically in the false condition that means if if this thing is not available it will return minus one if it is a true that means if the string or a character is available it will return the index of that particular character or substring otherwise it will return a minus 1 otherwise it will return minus 1. okay and second one last index of last index of so here e i and o are characters i and o are capitals so here also the same syntax so string dot last index of same string so we have to pass any substring or any character okay here also the same thing it will return the index so here it is a first occurrence okay here it is a first occurrence and here it is a last occurrence last occurrence okay so here the same thing but in this case it will return the first occurrence in this case it will return the last occurrence as the name is last index off right and here if if the string is not available it will return minus 1 and here also if the string is not available it will return minus 1 right and the next or third one is we can give the second parameter for this index of so index of our last thing itself also we can give the second parameter string dot index of here you can give the string or character and here you can give the [Applause] starting index start index so that means from where we have to check okay from there we have to search right from there we have to search from this position it will search for the first occurrence of this character so it will return the index of first occurrence first occurrence okay and if it is a false again the same thing that means false means if this string is not available in from this index value it will return -1 the same thing the same thing is for last index of also so last index of also will be taking the second argument as a start index from where we have to search right so if it is available automatically it will return index of the last occurrence okay so this is the thing and the next one search search okay so here the search function the syntax is string dot search of string or character a string or a character okay string or character and here also we get the index of first occurrence first occurrence so then what is the difference between the search and index of so one difference is here we can give the second argument but for the search function we can't give the second argument right so in this case index off and last induction we are we can give the second argument as a starting x that means we can reduce the space such space but that cannot be done in search function so it will search from the beginning the starting of the string it will search for this from the starting of the the next one is slice slice so that means we can extract some portion of the string okay we can extract some portion of the string so hope you understood these four functions index of last index of and the search function right the next one is slice the syntax is string dot slice of start index stop index so we need to give the index position start and stop so that it will extract the values from starting index to stop index so this start index will be included in the extraction and this top index will be excluded so we should not include that one so for example if you go with the slice 0 comma 11 it will start executing that will start extracting the proportions from the index values 0 to 10 because 0 will be included and stop in stop index will not be included so up to 10 will get okay so this will be slicing operation and here one more advantage we can also give the negative indexes negative indexes for example if you consider the string as welcome okay so the index value is 0 1 2 3 4 5 and 6 okay so this is the positive index this is the first two index and we here we can have the negative index the last element the index value of the last element will be minus 1 here minus 2 minus 3 minus 4 minus 5 minus 6 and minus 7 right so this is the negative index so in the slicing operations we can give the positive index or the negative index okay we can also give the positive index as well as the negative index so negative index means it will start counting from last element to the first end if it is a positive index from the first element to the last segment that we have to remember right yeah then so this is the slice operation and the next one is substring substring is a single word okay there is no capital letter here so string dot sub string and again here we are supposed to give the start index stop index start indexing stop index so here also the same thing we'll get the portions uh i mean the in the characters from the index values from starting index to stop index here also it will be included it will be excluded right but what's the difference between the substring and the slice so here we should not give negative index negative index is not acceptable negative index is not acceptable where slice operation will accept the negative index slice operation will accept the negative index but here it doesn't accept the negative units that is the difference between the slice and this substring right and there is one more function to extract the portion that is sub str sub str so here the syntax is string dot sub str and here we have to give the start index the first parameter is the start index and the second parameter is length up to where we have to up to how many i mean how many characters we need to extract so here we have to give the length of characters to extract so from this index this many characters will be extracted right so the first slice means start index and stop index and it will be included it will be excluded and here the native index will also be accepted negative index will be accepted but here the same thing here native index will not be accepted and here the start index and the length of the characters to be extracted here we need to give the length of the characters how many characters we need to extend right so these are the three functions for extracting the portion of a string and there is a one more uh the next method is a replace method so replace method the name itself indicates it will replace the whole scene with the new string so the syntax will be a string dot a replays of old string new string okay old string and news here also it will replace only one occurrence only first occurrence okay it will replace only first occurrence it doesn't replace all the currencies it just replace okay so we need to replace the world's thing to bluestem so it will search from the first character so whenever it finds this old state automatically that old string will be replaced with a new string right so this is a replace function and there are a few more functions available so in order to convert from lowercase to uppercase and uppercase to lowercase so we are having a functions called two uppercase to lowercase 2 uppercase function [Music] so the syntax is string dot to upper case okay automatically whatever the string available in in this string the all the lowercase characters will be converted to uppercase similarly we are having the one more function called two lower case so don't worry i will execute each and every function that will show you okay so i am just giving the syntax here string dot two lowercase so automatically whatever the characters uppercase characters available in this string that will be converted into lowercase and there is a one more function called concat means concatenation so it replaces the plus operator so generally plus will be used as a concatenation between two strings that can be replaced by using this concatenation so string one dot concat string two automatically whatever the string available in string two that will be concatenated to string one so one thing i need to say here so whatever the thing we are creating everything will be creates a new string okay the original string will not be affected so if you convert some lower case to uppercase the original string will not be converted right say new string will be created similarly concatenation also a new string will be created the word string will not be updated right so a new string will be created by applying these functions right next one trim function trim function it removes all the leading and tiny spaces so it removes the spaces and the syntax is string dot for example we are having some spaces three spaces and we are writing some javascript and three spaces here and if you apply this trim function it will remove all these spaces all these pieces leading spaces and tiling spaces but not the spaces in between the string okay it doesn't remove the spaces in between the string it only removes the leading spaces and taking spaces so the result will be directly so without the leading spaces we'll get the result so this space is common okay this space is common it will remove the leading spaces and tiling spaces so this will be the result for trim function it will remove the spaces then so how to extract the characters from the position okay extracting the characters from the position so for that we are having the function called caret function so care it means character it so string dot care act and here we need to give the position or index whatever it may be so whatever the character available in that position that will be written okay it will return the character at position character at given position okay so we need to give the position so if the position is beyond the length of the string nothing will be printed so i will show everything okay and there is one more function called cable code at so here c and a will be captive cap coded so it will return the unicode of the given character okay first it will it will uh get the character for that position and it will return the unicode for that particular character so string dot care code at position it will return unicode so generally we are having a different uh format that is ascii we know that ascii so this is a unicode so it will return the unicode of the character which is available in this position okay code for the character available in this position right so these two by using these two we can extract the character that means we can get the character or the code at the given position right the next one is accessing so how can we access the string characters so this accessing can be done with the help of index value okay in between the subscript in between the subscript so index value always starts with starts from zero right for example s is equal to some welcome so index value is 0 so this can be represented as s of 0 and this can be represented as s of 1 and this can be represented as s of 2 and this can be represented as s of 3 and so on right so each and every character will be having some index value and we need to access the characters with the help of index given in subscript given index in substrate so by using this we can access the characters so hope you understood this one so i have given the complete information about the strings how to create a string how to create the string object and how what are the different methods the syntax for different methods available in the strings in javascript now we will execute each and every method so we i will show you how to create a string how to create a string object and how to execute all these string methods in the program by executing a small program right so that if you are having any doubts those doubts will be clarified so let's move on to the system hello friends so just now we have seen about the strings introduction and then string object and string methods now i will demonstrate each and every method of the string so by executing a small html code we execute small html code so begin with the html tag so in between the html tag will create a body and in the body we will write the content right yeah so we'll put the heading first of this topic that is strings so this is all about our strings and now uh see string strings and then string object string methods right so this is our concept now so inside this will write the scripting right so strings.html i'm just saving it so inside here we can write the script so inside the script will create one string so string can be declared by using the ad s1 is equal to see here we can write here in the double quotations so string is a number of zero or more characters including the quotations so either it can be double quotations or a single quotations so i am writing welcome to strings in javascript right so this is the string we have declared and now we'll display it so for display we'll go with the document dot right ln s1 okay so let us execute this one so i am saving this one so this is the file and if you double click on the strings you will get the file see so heading this one is a heading and this is a text right so welcome to strings in javascript that one that's what we have written here now usage of double quotations and single quotations so even ah string can be used in a single quotation so see where s2 is equal to instead of single quotations and double quotations this is given by single quotations string with single quote so again i will display the same thing document dot write ln see single quoted string s2 right so i will do the same thing here for better understanding right so instead of single here it's a double quoted you can observe here see double quoted string single quoted string just a second so here we can break room we can give the break row so automatically it was displayed here right so double quoted string and single quoted string this was right now if you want to insert any double quotations or single quotations inside the string so we need to give here so inside the single quotations see inside the single quotations we can give the double quotations see i will write here with double quotations so i will write here i will keep this one is a double quotations and let us execute here see if you observe here so it includes the double quotations okay the double quotations will are enclosed in double quotations right hope you understood so inside the single quotations we can use the double quotation similarly inside the single double quotations we can use the single quotations right see i will show you javascript i will keep this javascript in a single quotation you can observe here so you can observe in the output we will get the single quotation right so that that is the usage of double quotations and single quotations so a string is a zero or a more number of characters enclosed in a quotations either it may be a single quotations or it may be a double quotations right then and the next one see we can have the string object how to create a string object so this is a normal string we can have the type of a thing see document dot ln so instead of this one i will give type of s 2 s 2 ok you can observe sorry yeah you can observe existing see you can observe here string okay you can observe here string right and if you create a string object so where we can create a string object s3 is equal to we have to use a new keyword and followed by string and pass the string here so this is string object right now document dot write ln s3 string object s3 right so here you can go with the br so you can observe here string object so this is string object and now you can check with the data type so here you can observe type of s3 type of s3 you can observe here type of s3 is an object so whatever operators we have seen in the numbers that means double is equal to triple is equal to the same can be applied in the strings also right so if you are using double is equal to we'll get the i mean the values will be compared and if you are using triple is equal to the types and values will be combined the same thing here and this is how we can create the objects and strings now we'll see the methods so we'll see the methods so what are the different methods so i will give you so document dot i will write the heading here right ellen instead of i mean if you want to write heading in the script we have to put in the document.writer right so h1 string methods so you can observe here string methods right now we will see one by one so what are the string methods the first one is index of index of right so index of means so we have to pass one argument and we'll get the string so we'll i i will uh so we are having some s1 s2 and s3 so we will see that document dot right ln so here we will see what is a function index of okay so i will write here index o capital of index of function right now the syntax is what's the syntax so string dot that means s1 dot so i just want to uh identify the index of some character or a string of s1 so i have to write s1 dot if you want to find out the index of any character in the s2 we have to write s2 dot so like this s1 dot index of give the string here so either it may be a character or a string so i will go with some o so it will give the first occurrence okay it will give the first occurrence will see index of index office 4 that means index of o so here we are checking o what's the in the s1 what's the s1 here so you can observe s1 is a welcome to string in javascript so in that we are finding the index of o that means this one like what is the value the index r starts from 0 itself 0 1 2 3 and 4 so we got the result as 4 and if you are giving any string which is not available so i am giving gel that is not available in s1 right that is not available in s one so we will get c minus one it will return as a minus one right so first let me uh give the i i will print the s one and s two okay yes one comma so just for representation i am giving s1 comma s1 i will go with the br next s2 is equal to s2 and also i will display the s3 also okay and we'll go with the br br means break row okay yes 3 is equal to s3 and also we'll go with the br because for every time we are supposed to print the output in the next line now you can observe s1 s2 s3 okay so here we are getting we are finding some jet in s1 okay position of z in s1 so index of function position of z in s1 so you can observe here right at the next uh see the index of position so always here you can observe we have to pass one string or any character so that it will display the index of that particular character or a string and there is one more function called last index of so here this is in from the first occurrence okay so if you go with the o so that's the first occurrence position of o in s1 so you can find so you can find here s1 it is having one two so two rows right so but here we are getting the index value as 4 that means is the first occurrence if you want to get the last occurrence we are having with the same function yeah there is a one more function called last index of last index of so in the last index of i will be the capital i will be the capital and y will be the capital now position of o in s1 is the last index okay now we'll check the result so you're getting some value as 9 you can observe here 0 1 2 3 4 5 6 7 0 1 2 3 4 5 6 7 8 and 9 9 1 so position of o in s1 last index of this is the last index of it's not index of last index of o is nine right so hope you understood from the starting position if you want to check the index we will go with the index of that means the first occurrence and if it is the last index of function it will search for the position of given substring from the last okay from the last last occurrence that means the last occurrence first occurrence and last sequence and here we can also give a parameter the second argument that is a starting that means where we have to start from where we have to start index of with one argument with one argument so i just want to find the value of y index of o and i need to search from the position some six onwards okay from six onwards see index off with one argument so i have given from sixth so sixth position from the sixth position it will search for the word as a first occurrence so zero one two three four five six from here onwards it will search for the first occurrence of oh so you can observe here the position is mine position is nine okay if you if the index i mean if the value is not available automatically it will return minus one so you can see so you instead of go if you check for z if you check for jet will get minus one okay so hope you understood this one index of and the last industry now there is one more function called set so we can search whether the string is available here or not so next one is a search search function search here i will search o in the given s1 see 4 4 okay so here search will also give the same thing okay the index position from where it has been started but the only difference is in search function we should not give the arguments now we should not give the second argument okay so in index of we are giving the second argument that means where we have from where we have to start the search so here in the search function we should not give the second argument we are we are not supposed to go with the second argument that's the only difference between search and index of and then slicing so extracting some portions of the string so then we'll go with the slicing so first first method is a slice method slice method okay extracting from s1 from s1 right so here you can go with s1 dot slice so here in slice we need to give the start index and stop index okay so i will give some 0th index to 7th index so 7 will not be included 7 will not be included so you can observe here see so extracting from s1 slice so from 0 to 7 0th index is here 0 1 2 3 4 5 6. so it should not include the 7 it will go with the 6 itself right and see we can if you are not giving the second argument in this slice will get the complete string okay it's an optional the second argument that is a stop index is an optional if you are not giving the stop index automatically it will start extracting from the given index value to the end of the string so for example if you go with seven it will start extracting from the seventh index so you can observe here from seventh index it start executing and if you go with some 17 it will start extracting from the 17th index value so this is the 17th okay so like this this is an optional thing so if you go with the 0 to 17 so you'll get up to here okay up to here 0 to 17 so this is the 0 to 17 right and here uh we can also give them negative indexes so if it is a positive index it will start from the left to right if you give the negative index it will start with the right to left okay see i will go you i i will explain you this with negative index with a negative index so i will go with the negative index right so minus 1 so here the minus 1 is the index of last one minus 1 is the index of last one so we will go with some 5 minus 4 2 sorry minus 70 into minus 5 okay you can observe here minus 17 to minus 5 so minus 17 to minus 5 means so you can observe here so this is the this one so always it will start from the negative index will start from left okay left onward sorry right right most the last index value the index value for the last element is minus 1 okay the index value of the last element will be the minus 1 you can observe here 0 to minus 5 that means up to here it will get okay so here minus last minus 1 minus 2 minus 3 minus 4 minus 5 okay it doesn't equal to the minus 1 it doesn't include the minus 5. hope you understood this one right negative index and the next one is substring substring so similar to the slice function we are having a one more function called substring see substring is a single term so we should not give any capital letter s one dot substring so here here also the same thing so we will give some starting point and ending point so it's trying to from welcome to string so that is 0 to 17 index so what's the difference between substring and slice so slice will accept the negative index but substring will not accept the negative index that's the only thing and then coming to that there is one more thing called sub str sub str so what's the difference here instead of giving the substring so some str so it will start the first one is the starting position and the second one is the length of the string you need to extract for example if you go with the seven seven characters it will extract the seven characters you can observe here seven characters zero one two three four five six seven character if you want to get the ten characters you will go with here ten so that will get the 10 characters right so that's the thing and here also it will accept the negative index sub str will also accept the negative index right then there is a one more function that is a replace function replace function so replace function will take the two arguments one is the old world string and the next one is the new string okay so so not extracting replacing see replacing s1 dot i will go with the replace s1 welcome to strings in javascript so i will give the javascript right so i i will give the world string and new string is js okay javascript is replaced with gs now you can observe here s1 dot request right now you got it welcome to strings in js so one thing we need to remember whatever the thing we are doing it will create a new string but the original stream will not be modified here the original string will not be modified right so replace means world string command new string so it will replace only the first occurrence if our string is having a more number of occurrences it will replace only the first occurrence right there's a replace function and there is there are a few more functions called a lower case and upper case so i will give you from there s4 is equal to i will take some see welcome to javascript all are in small letters right so i will do here document dot sorry i will take one more variable s5 is equal to s4 dot to upper case so this is a function to uppercase it will convert everything in a uppercase character now you can go with the document.rightln s5 okay comma vr because so for every function we are giving in the display see you can observe welcome to javascript this is a upper case now we know that s5 is having the upper case now i will show you one more function so document dot write ln s5 dot to lower case so all the lowercase characters will be in all the uppercase character will be converted to lowercase characters so this these are the two functions two upper case two lower case so u and c is capital here l and c are capital here right and then we are also having the concatenation okay we are also having the concatenation function will be which will be concatenate two different strings that means this is a replation with replacing of a plus operator plus operator right so where six is equal to i will take directly i will give the string so hello so now i will concatenate document dot write ln concat of yes 6 comma yes 4 so that both will be concatenated both will be concatenated right see where we have to concatenate sorry so here yes s6 dot it should be concatenated you can observe hello welcome to javascript right so here i will give the brick line yeah so hello welcome to javascript or simply you can go with the remove this one so you'll get the same thing s6 dot concatenate s4 so that s6 dot concatenate whatever the string we are having in s6 it will be concatenated with a support s4 so that's why we are getting some hello welcome to javascript right so hope you understood this one and there is a one more function called trim function trim so see where yes 7 is equal to i will give three spaces and i will give some javascript okay again three spaces and close now so you can observe here the length of the thing the length in order to find the length of any string we are having the length property so directly we can go with the length of property document dot right ln here you can go with s7 dot length directly we get the property right see here we need to give the break row so that so you can get the length is 17 okay you can here you can go with the data length of s7 is yeah length of s7 is 17 now that means you can observe here so 4 5 6 7 8 9 10 11 and here 3 spaces and 3 s here 3 spaces so 17 so now i will use the function called stream okay i will use the function called trim so where s 8 is equal to s7 dot trim function so that it will trim the value so now we will check the right ln and i will i will take the everything here yeah so i will go the same thing here also now we will check we can observe yes because we have applied the trim function so trim function will remove all the spaces okay leading in trailing spaces it will remove all the leading and trailing spaces you can observe here we got a 7 s8 here this is not a 7th right s8 is 11 because it removes the spaces only leading and trailing but not in between the string okay so we are having some three spaces leading spaces three and triangle spaces three six and in between the string we are having one space total seven but this trim function only removes the leading and tiling spaces okay so that's why we got one 11 that means it removes the leading three spaces and tiling three spaces total six spaces has been removed so that we will get the result as 11 right so hope you understood this one it removes the leading and trialling spaces now there are there are two more functions there is a cat at care at function function means here we need to give the position so that it will give the uh character at that particular position so we'll go with the right document.writeln so here careful see now we will go with the s 1 or s 8 dot sorry we'll go with the s 1 dot okay careful yet of what is a yes one data so welcome to strings in javascript so we'll go with the [Music] whoa okay some four four out of five okay we'll give anything no problem see care at five is m so you can observe here 0 1 2 3 4 5 the position 5 position 5 the character is m now we can also know that length of this one so we will check the length okay we'll check the length and we'll give the position beyond that length okay so document.writeln s1 dot length what's s1 dot length 36 now you give the value 38 what's the result here observe empty okay it will not return anything it doesn't return anything if the uh position goes beyond with the length right so if you go with the length three we will get the result as the third character c right now there is a one more function called care coded care coded so these things will be saved in a different format that is a unicode format so normally we know all about ascii format but here it will store in a unicode so it will give the unicode value of that particular position right so i will i will take the same thing and will check the unicode of this one okay can put it c a capital okay cad coded of this one so you can get the car code of c is 99 okay car code at off c is 99 and if you go with some eight you can get the car code of va a position 8 what's the value at position 8 you can check the position 8 it is t so t value will be 1 1 6 this is a unicode value okay this is the uni program and now there is one more thing so how can we access the characters of a string so directly we can go with the index values so we can use the subscript and index values so that we can access the characters right so i will show you that for we will use one loop i is equal to zero i less than some s one dot length or we can go with the s s eight okay s eight dot length i plus plus and go here so document dot write ln and take s8 of i give the index value in the subscript okay plus so each line will go with each line okay so it will print each character in a single line so you can observe here javascript okay so this is how we can access each and every character by using the subscript followed by the index value subscript followed by the index value right so hope you understood this one the string so what are the strings and how can we use this double quotations and single quotations and then how to create a string object and then different types of string methods so index of last index and search slice substring server str replace two uppercase two lowercase and the concatenation length property carried care coded and how can we access the characters of a string right so let's stop here and if you have if you are having any doubts regarding the strings concept feel free to post your doubts in the comment section definitely i will try to clarify all your doubts and if you really understood my session like my session share my session with your friends and don't forget to subscribe to our channel thanks for watching thank you very much
Info
Channel: Sundeep Saradhi Kanthety
Views: 4,388
Rating: undefined out of 5
Keywords: sundeep, saradhi, kanthety, html, introduction, basics, markup language, static, webpages, web browser, text editor, extention, .html, .htm, java scripting, dynamic, front pages, user interface, tags, html elements, java script, applications of java script, script tag, head tag, body tag, javascript file, scripting language, strings, string object, string methods, charat, charcodeat, index, position, start index, stop index, replace, trim, search, accessing, concat, functions, indexof, lastindexof
Id: tXYnjmLr-5g
Channel Id: undefined
Length: 58min 19sec (3499 seconds)
Published: Fri Jan 15 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.