Session 8- Working with Java Strings | String Methods |… — Transcript

Learn Java string methods, including how to create strings and use built-in methods like length, in this practical Java & Selenium session.

Key Takeaways

  • Java provides many built-in string methods to perform operations on string variables.
  • String variables can be created using literal syntax or by instantiating the String class.
  • The length() method returns the number of characters in a string as an integer.
  • String methods are essential for validations and operations in automation testing.
  • Understanding string methods is foundational before learning custom methods in OOP.

Summary

  • Introduction to Java strings and their importance in programming and automation.
  • Explanation of built-in vs user-defined (custom) methods in Java.
  • Demonstration of creating string variables using two different syntaxes.
  • Detailed explanation of the string length() method to find the number of characters.
  • How to call string methods using dot notation and use auto-suggestions in Eclipse.
  • Storing the return value of string methods in variables and printing results.
  • Mention of other frequently used string methods to be covered in the session.
  • Brief mention of upcoming sessions covering object-oriented programming and user-defined methods.
  • Practical coding demonstration in Eclipse IDE with creation of packages and classes.
  • Focus on string methods relevance in automation testing and interview preparation.

Full Transcript — Download SRT & Markdown

00:04
Speaker A
So in today's session, we are going to learn something about strings, which is a most important topic, and we do different types of operations on strings by using string built-in methods.
00:25
Speaker A
So we have certain methods available in Java, so by using those methods,
00:45
Speaker A
we can perform different types of operations on strings. So actually, there are two kinds of methods in Java: one is built-in methods and user-defined methods, or we can say custom methods, and using different methods, we can create our own methods, and they are again part of the class, and
01:03
Speaker A
there are some built-in methods. Built-in methods in the sense Java has already provided those methods, and
01:25
Speaker A
we have to just use those methods, and we have to know how to use those methods. Okay, so I'll
01:45
Speaker A
tell you a simple example of what a method is and how it will work. For example,
02:07
Speaker A
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
02:27
Speaker A
find the length of a string. For example, I want to find the total number of characters in a string.
02:46
Speaker A
I want to find the total number of characters in a string. So what we can do is, yes, s is a string
03:02
Speaker A
variable, so we have to take the string variable, and this is a variable of type string. String is a
03:21
Speaker A
predefined class in Java. String is a predefined class, so which has some predefined methods
03:40
Speaker A
also. So we can apply all string-related methods on this variable. Okay, so when I create a variable
03:54
Speaker A
with a string type, whatever methods are already available in the string class, we can apply all
04:14
Speaker A
those methods to this variable. Okay, so how to use? Suppose I want to find the length of a string, so take
04:46
Speaker A
this variable dot. We have a method called length like this. So this is called a method, a built-in method
05:07
Speaker A
which is already implemented by Java itself. So s dot length, when I call this method, this will
05:24
Speaker A
return the length of a string. So that we can print by using System.out.println, or we can store that
05:40
Speaker A
length in some other variable, and we can perform some operations on the length. Okay, so this is
05:58
Speaker A
simply called a method, and by using this method, we can perform different types of operations on
06:16
Speaker A
string variables. So there are so many methods. Length is just one of the examples, just
06:33
Speaker A
one method. Similarly, there are so many methods in the string class, and we can apply
06:48
Speaker A
all those methods on the string variable, then we can perform various types of operations. So
07:10
Speaker A
now today's session, we will see what are those methods available and how can we
07:34
Speaker A
use those methods. There are 100,000 thousands of methods, and we can't discuss each
07:53
Speaker A
and every method, but mostly used up to 8 to 10 methods are there. We will use them very
08:05
Speaker A
frequently, even in our automation also, especially when you do validations, we use these methods very
08:24
Speaker A
frequently on strings, and those methods we will discuss today. Okay, very simple, very easy concept,
08:42
Speaker A
and you can easily understand also. Okay, so what is a method? A method will perform a certain task,
08:58
Speaker A
and whatever methods we are discussing here, all those methods belong to the string class, so that
09:10
Speaker A
we can apply all those methods on string variables. Okay, so in the coming sessions in object-oriented
09:30
Speaker A
programming, you will understand in detail what a method is, how to create user-defined methods, how to
09:52
Speaker A
pass parameters, how a method will return the value, all those things in the coming sessions we
10:14
Speaker A
will understand in detail in the OOP concept. I will discuss that, and here these are the
10:35
Speaker A
built-in methods. They are already available; just we have to know how to use those methods. Okay?
11:04
Speaker A
Okay, all right, so let us directly go to Eclipse, and I will show you different types of methods
11:32
Speaker A
and different types of use cases. You can easily understand. Let's go to our project and create a new
11:59
Speaker A
package, and today is day 8. Click on Finish. Take a new class, and I'll name it as StringMethods. Take
12:13
Speaker A
this main method, very important, even from an interview perspective also, most important topic. So let
12:40
Speaker A
listen carefully and focus, right? So the first, let me take one string variable. Let's say string s
12:54
Speaker A
equal to... okay, let's take a string variable. String s equal to,
13:18
Speaker A
because it is a string, we have to keep it inside double quotations: "welcome." So this is my string variable. String s equal to
13:38
Speaker A
"welcome." So I just created a string variable. This is how we can normally create a string variable.
13:56
Speaker A
Okay, in Java, there are other ways also. There we can create a string. So this is one approach. The second
14:11
Speaker A
approach is we can declare a string variable like this: string s equal to new String of, and here
14:24
Speaker A
we can specify the value. This is another way of creating a string variable. Actually, there
14:38
Speaker A
is a difference in this. Later I will discuss what is the difference between these two, but
14:54
Speaker A
mostly normally we use this one: string s equal to "welcome." If you want to create a normal variable,
15:16
Speaker A
you can simply do string s equal to "welcome," and if you want to create a string class object, then
15:31
Speaker A
we will see like this: string s equal to new String of "welcome." These are two different
15:52
Speaker A
notations or two different ways we can create a string variable, but there is some difference
16:07
Speaker A
between these two. When comparing or when I store data in the database,
16:20
Speaker A
I'll show you that later. For now, you can just follow anything, either the first syntax or the second syntax. Both are correct. This one
16:42
Speaker A
is correct, this one is also correct. So let's say when I print this, you'll get the same thing when
17:02
Speaker A
I print s value. Okay, and you will see the value of s is "welcome," so you'll see the same, and even
17:26
Speaker A
if you have this kind of notation, second string s equal to new String, because why we can use new?
17:48
Speaker A
Because String is a predefined class. So now we got the same value. So for both, we are getting the same
18:04
Speaker A
value, but it doesn't mean both are exactly the same. There is some minor difference
18:33
Speaker A
is there. So later we will try to understand the difference, but for now just follow one of the
18:52
Speaker A
syntax. Either you can specify this for string s equal to "welcome," or string s equal to new String
19:10
Speaker A
of "welcome" that we have to put in the brackets. Both are correct. Okay, now let us see how can
19:37
Speaker A
we call the methods on the string. What are the different methods available in string? Okay,
20:00
Speaker A
the first method is what? Length, as we discussed. So length is a method through which we can find
20:21
Speaker A
the length of a string. So that means this particular method will return the length of a string. Length
20:51
Speaker A
of a string means number of characters, number of characters in a string. If you want
21:17
Speaker A
to find the number of characters in a string, so this method we can use. So for example, let
21:38
Speaker A
us say I'm taking one string variable: string s equal to "welcome." Now I want to find the length
22:04
Speaker A
of this particular string. So how can we do it? We can simply say s dot. You can see as soon
22:27
Speaker A
as you put dot, it will show you all the string-related methods. These are all string methods you
22:48
Speaker A
can see. There are so many methods. There are so many methods. So it will show you in auto
23:15
Speaker A
suggestion automatically, and then you can simply type one or two characters, it will automatically
23:47
Speaker A
pop up. You can see length. You can see as soon as I type "Le," it is showing one method: length. This
24:04
Speaker A
is the method. So s dot length. So this particular method will return an integer as a return type. You
24:26
Speaker A
can see the description will automatically show you here. It returns the length of a string. Length
24:43
Speaker A
returns the length of a string. So what is the return type of this method? It is integer. Integer
25:02
Speaker A
is the return type. So we can store this value, the length of a string, in some other variable.
25:30
Speaker A
Let's say int l, and then we can print the value of l. Okay, so this particular method will return
25:59
Speaker A
the length of a string, so I can store that in a variable, then I can print the value of l. This is a
26:20
Speaker A
way we can store the value in a variable, and then we can capture it. Suppose I don't want to use any
26:39
Speaker A
variable, I just want to print, so you don't need this variable. You can put that method directly
27:00
Speaker A
inside this println. Yes, s dot length. So you can directly, so this will return the length of a string printed by
27:24
Speaker A
println method. So we can directly print length.
27:43
Speaker A
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
28:05
Speaker A
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
28:36
Speaker 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
29:15
Speaker A
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
29:39
Speaker A
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
30:00
Speaker A
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
30:20
Speaker A
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
30:33
Speaker A
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
31:00
Speaker A
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
31:24
Speaker A
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
31:53
Speaker A
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
32:18
Speaker A
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
32:44
Speaker A
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
33:16
Speaker A
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
33:39
Speaker A
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
33:52
Speaker A
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
34:16
Speaker A
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
34:39
Speaker A
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
35:11
Speaker A
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
35:46
Speaker A
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
36:07
Speaker A
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
36:27
Speaker A
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
36:47
Speaker A
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
37:09
Speaker A
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
37:19
Speaker A
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
37:44
Speaker A
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
38:04
Speaker A
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
38:30
Speaker A
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
38:51
Speaker A
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
39:11
Speaker A
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
39:32
Speaker A
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
39:54
Speaker A
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
40:18
Speaker A
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
40:34
Speaker A
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
41:01
Speaker A
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
41:28
Speaker A
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
42:01
Speaker A
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
42:28
Speaker A
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
42:54
Speaker A
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
43:27
Speaker A
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
43:52
Speaker A
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
44:10
Speaker A
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
44:31
Speaker A
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
44:45
Speaker 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
45:04
Speaker A
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
45:20
Speaker A
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
45:46
Speaker A
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
46:15
Speaker A
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
46:39
Speaker A
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
47:01
Speaker A
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
47:19
Speaker A
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
47:44
Speaker A
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
48:10
Speaker A
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
48:36
Speaker A
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
48:56
Speaker A
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
49:14
Speaker A
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
49:41
Speaker A
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
49:57
Speaker A
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
50:14
Speaker A
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
50:29
Speaker A
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
50:48
Speaker A
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
51:06
Speaker A
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
51:30
Speaker A
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
51:51
Speaker A
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
52:09
Speaker A
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
52:27
Speaker A
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
52:57
Speaker A
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
53:16
Speaker A
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
53:36
Speaker 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
54:09
Speaker A
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
54:28
Speaker A
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
55:13
Speaker A
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
55:42
Speaker A
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
55:57
Speaker A
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
56:24
Speaker A
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
56:50
Speaker A
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
57:09
Speaker A
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
57:31
Speaker A
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
57:47
Speaker A
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
58:00
Speaker A
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
58:18
Speaker A
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
58:35
Speaker A
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
58:58
Speaker A
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
59:13
Speaker A
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
59:39
Speaker A
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
59:58
Speaker A
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
60:20
Speaker A
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
60:48
Speaker A
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
61:09
Speaker A
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
61:31
Speaker A
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
61:48
Speaker A
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
62:13
Speaker A
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
62:35
Speaker A
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
62:53
Speaker A
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
63:14
Speaker A
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
63:32
Speaker A
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
63:48
Speaker A
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
64:14
Speaker A
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
64:35
Speaker A
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
64:56
Speaker A
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
65:15
Speaker A
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
65:30
Speaker A
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
65:45
Speaker A
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
66:09
Speaker A
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
66:33
Speaker A
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
66:48
Speaker A
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
67:05
Speaker A
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
67:26
Speaker A
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
67:46
Speaker A
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
68:11
Speaker A
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
68:31
Speaker A
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
68:54
Speaker A
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
69:08
Speaker A
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
69:32
Speaker A
directly print but once you get an array you can print values from array by using arrays.
69:38
Speaker A
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
69:58
Speaker A
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
70:16
Speaker A
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
70:36
Speaker A
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
70:54
Speaker A
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
71:16
Speaker A
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
71:55
Speaker 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
72:11
Speaker A
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
72:30
Speaker A
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
72:49
Speaker A
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
73:06
Speaker A
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
73:23
Speaker A
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
73:37
Speaker A
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
74:00
Speaker A
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
74:21
Speaker A
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
74:48
Speaker A
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
75:05
Speaker A
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
75:29
Speaker A
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
75:49
Speaker A
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
76:16
Speaker A
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
76:41
Speaker A
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
76:59
Speaker A
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
77:28
Speaker A
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
77:44
Speaker A
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
78:03
Speaker A
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
78:21
Speaker A
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
78:50
Speaker A
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
79:06
Speaker A
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
79:36
Speaker A
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
79:53
Speaker A
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
80:12
Speaker A
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
80:31
Speaker A
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
81:08
Speaker A
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
81:37
Speaker A
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
82:00
Speaker A
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
82:25
Speaker A
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
82:38
Speaker A
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
82:54
Speaker A
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
83:09
Speaker A
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
83:29
Speaker A
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
83:44
Speaker A
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
84:05
Speaker A
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
84:24
Speaker A
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
84:39
Speaker A
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
85:02
Speaker A
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
85:25
Speaker A
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
85:46
Speaker A
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
86:11
Speaker A
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
86:31
Speaker A
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
86:49
Speaker A
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
87:08
Speaker A
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
87:21
Speaker A
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
87:43
Speaker A
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
88:00
Speaker A
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
88:20
Speaker A
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
88:49
Speaker A
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
89:06
Speaker A
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
89:24
Speaker A
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
89:44
Speaker A
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
89:58
Speaker A
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
90:12
Speaker A
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
90:28
Speaker A
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
90:48
Speaker A
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
91:06
Speaker A
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
91:37
Speaker A
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
92:00
Speaker A
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
92:27
Speaker A
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
92:47
Speaker A
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
93:09
Speaker A
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
93:29
Speaker A
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
93:49
Speaker A
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
94:12
Speaker A
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
94:33
Speaker A
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
94:51
Speaker A
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
95:10
Speaker A
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
95:30
Speaker A
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
95:51
Speaker A
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
96:08
Speaker A
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
96:27
Speaker A
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
96:46
Speaker A
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
97:05
Speaker A
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
97:23
Speaker A
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
97:40
Speaker A
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
97:54
Speaker A
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
Topics:Java stringsstring methodsJava built-in methodslength methodJava programmingSelenium automationstring variable creationJava OOP basicsSDET QA trainingEclipse IDE Java

Get More with the Söz AI App

Transcribe recordings, audio files, and YouTube videos — with AI summaries, speaker detection, and unlimited transcriptions.

Or transcribe another YouTube video here →