so in the today's session uh we are going to
learn something about uh strings which is most important topic and we do different type of
operations on strings uh by using uh string built-in methods so we have certain methods
available in Java so by using those methods uh we can perform different type of operations on
strings so actually there are two kind of methods in Java one is buil-in methods and user defin
methods or we can say custom methods and user different methods we can create our own methods
and they are Again part of the class and uh there are some built-in methods buil-in methods in the
sense Java is already provided those methods and we have to just use those methods and we have
to know how to use those methods okay so I'll tell you simple example what is Method and how it
will work for example uh let us take one string variable let's say string s equal to elcome so
this is my string variable right now I want to find the length of a string for example I want
to find total number of characters in a string I want to find total number of characters in
a string so what we can do is yes is a string variable so we have to take the string variable
and this is variable of type string string is a predefined class in Java string is a predefined
class so which is having some predefined methods also so we can apply all string related methods
on this variable okay so when I create a variable with a string type whatever methods are already
available in the string class we can apply all those methods to this variable okay so how to use
suppose I want to find length of a string so take this variable dot we have a method called length
like this so This is called method buil-in method which is already implemented by Java itself so
s do length when I call this method this will return length of a string so that we can print by
using system. out. printel or we can store that length in some other variable and we can perform
some operations on the length Okay so this is simply called a method and by using this method
we can perform different typee of operations on string variables so there are so many methods are
there length is is just one of the example just one method similarly there are so many methods
are there in the string class and we can apply all those methods on the string variable then
we can perform various types of operations so now today's session we will see what are what
are those methods are available and how can we use those method there are 100,000 thousands
of methods are there and we can't discuss each and every method but mostly used up to 8 to
10 methods are there we will use them very frequently Even in our automation also especially
when you do validations we use these methods very frequently on strings and those methods we will
discuss today okay very simple very easy concept and you can easily understand also okay so what
is a method a method will perform certain task and whatever methods we are discussing here all
those methods are belongs to string class so that we can apply all those methods on string variables
okay so in the coming sessions in objectoriented programming you will understand in detail what
is Method how to create user defin method how to pass parameter how method will return return the
value all those things in the coming sessions we will understand in detail in the O concept
I will discuss that and here these are the buil-in methods they already available just
we have to know how to use those methods okay okay all right so let us directly go to eclipse
and I will show you different type of methods and different type of use cases you can easily
understand let's go to our project and create new package and today is day 8 click on finish take
new class and I'll name it as string Methods take this main method very important even interview
prospective also most important topic so let listen carefully and focus right so the first let
me take one string variable let's say string s equal to okay let's take string variable string
s equal to uh because it is a string we have to keep inside the double putations welcome so
this is my string variable string s equal to welcome so I just created a string variable this
is how we can normally create a string variable okay in Java there are other way also there we can
create a string so this is one approach the second approach is we can declare a string variable like
this string yes equal to new string of and here we can specify the value this is another way
of creating a string variable actually there is a difference in this later I will discuss
what is the difference between these two but mostly normally we use this one string s equal to
welcome if you want to create a normal variable you can simply a string s equal to welcome and
if you want to create a string class object then we will see like this string s equal to new
string of Welcome these are two different uh notations or two different ways we can create
a string variable but there is some difference between these two when comparing or when I store
data in the database uh I'll show you that later for now you can just follow anything either first
syntax and second syntax both are correct this one is correct this one is also correct so let's say
when I print this you'll get the same thing when I print s value okay and you will see the value
of s is welcome so you'll see the same and even if you have this kind of notation second string
s equal to new string because why we can use new because string is a predefined class so now we got
a same value so for both we are getting the same value but it doesn't mean both are exactly the
same there is some difference minor difference is there so later we will try to understand the
difference but for now just follow one of the syntax either you can specify this for string s
equal to welcome or string s equal to new string of welcome that we have to put in the brackets
both are correct okay now let us see how can we call the methods on the string what are the
different methods are available in string okay the first method is what length as we discussed
so length is a method so through which we can find length of a string so that means this particular
method will returns uh length of a string length of a string that means number of characters
number of characters in a string if you want to find the number of characters in a string
so this method we can use so for example let us say I'm taking one string variable string
s equal to welcome now I want to find length of this particular string so how can we do it
we can simply say yes dot you can see as soon as you put dot it will show you all the string
related methods these are all string Methods you can see there are so many methods are there there
are so many are there so it will show you in Auto suggestion automatically and then you can simply
type one or two characters it will automatically pop up you can see length you can see as soon as
I type Le it is showing one method length this is the method so s do length so this particular
method will return integer as a return type you can see the description will automatically show
you here it Returns the length of a string length Returns the length of a string so what is the
return type of this method is integer integer is the return type so we can store this value
the length of a string in some other variable let's say int L and then we can print the value
of L okay so this particular method will return the length of a string so I can store that in a
variable then I can print the value of L this is a way we can store the value in a variable and then
we can capture it suppose I don't want to use any variable I just want to print so you don't need
this variable you can put that method directly inside this print L yes do Len so you can directly
so this will return length of a string printed by print Ln method so we can directly print length
in a as an output so like this we can use length okay and suppose you don't have a string variable
I want to apply the length method directly on the value directly so how can we do this suppose I
have a string like this welcome this is a direct value not a variable now I want to find length
of this simply can say dot length you can also do like this this will also return return the same
thing okay so either you can use variable string variable or you can use directly string value
when I specify the value put inside the double quotations and if you don't have a value or if you
have a variable then you can directly specify the variable dot length we can use any way okay s do
length s is a variable which contains same value again s contains a some string value you can
use like this or if you don't have a variable if you want to directly use on a string v string
value you can put that value in the double codes dot length so this will also return return the
same thing so this is how we can find length of a string means how many number of characters
are present inside the string okay now the next method conat conat this is another type of method
so what is Con cat hcat means you can join the two or multiple strings joining strings joining
strings so for concatenation uh earlier how we have done concatenation we have done concatenation
many times so how we have done yes by using plus operator we can also do concatenation right by
using operator we can do concatenation and also in string they have provided one more method
called concat by using this concat method also we can join multiple strings we can use both uh
I'll show you uh let's take some strings let's say string S1 equal to welcome this is one string
and I'll take one more string string S2 equal to say to Java so now I have a two strings I want to
concatenate them I want to join those strings so normally what we can do is we can simply use plus
operator I can say S1 plus S2 so this will do the same thing but other than this plus operator how
can we use concat so you can say welcome to Java so this is a concatenated string it is basically
joined both the strings same thing we can achieve by using concat method so how can we use S1 dot
conat of S2 so S1 is a first string Dot concat and in the bracket we have to specify second string
so this will concat both the string S1 do concat of S2 so that will written the concatenated string
so so we can put in the print statement so that it will print concatenated string so when I run this
you'll get the same but so string comparison we can do in two ways either by using plus operator
or by using concat method okay and one more thing I'll show you suppose I have one more string let
us say string S3 string S3 let say automation this is another string now I want to join three strings
S1 S2 S3 right so if I use normally plus operator you can directly say S1 plus S2 + S3 so this will
join all three strings S1 S2 and S3 S1 S2 and S3 so you will get the final string but how can we
use concat for for joining more than two strings because concat method we can join at a time only
two strings S1 is one string dot concat of S2 that's it so concat method can join two strings
at a time at a time it can join only two strings but if you have a three strings then how can we
join by using concat itself okay so by using conat itself I want to join three strings how can we do
it but at the time Conca method will join only two strings right so how can we achieve this if it
is a plus operator we can directly do this but if it is concat if we want to join multiple then
we have to do multiple concat okay yes you right so first we have to concat first two strings S1
and S2 how can we concat so if I do this S1 and S2 what is an output you are getting here this is
an output right so what I'm doing is I will try to use this as it is so this will concut S1 and S2
once you get an output again dot one more concat concat PS3 okay so first we are concatenating two
strings S1 and S2 so this will return one output and after getting an output again on top of it
we are again conting with another string so if we have a multiple strings we have to do like this S1
dot concut of two dot immediately concut of three dot concut of S3 4 S4 like this you can concut
n number of strings so this is how we can do the concatenation of more than two but by default at
a time it can do only two strings concatenation only two strings are allowed if you want to add
another string you can say dot concat one more concat method we have to use so you'll get the
same output so conat by using plus operator we can do by using concat method also we can do now
let me show you this suppose I don't have string variables I want to use I want to concat okay and
directly so S1 do con of S1 yes that is also true that is also right so you can do any number of
ways okay so you can just think about it there are many ways to do it I'm not saying this is the
only way to do it because this is a programming so if I give one problem multiple people will come up
with the multiple Solutions all are correct okay until unless your output the result is correct
right so that way is correct so that is perfectly fine you can also do like this first you can conat
combination of plus and concat also you can use S1 do conat of s S2 Plus S3 also correct that is also
true let's try this okay Angel is saying like this S1 first S1 okay first letters S2 + S3 so what
this will return return S2 + S3 it will concat it will join S2 + S3 and uh this we will concat
you can also do like this S1 dot conat you can also do like this so here we are using concat here
we using class operator so this is also this is a combination of conat and plus operator so this
is also correct okay now suppose I don't have variables uh I just directly want to concatenate
strings Valu string values are directly conc I don't have a variables I look at here everywhere
have used variables string variables suppose if you want to apply them directly on the value then
how can we do this let's say system if it is a con plus operator we can do like this so this is
my first string plus this is my second string so we can do like this by using plus operator we can
directly concat to string values here we haven't used variables just we use direct values and same
thing we can also do by using T C how can we do it you can say first string is welcome dot conat
of second string is to Java like this also we can WR so instead of variables you can directly
specify the values also so this is also do the same thing okay so you can also concat more than
two strings this is all about concat method so you can use either plus operator or you can use
concat anything is fine both are available okay but conat is especially for string related method
plus is a normal operator when I use plus operator between two strings it will concatenate when I use
plus operator between two numbers it will perform addition operation because plus operator works
in the different aspects when I use plus operator between two strings it it will concatenate if I
use plus operator between two numbers it will do addition of two numbers and if you use concatenate
operator between a number and string then also it will perform concatenation okay remember
that part now let us see one more method trim this is one more method called trim so what
this method will do it will remove spaces the right and left side of the string remove spaces
right and left side it will remove the spaces right and left side okay so for example let's
say I have one string here s equal to and I'm giving three spaces 1 2 3 welcome and then I make
again give three more spaces 1 2 3 three now I have three spaces before the string three spaces
after the string okay now if you find the length of a string what you will get if I find length
of a string what you will get length I'm asking about length can you guess there are three spaces
before the string after the string there are three more spaces and how many characters are there
in the string seven characters are there okay 7 + 3 + 3 7 + 6 totally 13 characters are there
okay including space space is also counted in the length method okay so my here my requirement is I
want to find or I want to extract just a string I just don't want to extract this spaces okay when
you print this when you print yes value just observe it is just printed s value along with the
spaces you can see there are three characters here and there are three spaces here three spaces
we are able to select here can see and here also we're able to select three spaces okay so
now I want to remove the spaces I just want to get only string after removing spaces right and
left both you can have any number of spaces but I just want to remove the spaces right and left
then you can use this method trim so how can we use this so this will print along with spaces
so print string along with spaces but I want to remove the spaces right and left and then
I will print so the method is s do trim s do trim every method should followed with bracket
so s do trim that's it we don't need to pass any parameters just print it s do trim if you want
to store trimmed string in some other variable you can do that okay now see the difference here
I'm printing the direct s value here I'm printing s do trim you can see the difference okay now
you can see here there are three spaces right and left side but here there are no space I'm
not able to select anything here but here we're able to select so right and left side spaces
are removed from the string so for that we can use this method s do string s is a string
variable so that will remove the spaces right and left side okay so let's try to find the
length before trimming after trimming so here uh let us try to find before triming okay
so let's system do do do print or you can say okay so let me take this string yes
dot length I'm directly finding the length of a string here I will write another
statement system p s dot length so this is before trimming okay before remming
I'm concatenation again I will keep it here so just I'm printing s value here here I'm
printing just s value in the second statement I'm printing the length now here I'm trimming
this yes and I'm printing after triing so then uh I will try to find length of ating after after
triming so how can we do this yes do trim so this will trim the string after trimming then I want to
find the length again continue dot length you got my point I can use multiple methods sequentially
so you can say first what I'm doing here H do trim so this will remove the right and left side spaces
and WR the actual string and followed by length so once it is written actual string then I'm finding
out the length of a strink okay so now we can see here this is after trimming so I can just put
some message after R like this right now when I run this you can see this a difference so before
trimming 13 characters are there including spaces and after trimming we have only seven characters
so like this we can also the combination of methods based upon our requirement so first we
have trimmed the string then we have find out the length here we directly find out the length
so this is how we can use trim it will basically remove the left and right side spaces now the
next method is carrot carrot method car a is a capital letter all these methods are K sensitive
so you have to use very carefully car at so this particular method will return a character from a
string based on the index returns a character from a string based on index so again in the string
also there is an index it will start from zero okay for example uh if you take this particular
string here this is an index 0 1 2 3 four five six six is the last index so index will start
from zero even in the string also so here the carrot method will return a specific character
from a string based on the index which we have to provide okay so for example let say here yes equal
to welcome this is my string this is my string now I want to extract specific character for example
I want to extract a c I want to extract the C so what is the index of C here index will start
from zero index start from zero so now what is an index of X say system yes dot carat of here
we have to pass index number so when you count from zero what is an index of c0 1 2 3 three is
a index of C so that you can pass here three so now this particular method yes is actual string
dot car of three so this will actually Returns the particular character from the index so when I
run this this will return the particular character you can see C is got returned so like this you
can extract any character from a string by using car method so car method will return particular
character from a string okay suppose I say system print yes do car of Z Now what is an output of
this s do carat of Z what this will return W because that's the first zero character Zero index
there is a dou right this is a return type so very simple character method will return particular
character or specific character based on the index which we passed right next method contains
another most important method we use most of the times when you do validations in your automation
test contains method so contains method uh will basically check a particular string is a part of
the main string or not okay for example uh let me take I have I have one stream like this just a second just a moment so I have one main string like this okay
this is the main string now this is another string now I want to check this particular string
is a part of the main string or not okay this particular string is a part of the main string
or not yes or no yes because this is a welcome this is a part of the string here it is right
so if this particular string is a part of the main string then the contains method will return
returns true okay so how can we write this suppose S1 contains S2 like this you can write now S2 is a
part of the S1 then it returns true S2 is not part of S2 then return false that means the contains
method always returns a Boolean value it can be true or it can be false right so if the value
is a part of the main string the string which we are passing is a part of the string string
then returns true if it is a odd part of the string return false that's how contains method
work and the string values are K sensitive it will treat differently lower case and uppercase
characters it will treat differently okay so if this string is a part of the main string not
only first position it can be anywhere in the string then it returns true if it is not part
of the string then return false that is called contain method okay so we can check a particular
string is a part of the main string or not so that we can do by using contains method so contain
method is always returns true or false okay returns true or false true or false now let's
see some examples yes dot so here we already defined this s variable you you can change this
value n number of times because this s is already defined that's the reason data type I have not
specified okay s is already created many times so now I can say yes dot what is the current
value of s here welcome right now I say yes dot contains is. contains and here I'm passing
welcome or I said W and then I'm printing the output every method will return some output so
that we have to print by using print statement now tell me the output of this true or false
true or false it is true okay because this W is a part of the main string so that is true
perfect now s do contains C now tell me it is true or false s do contains C yeah C is also part
of the string so that is also true that is also true now I'm saying s do contains s do contains
W again true or false this is false why because W is a upper case and here W is a lower case
so string values are case sensitive so it will treat ler and uppercase characters differently so
this will return false this will return false and similarly if you just say system pint Ln s do
contains C so all are capital letters so this also return false this is also return false okay
now you understood how to use this contains method you can just check the string value is a part of
the main main string or not sorry not con contains sir right so now these are the four values
true true false false so contains method is always returns true or false so is it necessary
that all characters that we are looking in the first string to find match must be together yes
yes exactly okay so all characters the sequence also should exactly match suppose a w e l is
one string the same sequence you should find here but W is one place e is another place L is
another place it will not match like that okay so w is exactly should be part of the mainst string
the sequence also exactly match but the position is not important suppose here you can see I'm
comparing see c m where is this position here but it is still returns true so the position is
not important but the sequence of characters are exactly should match okay so that's how we can
check suppose here if you write like this s do contains s do contains w l m w e l m true or
false true or false false okay because this is complete sequence is not there in the mainst
string W is there me is also there here but the combination of these two the sequence exactly
not matching here so obviously it is false okay so this is how we can say contains method it will
check the string is a part of the main string or not check string is a part of main string or
not so always returns true or false okay now so the next method most important especially we
use it for comparison equals and equals ignore case equals and equals ignore case so these are
the two methods and both are used for comparing the strings both are used for comparing the
strings equals and equal case so how to use this comparison how to use whenever you want
to compare two things are equal or not you can prefer either equals method or equal ignore case
but slight difference let me show you so let me create a two string S1 equal to welcome this
is one string S2 is also welcome both the two two different strings S1 and S2 now if you want
to compare these two strings can we use equal to can I use W equal to S1 W equal to S2 because
equal to is a comparison operator right yes you can use it but not always there is a difference
actually I tell you so system P S1 dot equals of S2 so you can use either double equal to for
comparion or you can use equals method S1 do equals S2 if the strings are equal returns true
even comparison operator also returns true and equals method also returns true true but uh
sometimes uh dou equal to equals method not exactly the same in some scenarios are there I
will show you that in the next session but for now just follow this okay if you want to compare
the two strings we can use either double equal to or we can use equals method but when we have to
use double equal to when we have to use equals I will show you with some examples later okay
for now just follow like this so when I want to compare those thingss can use equals method
like this S1 is one string dot equals S2 both are same so returns true suppose uh let us say
I'm writing like this system L S1 dot equals here I'm saying welcome now tell me true or false
can we write like this first of all yes we can write like this right S1 is a string variable dot
equals I'm specifying the value directly instead of variable I'm specifying the value directly
in the double quotes correct so now what is an output of this obviously true or false S1 is a
welcome S1 is a welcome and comparing with the welcome another welcome it should return false
why because K sensitive so here W is a lowercase character here W is an uppercase character
right so equals method will return false equals method will consider case sensitivity
it will treat lower and uppercase characters differently but my requirement is even though
if you pass uppercase I just want to ignore the case sensitivity then you can go with the equals
ignore case so equal ignore case method will will not consider Cas sensitivity it will treat lower
and uppercase characters same it will treat lower and uppercase characters same so if you don't want
to consider case sensitivity you can directly go with equals ignore case that's a major difference
between equals and equal ignore case both are used for comparing strings but equals method will
consider case sensitivity means it will treat lower and uppercase characters differently whereas
equals signore case method will treat lower and uppercase characters as a same so if you want to
consider case inity you have to go with equals if you don't want to consider Case Case sensitivity
or if you want to ignore case sensitivity you can go with the equal ignore case now if I look
at this if I write the same statement by using equal ignore case it will return return true just
observe equals ignore is instead of equals I'm just calling equals ignore case here it is rning
false here it should return true that is a major difference okay now we can see this is a
false and last one is a true so equals and equal ignor when you're comparing the strings
we can use either equals method or you can use equal ignore case slight difference okay now
in the next session I will talk about uh what are the differences what is the difference
between dou equal to but here we are getting the same value right here we are getting true
here also we are getting true but what is the difference there is a difference so when you
when we have to use double equal to when we have to use equals what is the difference I'll
show you with the examples okay next method replace next method is what replace so this
particular method will replace single or multiple characters in a string or it will replace
multiple of sequence characters in a string that's called replace method it will replace single or
multiple sequence of characters multiple in the sense sequence multiple in the sense sequence can
put bracket characters in it will replace so first we will see how to replace a single character
and then we will see how to replace a multiple characters multiple characters means sequence okay
let's take a bigger string s equal to welcome to selenium Java again selenium python selenium
sh okay I'm just taking very bigger string many things are repeated in this there are many things
uh repeated now wherever the E is repeated e or you can take any character so e is repeated many
places so wherever the E is repeated or wherever the E is occurred I want to replace all yes with
some other character okay let's say I want to replace all es with some X so different character
I want to replace in all the places right so we can say s do replace sorry so s dot replace
S do replace and which character you want to replace that we have to put in the single codes
first which character you want to replace I want to replace e second second parameter comma second
so e is replaced with which character let's say x some other character and this command will return
a complete string after replacing all e with X okay so I can directly print like this system P
yes do replace all e wherever the E is repeat all e will be replaced with X and then it will print
you can put any character replaced character so when I say run as jav application okay now
just observe the output so this is an output so wherever the E is occurred that is replaced
with that is replaced with X you can see this is a one place and here it is another place here
here here okay and here here all es there is no e now every e is replaced with X so if you want to
replace a specific character sorry if you want to replace character then we can use replace method
like this specify s do replace S is a string do replace which character you want to replace and uh
and with character which you want to replace with so you can specify this is the existing character
this is a new character so this will automatically replace can we do group of characters yeah that
I will show you so not randomly but if you have a sequential you can do that so that's the next
option so in this we have seen how to replace a specific characters in multiple places or in all
the places but I sometimes I want to replace a string string I want to replace for example
here celum is repeated here and here is is also repeated two times it is repeated and three
times here it is three times it is repeated now I want to replace the entire string here here and
three three places it is repeated all three places I want to change okay so I can change like this
yes dot replace okay and which string you want to replace here I want to replace uh selenium okay
so I can selenium like this and replaced string which string you want to replace with let's say
the r like this right now we can just say print because it is a string we have to keep inside the
double quotes and put inside the print statement s do replace of which string you want to replace and
the new string which you want to replace with okay just observe wherever the celum is repeated it is
replaced with the play right so this is a final one okay so here this is one place second place
and third place is called replaced so by using replace method we can replace character we can
also replace multiple or sequence of characters multiple or sequence of characters this is how we
can use replace but randomly randomly means what the same character should repeat multiple places
we can do it same string should repeat multiple places that also we can do it so replacing of
character replacing of a string possible by using replace method so replace method can use to
replace single or multiple characters in a string multiple multiple characters in the sense those
characters should be in the sequence at a time we can replace only one single character or at time
we can replace with one single string multiple things we cannot replace okay so this is a replace
so whenever you want to replace Single Character you can go with the syntax whenever you want to
replace with a multiple characters or sequence of characters you can put them in the double quots so
one method replace method we can use two different purposes Single Character or multiple characters
okay now so the next method substring every method is most important guys because we are going
to use all these methods even in automation multiple places when you do validations these
methods are very very helpful the next method is substring so what is this method substring
method will do it will extract substring from the main string extract substring from the
main string it will extract sub string from the main string so let us try to understand uh then
I will show you some example sub string what is substring I will also give you some simple thumb
rule here and you can just follow this suppose I have this string this is my string this is main
string in this I want to extract some portion of the Str suppose I want to extract W or I want
to extract c m or I want to extract first middle three characters or I want to extract last
two characters I want to extract first two characters like this I can extract some part of
the string from the main string that we can use use substring method so substring method can
extract some portion of the string from the main string so these are all some portions of the
main string so by using substring we can extract some portion of the string okay so but how to use
this substring so here we have to write like this substring in the bracket we have to pass two
parameters one is s is a string s do a subring here two parameters we have to pass stting index
ending index indexes we have to pass so normally uh index will start from zero only right so this
is zero 1 2 3 4 5 6 this is the indexes so the starting index suppose I want to extract first
three characters what is the starting index of w zero so zero is the first parameter comma from
zero W I want e and l so so what is the index of l two what is the index of L 2 but instead of
two here we have to specify 2 + 1 that is three we have to specify that means when you count
starting index value start from zero starting index and when you count ending index start
from one because always it will be+ one so this is 1 2 3 4 5 6 7 if you look at here first three
characters I want to get so what is the starting index zero and what is an ending index three so
0 comma 3 means 0 1 2 so ending index is three so you have to count like this 0 to three w l will be
written like this okay suppose I want to capture c m now tell me index s do substring of tell me
the starting index and ending index I want to capture c m e c starting index of C is what three
comma ending index of e is what ending index of e seven is an ending index s seven T comma 7 so
this will return c m e okay like this similarly I want to capture uh I want to capture let's
say I'm writing like this s dot substring of 1 comma 4 now tell me what is an output of this 1
comma 4 1 comma 4 one is the starting index where it is started here e and four is the last from E
four is the last index means here e LC e LC you will get like this okay now you understood how
to extract substring from from the main string by passing starting index and ending index start
anyway index will start from zero but when you specify the ending index we should say plus one
or thumb rule is like this starting index when you count start from zero when you count ending
index start from one in that case you don't have any confusion okay so starting index count from
zero ending index count from one especially only for substring method I'm saying but actually index
will start from Z Z only but when you pass ending index you always do plus one okay this is how we
can use subing method so you can extract anything from this particular string anything okay now I'm
writing like this s dot substring of substring of 0o okay 0 comma 1 now tell me what is an output
of this 0 comma 1 Single Character it will return W because what is the starting here zero zero
index is what W ending index one ending is one means what again W it will return return Single
Character so if you want to extract a single character like this is there any other way to
do it Single Character I want to extract Single Character I want to extract how can we extract
in another way without using subst yes character method s dot carat of directly you can pass index
zero that will return W right so that will return W just a second okay so you understood now how can we use subing and character okay
fine so let's try to use this practically so substring starting index
You Can Count from zero ending index you can count from one this is just a
thumb rule okay actually starting index any index will start from zero only
but when you pass ending index you always add plus one okay now based on this
we will see some example s equal to selenium right now I want to you can you guys can tell I will write a method so system s
dot substring of sorry s dot substring of substring of Z 1 comma 5 now tell
me what is an output of this 1 comma 5 1 comma 5 0 1 2 3 4 5 0 5 6 7 1 2 3 4 5 6
7 8 Now 1 comma 5 1 means what E from here five five means what here n
between e and n e l e n so this is an output everybody's understood any confusion here I'm just simplifying it okay so this will return e l en is a output
that is a substring yes perfectly fine now you guys can tell me first three characters I
want to extract s e l now tell me what is the starting index and ending index s do substring
of you tell me I want to get output like this s first three characters starting index and
ending index0 comma 0 1 yes three so this will 0 comma 3 will return first three characters
okay perfect so now I hope you understood how to use substring method so what is the use of
substring it will extract a substring from the main string based upon the indexes which
we passed so very very useful method next one the next method converting the case so we
can convert the string from lower to Upper and upper to lower case Okay so the question is if
you want to replace random characters or two or three characters can we use replace yes you
can use replace but you have to write multiple replaces you have to use multiple replaces
let me give you a simple example you guys can try this if you want to replace multiple
characters how can we do this let us say here I have a b c a a cc let's say this is my string I
want to replace a with X I want to replace C with Y okay this is my requirement all A's I want to
replace with X all C's I want to replace with Y so how can we achieve this first you have to take
the string replace all A's with X so then what you will get x b c x x c c this is the string
you will get once you replaced then again use the string and replace all C's with Y then you
will get x b y x x y y like this so two times you have to use replace method first you have to
take the original string replace all A's with the X then you'll get another string again replace
all C's with another so then you will get the final sing so you have to use two replace methods
like this yeah that is the combined one perfect replace all a comma X then it will give one
string on top of it again one more replace you can use replace C comma y yeah that is perfect
the state point you got my point so first you have to replace all a with x dot again one more
replace C with Y so we can write statement like this see once you start automation you will see
all these examples okay where you have to use if if you start writing your automation code
can you understand now can you understand the automation code I write like this now you can't
okay just wait for some you just understand how to use and what is these methods and once you
completed then once you start automation you will understand exactly where you will use all
those things so we just learning all the all the concepts bits and pieces so once you have
understood all these things then finally we will try to use everything in automation so there
you will know exactly how to use all those things okay okay now let see uh one more method yeah
converting the case so we can convert strings lower to Upper and upper to lower for that
we have two methods two upper case and two lower case so these are two more methods
which we have two uppercase and two lower case two uppercase and two lower case
so these are the two methods which we have let's see uh taking string and here
I'm taking mixed characters now I want to convert this entire thing into uppercase can
say as dot two uppercase this is the method s do two uppercase this will convert entire thing
into the uppercase and then you can print like this okay and then if you want to convert entire
thing in the lower case instead of two uppercase you can say two lower case like this very simple
you can just use it two upper case and two lower case now we can see everything is an upper
case here everything is a lower case so this is converted everything in toer and this R is
converted everything into case so very simple St forward two upper case and two lower case
converting the case one case to another case right so now we'll see one more method so another two
to three methods are there we will finish quickly so the next method split most important method
split so this particular method will split the string into multiple Parts based on the deleter
so split method will split into multiple Parts based on the Del meter so first we'll see simple
example then I will show you real time use case so split me will split the string into multiple
Parts based on the Del meter okay let me give you a simple example let's say I have a string like
this AB b c okay uh at theate gmail.com so this is let's say I have some email ID like this this
is a string from this particular string I want to extract this particular F normally that is
a username okay so from this particular email address I want to extract the username how can we
extract how can we extract a BC I want to extract which method I can use substring right substring
so by using subing we can specify starting index is zero and ending index is three so then you
will get ABC suppose tomorrow if it becomes biggest thing okay so for example AB CD EF like
this will it work again we have to again change the index in the substring method right we have
to keep changing starting index ending index in the substring method as soon as you are keep
changing this email address the user name it keeps changing accordingly in the subing method
also you have to keep changing indexes so that is not sufficient so even though that username
is keeps changing we should able to extract exactly okay without changing anything if I use
subing what is the uh what is the limitation here especially in this particular scenario what
is the limitation I can say s do substring s do substring starting index zero ending index
I can say three okay so this will return ABC no doubt s do substring of Zer comma three so when I
run this this will give you ABC as a output okay we got a username but tomorrow instead of ABC if
it is got changed let's say I make it as ABC 1 2 3 so now what is the problem in this you will not
get the right thing still you're getting only ABC but I want to get the entire usern okay this is
the problem with the subing so how can we extract dynamically so we use one more method called split
by using split we can split this entire thing in multiple parts and once you split then we can
extract only this part whichever part you want you can extract after splitting okay so subing method
is not appropriate here so we need to go with split so now I'll tell you how to use split this
whole string we can separate or we can split based on some deleter what is a delimeter here atate is
a deleter dot is a deleter sometimes you may have space space is also deleter okay so based on them
we can separate so for now I want to separate this string based upon the adate symbol because before
adate ABC is there after at theate gmail.com is there but which one we want we want ABC that is a
before at the now I will divide this string into two parts based on atate at theate is a deleter
we can use so how to use it s do split s do split here here we have to just specify Del meter in
the double quotes on what basis you want to divide this based on the at the rate So when you say at
the rate here so this will return two strings the split method will return two strings when you put
the cursor here split the string around matches of the given that means it will basically return two
values how they it will St how the it will split based on the Del meter so whichever delimeter here
it is so before at the what is there ABC this is one string after at what is there gmail.com so
this is another string this is one string this is another so the finally display method will return
two strings first string and second string so if you want to store those two strings in a variable
what type of variable it should be if you want to store those two strings in a variable what type
of variable it should be it should be single dimensional array right so what I will do is after
splitting them into two parts we will create one array variable here what type of an array string
type of array because both are strings string I can say string a equal to you you understood the
point what we have done is s do split of at theate based on at theate we have split the entire string
into two parts now first part is ABC second part is gmail.com both the strings will be stored in a
now what is a contain two strings a contains two strings first string is what ABC second string is
what gmail.com now I want to get the first one how to get the first one from a how to get the first
ABC from a based upon index so you can say print a of Z means it will return ABC and if you want to
get gmail.com how can we do this a of one so that will return at theate at theate gmail.com okay
so when I run this you will able to extract exact thing now you can see this is APC and gmail.com So
based on the daily meter we can split the string into multiple Parts based on the Del meter
we can split the string into multiple parts you understood this example I will show you some
more example everyone is understood so even if you change this suppose ABC 1 2 3 now can we get ABC 1
2 3 here here can we get ABC 1 23 yes even though that username is got changed still we are able
to get abc123 without doing any change here but in the subing method we have to keep changing the
indexes but here we don't need to change indexes so even though this username has got changed still
we are able to get username here okay so that is the main usage of using split split method
we can use to split the string into multiple Parts okay so zero means index number from
array you have two values here zero position ABC is there second position gmail.com is there
now I want to extract the first value from the zero position how can we extract from array a
of zero that will return return the first value ABC and if you want to extract the second
value means a of one index number in the last classes we have already discussed about
this concept single Dimension if you want to extract the values we can pass the index number
a of zero we'll get the ABC a of one we will get gmail.com okay so s do split off at the
rate no you cannot directly print because the split method will returns array right the
split method will returns array so we cannot directly print but once you get an array you
can print values from array by using arrays. two string of a okay if I print like this in
the last class we understood if you want to print array elements how can we print by using
array class array dot two string of a so what is an output of this array do two string of a so
there are two values in Array right so these are the two values second Value First value now you
understood so you cannot directly print if you put this directly inside the print Ln you will
not get you will get an exception because split method will return return an array of elements
so once you capture them into the variable then you can print that variable or you can use a
looping statement to print those values now we will get three values so this is how we can get
you understood so I can put the statement here so that you will be more clear after splitting
a is an array variable okay so all the values after splitting all the values will come into
a that is array variable now I have printed all the values and if you want to extract the first
value zero second value is one by using index we were able to extract yes that is also correct
arase do two string of s do split of at that's one single statement you can write if you don't
want to use this variable you can directly write s. split at the rate that you can directly pass uh
here here you can directly pass yes that is also correct okay now let me show
you one more example for split method see this this is another example uh I have string variable string some
amount amount is like this dollars dollar 15 comma 20 comma some 55 and close this is my
amount or some price in string format okay a string format so here it is a dollar symbol and
there is a commas also there but what is output I want to get my expected output should be like
this expected should be just a number 15 20 55 that's it even call commas also I don't want
just a number I want like this okay so actual amount is this one but I want to I cannot perform
any arithmetic operations on this because it is a string format moreover it contains the dollars
and commas and many more if you have such type of things you cannot perform any additions
subtractions all those things right so before doing it what we have to do we have to remove
them dollar symbol I have to remove commas also I have to remove I should get exact number from
that amount okay so this is my requirement so how can we achieve this by using split method so
here we have have to use uh split method by using split method we can do it sorry not by using
split method we have to use something okay can anyone tell me what is the method we can use not
split method no yes so by using replace we can do it by using replace uh we can do it string amount
equal to this one so dollar and comma everything I have to remove only the number I should get
only number I should get how can we do this so this is not a split method guys by using replace
we can do it okay let me try this first of all we have to remove dollar symbol okay there's is only
$1 that we have to remove so how to remove this dollar how to remove this amount do replace okay
Single Character dollar is what Single Character right right in the single quotation specify
the dollar so this we have to replace with what replace with what nothing nothing empty
just put only one single course just single course nothing is specified here if you put any
character like this suppose replace dollar with hash you can say like this so now it is replac
dollar with hash if you don't specify anything it is just remove the dollar sign nothing okay
now this particular statement will remove dollar sign this particular statement will remove the
dollar sign from the string Single Character let me print the output system do do print amount
REM space so if you put single quotation empty it will give you an error so let us try to take this
as a string okay like like this so here there is no replaced value so this will remove the dollar
sign and it will print the output like this okay first step we have removed dollar sign so now we
got this output now we got this output now the next step from this particular thing we have to
remove the commas commas also we have to remove so how to remove this commas again take the same
statement okay so this statement anyway will give you the exact number but only commas are there
now this time you have to replace commas also now we can continue with this dot replace one
more replace and commas we have to replace right commas we have to replace in the double code
specify comma and replace with what nothing put empty double quotes So now this is the final
statement so first we have removed the dollar symbol after that we removed the commas so this
will give you the exact number so this will give you exact number so after removing dollar and
come so everyone is understood this is one use case so when I use a single code it is not allowed
actually so this is a problem here it is fine but here when I use single quote here empty value is
not allowed in the single quotations okay so that is a reason we have taken this as a string in the
string we can specify Single Character also right no problem so that is the reason we have taken
this as a string single quotation empty value not allowed understood how to do it so this is
the one and let me show you one more example for uh split method example two okay now I have a
string like this string equal to AB b c ABC comma 1 2 3 at the rate xyg this is my string in whole
string I have ABC comma 1 2 3 at XY Z this is my string now I want to print separately I want to
extract them in three different strings first a BC is one string 1 2 3 is another one XY G is
another one so I want to split them into three parts I want to First extract a BC 1 2 3 and XY
Z from this particular string I want to extract three a BC 1 2 3 and XY Z so how can we do this
I want to extract ABC 1 2 3 XY Z 3 so first step we will try to split this by using comma delimer
okay by using comma delimeter let us try to split this entire thing how to split s do split s do
split and wherever the comma is there comma is a deleter so comma so once you split this what
is an output of this what is an output of this s do split of comma so this will also return
some array this will also returns some array right you can say AR RR this is my array variable
and string type of an array variable so now s do split of comma means before comma ABC is one
string after comma 1 2 3 at XY Z is one string so after completion of this statement what is ARR
contains AR contains AB c and 1 2 3 at theate XY Z two strings two strings right let's try to print
erase do two string of a r just I'm printing after splitting based on comma what things is captured
so these are the two values are captured ABC is one and 1 233 X is another one so now currently
ARR contains these two strings so ABC is got separated now want to separate this one 1 2 3
at theate XY Z I want to separate based on at theate delimeter based on at theate delimeter I
want to separate 1 2 3 atate XY Z so how can we sep separate so this I want to capture from ARR
first of all right after capturing this value then we will apply this how to capture this one 12
XY from AR how to capture a r r of one right this is a how to capture this from ARR ARR of one so
this will return 1 2 XY that we have to split so dot split and what basis we have to split at the
so this will also return another array right so let me take another ARR string arr1 let's call
this as arr1 and let's call this arr2 this is arr1 okay so arr1 of one dot split off at theate sorry a of two we are storing the
value and here a of one a of one of one dot split okay so what is wrong in this
so we already captured comma and AR one okay just a minute let me try to print this arr1 okay so now we got this in ARR one
array one we have this one so from array one ARR one we want to extract the second
part so ARR one of one zero is the starting index ABC one is this one so this again
we have to split error one of one dot to split based upon the at theate so this
will again return another array so arr2 should be array variable again okay now if
I print AR of two array do two string of arr2 so what is an output of this this is 1 2 3
comma XY J so here we got this so this is a step byep process just try to understand this is a main
string so we have split this into based on comma so after splitting this based on comma delimeter
we got this value a BC separated 1 2 3 at the XY Z is separated in Array one now again from array
one we are taking the second value 1 2 3 at the XY Z again we have split into two parts here so when
I split into two parts again 1 2 3 separated XY Z is separated both are stored in AR array number
two so when I print array number two we got 1 2 three separated XY Z is separated so now I want
to print all three separate AB b c 1 2 3 XY Z Now tell me the statement I want to get ABC from where
I can get ABC a rr1 of zero in first array eror one first array zero element is what ABC so here
you will get ABC now the next statement I want to get one two three from here we will get 1 two 3
here 1 two 3 in Array number two in Array number two zero index so here I can say array number two
zero index so this will return 1 two 3 this will return 1 two 3 okay now XY Z I wanted so from arr2
index is one so here we will able to extract X so this way we can extract or we can split the entire
thing into multiple parts and then we will able to extract the values okay so this is how we can use
split and based on the space Also we can split for example let's say example three space let's say s
equal to ABC space one 2 three now I want to split them into two parts based on the space space is
a delimeter now so s dot split s do split and uh in the double ques just give one space one space
that is acting as a Del meter and that I can store in a variable so a r this is a array variable of
string type okay now we can print array so we can say array Dot to string of a r so now we will see
1 2 3 and ABC are two different parts so you can see this is one part and this is another part
so based on the space also you can divide the string into multiple Parts okay but here there
are some restrictions so you cannot use every symbol as a separator or deleter especially uh
star star is a regular expression you cannot use okay and percen is also regular expression you
cannot use character you cannot use end also you cannot use okay these brackets you cannot use and
uh iPhone or underscore you cannot underscore you can use but iPhone yeah I also you can use no
problem okay so there are some restrictions are there for these particular symbols you cannot
use as a delat as deler especially in Split methods okay AR RR 1 plus AR rr2 is not possible
because AR rr1 again contains multiple values arr2 is also having multiple values so you cannot
combine like this if you have single value is a different thing but you have multiple
values it is not possible in replay we are removing dollar what about space yeah just now
have given the space means again you have to you can also remove the space no problem okay if
you have some space like this okay you can just replace the dollar like this similarly you have
to replace also you can put one space here okay you can put some space after dollar you can put
one space like this so then what happens dollar along with the space both will be replaced with
the nothing you can also do like this if you have a space also you can still handle it no problem
example three okay so in example three you can just look at here I have ABC space and 1 2 3
okay how can we split this into two parts is there any deleter available here no only space
is there so space is also we can use as a Del meter so how can we do it as a DOT split and
uh here within this double quotation I'm giving one space so that is acting as a delimer so
wherever the space is occurred that will be split into two parts now aor contains a two
values a b c and 1 2 3 so for example let's say I have one more value a BC 1 2 3 XY J XY Z
Now tell me how many values you will get here in ARR see how many values you will get here I have
a three values and delimeter is a space so here here one space here it is one place so obviously
it will return three values based on the space we have split so three values you will get okay so
this is how we can split based on the space Also we can split but sometimes if I have a multiple
delimeters then you have to write multiple splits okay it will be little complicated you have
to write multiple splits if you have multiple deleters okay so this is how we can use split
method and the last example you guys can tell me example four this is just your test okay
I can take one string name equal to so John Kennedy so this is some name I have put J is
a capital letter K is also capital letter okay now I want to check the J John is a part of the
string or not I want to check this particular John is part of the main string or not so which
method we have to use yeah obviously contains method we will use right so you can say name dot
contains name do contains here you can say John so this will return true or false so when I print
this so name do contains Z when I print this this will return return true or false now as per
this value which one it will return return true or false false it will written why because
John is a lowercase character but in the main stream J is uppercase character right this is the
problem now my question is I don't want to okay so I want to uh check this John is part of the
main string or not okay but I don't don't want to consider the case I don't want to consider
the case but I want to check John is present or not how can we do it I don't want to consider
the case still I want to use the same value but you should not consider the case sensitive so
it should return true right so how can we do it still I want to check John is a part of the
mainst string or not but I don't want to consider the case sensitive finally I want to get true
as an output how can we make it happen so when I use contain John I should get true here how can
we make it happen there multiple ways are there and at the same time I don't want to use equal
ignore case I don't want to use equal ignore case without using equals ignore case without using
equal ignore case is there any way to do this I just want to check John is a part of the main
stream but I don't want to change the value I don't want to change the value still it should
match it and returns true how can we make it happen Okay let me show you this there are many
ways to do it I'll show you two different ways okay first thing take this name okay and
uh take this name and convert this entire or replace J with a lower case character
let's try to replace name dot replace name do replace name do replace which one you have
to replace J okay so J you want to replace with what lower case J lower case J so what this
will return return this will will return name which contains J is a lowercase character right
so what this will return return name do replace J comma J it will return the same name but J
is in lowercase character it will return the same name but J is a lowercase character
now after that if I use contains contains joural right will it return true or false true
or false the combined statement so what exactly we are doing here we are replacing this J with
a small character so this will return the same name but J is a lowercase character after
that we are checking contains so obviously it will match so this will return true
okay this will return return true this is one approach now can you tell me any
other approach without using equal ignore case you can combine any methods okay you can
combine any methods based upon your requirement you need to think first what is your objective
and accordingly you will find appropriate method okay so this is one approach now what is the
next approach I don't want to change the value John lower case without using equal ignore case
without using equal ignore case still I want to check the John is part of the main string or not
ultimately I should get true as an output but I don't want to use equal ignore case yes so you
can do like this take this entire name convert this entire name into lower case two lower case
okay now the entire name will be converted into lower case right so once you get a lower case
everything then you can use contains on top of it contains say John so because everything is a
lower case and John is a lower case so obviously it will returns true so this is another way so
without using equal ignore case first of all we can't use equal ignore case but this is a way so
there are two different ways we can do it so this will also so you can also combine multiple
methods based upon our requirement combine multiple methods say methods you can sequentially
use also sometimes so earlier we already used here so the concat method we used so concat method
multiple times concat followed by another concat and similarly replace followed by another replace
Okay and like this we can use split followed by another split replace followed by another replace
and sometimes one method followed by another type of method here two lower case method followed
by contains here replace method is followed by contains right all methods applicable so whichever
method you want to combine you can combine but the goal the objective should be clear accordingly you
have to combine the methods blindly you should not combine two methods I will combine repl with
the split method but what is the purpose what is your goal what is output you want to get so
that you should be more clear and accordingly you can just combine the methods okay so this
is all about string related methods these are all built-in methods so we are not implemented
these methods split method how it is working internally and length method how length method
is giving length of a string so that is already implemented by Java itself okay we just trying to
use those methods for our application so we are called them as a buil-in methods those methods
already present in the Stringer class and very very useful and especially in automation we will
use this method Suppose there is a web page and every web page is having some title so we want
to check the title is correct or not according to my test case I want to validate the title of the
web page so how can we do it once we capture the title we will compare the title with our expected
title the comparison happen so how we can do the comparison by using equals or equal ignore case
we can do the combine similarly contains method where frequently used substring also very
frequently used so very very useful methods especially in automation also and some more
methods are there data conversion methods are there so in the coming sessions I will discuss
about them so these are all purely related to Strings strings means these methods are applicable
for only string data type remember this okay L the conad you should not apply this method for integer
variables or any other data type variables these these methods are applicable only for string
type of data that is the reason we call them as a string Methods okay for other data types we
have a different type of methods are there and these methods are especially for string data
type right so practice this for today just do one round of practice very simple and tomorrow we
will continue this class tomorrow I will show you some more examples related to the strings we will
combine the both the topics arrays and strings by combining these two topics we will practice
some more examples so tomorrow I will show you and also I will explain about the difference
between double equal to and equals method and if you create a string like this or if you create
a a string like this what is the difference and when you compare the strings by using double equal
to by using equals method what is the difference when we have to use equals method when you have
to use double equal to Method okay so that I will show you in the tomorrow session so tomorrow
session is also most important we will discuss some more examples based upon arrays and strings
concept and how to reverse a string which is very popular example if I give one string we have to
reverse it okay so we will see that in the next session okay so we'll stop here for today's
session we will continue in the next session
Get free YouTube transcripts with timestamps, translation, and download options.
Transcript content is sourced from YouTube's auto-generated captions or AI transcription. All video content belongs to the original creators. Terms of Service · DMCA Contact