ServiceNow JavaScript tutorials String functions | ServiceNow javascript Examples on string function
[Music] hi all and welcome back so in today's tutorial we'll be looking on various method with respect to string in javascript we have already covered a series where we have defined different methods on array so today we'll be focusing on string so for the demo purpose we'll be using explore as i editor and you can use this method across your service now experience right across your requirement so this will be the editor for today's two so let's first understand what is string in javascript so strings are text that helps in holding data that can be represented okay so a javascript string stores a series of characters like um my name is amit okay a string can be any text inside double courses in this code for example if i write variable or variable or i can give str equal to [Music] named as amit okay now this turns out to be a string now let's now we will be playing around this string and we will explore various method with respect to string they which are present in javascript so let's go let's start with the first method which is character add okay this particular method returns the character and this pretty specified index string in javascript has zero based indexing okay so for example we will consider this string now if i want to confirm i want to get if i give cs dot info okay i want to check character at index 1 or character index 5 so i can simply go str dot actor at that and i can simply give the index on which i have to check so let's get it start with zero and we will run it out so we can see on zeroth index we have m okay i will zoom out somewhere so on zero index we have m in the similar way if i put five then it will return small m so this is how it goes now let's move to the next method which is character coded so what is the difference between character and character coded the character coded gives these methods returns a number that represent the unicode value of the character at that specified index so for example we have given area five okay so what is the unicode value of five okay it will give that value okay so we will simply modify this particular function only to get characters um it's character code right and if i run this out now let me do the change okay i will just for the comparison purpose i will do control that and i will paste it over here so you can see the difference between both if i run this out now you can see you will be seeing see the character at fifth index is m and the unicode value for m is one zero nine so that's how it works now moving on to the third method which is concave so this method combines combines the text of two strings and it has a new combined or joint string you can see to concat those t we have to use this concat function okay so let's define one more string i will give variable cr2 and i will give it a name i will give it a single quote so that that particular confusion will also get start my channel name is technoma done and now we'll be using a function a method called concat so how we can do that we have to do gs dot info now we want to config contact this to stream so you can simply do a cr str dot so first string dot concat and then we can give shrink second str 2. will end and if i run this out now so it should give the combine string so you can see my name is amit and my chain name is technoma state gives us the combine string now moving on to the next gene okay which is end wave so what end with function do this method checks whether a string ends with a specified string or character okay so for it and it returns a boolean value so for example my first string ends with harmon okay so what we will do we will simply give gs dot info i am i will start adding at the top most of the string method you must be not aware about so you you can go through it so ends with sorry gs dot info and now we have to check for string str str dot and right now we will be checking first i will just give some random string called for example let's say xyz does it ends with xyz if i run this out so it should return false because it is it's not ending your xyz right if i now give a method so let's see if it returns true if i run it again and now it has given to perfect now moving on to the next function which is include so what include function define so include function this method checks whether this the string variable contains which by string or not okay so for example in the string first my name is amit we have to check whether name is present or not right so it will read it's a boolean written uh function so if the string is present then it will stay true as it will say false so for example if i if i want to go if i want to check cs dot info i will add again and if i want to check now what if str dot includes we will again go with xyz and try it out so it written false because str doesn't include xyz right now if i change it to name if i run it again now it will return true because it it includes name name assistant right moving on to the next method which is which i think most of you will be aware about is index law so what this method does this method tells the returns the index of first occurrence of given query string okay and if that particular string is not present in the in the provided stream then it returns minus one that is uh in a given character or string the provider characters our string is not present okay so let's give it a start so we will simply go with cs dot info again and we will check str dot basic index of index of what we will check again the index of choices if i go and say so it doesn't include x z right so it should written minus one so you can consider minus one is like the string is missing now if i go with is right so if i give is if i return it out then it shows it's present at index 8 let's count it up ml 0 1 2 3 4 5 6 7 8 see so this way it works now let's move to the next method we are going to be quick okay the next method is repeat so what this method does this method returns string with the number of copies of the existing string okay this method accepts single parameter google that holds how many times you want to copy that particular string up for example i want to copy this string mine is amin because i'm really fond of myself 10 times so what i can do is cs dot info and i can do str dot repeat how many times you want to repeat so i'll give 10 times and if i run this out then it has written it my name is image 10 time okay let's verify it out control f and if i search amit you can see 2 times here and 10 times 0 so it gives the count of 12 times perfectly so the next method in our list is replace so this method returns a string with the changes this method is the case sensitive okay so you have to do you have to pass two parameters out the one which you want to change is the second with which you want to change the particular string so if i if i want to change my name from amit to what um gujarati so i can simply go and i can do gs dot info you can do str dot replace so this function holds two arguments right so the first argument will be and it's a case sensitive so we will give amit and the second argument with which you want to replace the string so here i will give amit and if i run this out now so it should repress the string amid if i run this out you can see the string got turned into mineral moving on to the next method the next method we have is search so what exactly search this search method specifies us this method sorry for specific y specified value a regular expiration this method is returns the position of the match if it found and if it's not found then it will give minus 1 as as the previous method index of okay this method is also case sensitive so let's give it a try so for example we again let's take this against so if i go here dot info and if i go str2 dot search search word if i check for technoma and and if i run so sorry i think i need some okay i missed code it gave 19 that means it it it is located at 19 if i change it out to pick from monks right which is not a valid string then it should return minus you can see over here so it's working as expected moving on to the next method these methods are pretty easy to understand but for the beginner this is must to understand these things so i'm creating this tutorial so search is done now the next method the next method is slide okay what this method does this method extracts the part of the string and returns the new string okay so this particular method accepts two arguments what is the start index what is the start index parameters code the index from where you want to extract and the second one is ended this particular parameter signifies that on which particular index you want to end it out so for example [Music] if i go if i go str2 dot slice now we have to provide the character so we'll provide character one two three from third character still seven character i want to slice this particular string out if i run this out then it has given c e h a n okay so from third to seven four characters okay if i'm not giving the second argument and if i try to run then what it does it will consider the first character and the end will be the end of the string clear now moving on to the next method which is split so what split does this print method split the string into an array of substring so basically when you want to create a array out of the string okay you can use the split button this method array okay so for example uh i want to break this particular line into my name is amit as a four arguments of aaron so what you can do gs dot dot info and we can give str to dot and on which basis we want to split so we will give p right to end so you can see it got converted into an array my channel name is so this is very useful when we want to convert a string or comma separated string into an array i i use this a lot okay because i store all the for example we have a client script where i want to make all the mandatory fills as we did only the tutorial which we have created two days back then we are storing all the strings as commas appear and i convert it on the fly so this is done split now the next method is start with so this method check if a string start with a given query or string okay if it start with that particular thing then it gives true as it returns false the way we had end with ends with right and the syllable is it starts with so for example if i go cs dot info i want to check if str2 dot starts starts with she has to starts with my name which is amit is it this thing if i run it out so it didn't fall because it didn't start with my name if i said like my and if i check if it's getting started with y if i run this out then it will return true because it started getting started with that right so this was starts with now what else method we have oh the other method is to lowercase so this method converts all the character present in a string to lowercase and returns a new string with all the characters in lowercase so for example if i want to convert this particular string my name is amit so how exactly i do i will simply do cs dot info and i can do st cr2 dot cr2 dot to lower case okay if i run this out so you can see everything got converted into small case right in this similar way we have one more method called convert to a pokey so the method has two uppercase and this particular method converts all the character present string to uppercase and it does a new string with all the characters enough working so we have to just replace the slower to occur and if i run it out and it has converted the string into all of this connector perfect now where exactly i use i use this also quite frequently because when i want to make my logic case insensitive right so what i do i either convert it to whatever user is providing as a value i either convert it to lowercase or uppercase and then i do all the uh comparison for the same okay so it depends on your use case now the next method which i use a lot so what this stream method is used to remove either white space from the given uh give a string this method gives a new string okay with with the level of white so for example in the start and end of the string as it will combine all the characters for example the string one is my name is and if i give spaces like this okay so if i want to and if i do gh dot info and if i do [Music] str dot len which is another function to calculate the length of the string okay if i run this out now you can see it's 24 character string because it has included the white space also now if i want to remove the white cell because it's not always visible to anyone right so in order to avoid this kind of errors we can we should always use stream okay when i was doing certain data so if i use stream so i will do um gs dot info and i will do [Music] sdr dot trim okay so it was it it has written the string i am not sure whether it has a white space or not so let's do the length are the same if i run then you can see there was around nine characters of white space over here so it has removed that blank character so this was the basic function which i feel which is very important for any beginner developer to understand with respect to stream okay we have already created a series with higher order functions on array and i will be trying to create your basic functions tutorials also on the array because i got a lot of request on the scene so please drop a comment if you want to see certain videos in the future okay till then stay safe stay happy and have a nice you
https://www.youtube.com/watch?v=YWRKgTHbxxA