String Methods - Part 1

Complete Step By Step Java for Testers Java Concepts - Data Types
11 minutes
Share the link to this page
Copied
  Completed
You need to have access to the item to view this lesson.
One-time Fee
$99.99
List Price:  $139.99
You save:  $40
€92.30
List Price:  €129.22
You save:  €36.92
£79.14
List Price:  £110.81
You save:  £31.66
CA$135.92
List Price:  CA$190.30
You save:  CA$54.37
A$153.17
List Price:  A$214.44
You save:  A$61.27
S$134.71
List Price:  S$188.59
You save:  S$53.88
HK$782.26
List Price:  HK$1,095.19
You save:  HK$312.93
CHF 90.56
List Price:  CHF 126.79
You save:  CHF 36.22
NOK kr1,076.34
List Price:  NOK kr1,506.92
You save:  NOK kr430.58
DKK kr688.47
List Price:  DKK kr963.88
You save:  DKK kr275.41
NZ$166.49
List Price:  NZ$233.09
You save:  NZ$66.60
د.إ367.19
List Price:  د.إ514.08
You save:  د.إ146.89
৳11,000.23
List Price:  ৳15,400.77
You save:  ৳4,400.53
₹8,335.05
List Price:  ₹11,669.40
You save:  ₹3,334.35
RM473.25
List Price:  RM662.57
You save:  RM189.32
₦139,656.03
List Price:  ₦195,524.03
You save:  ₦55,868
₨27,872.43
List Price:  ₨39,022.53
You save:  ₨11,150.09
฿3,637.73
List Price:  ฿5,092.97
You save:  ฿1,455.24
₺3,222.80
List Price:  ₺4,512.05
You save:  ₺1,289.25
B$497.67
List Price:  B$696.75
You save:  B$199.08
R1,888.37
List Price:  R2,643.79
You save:  R755.42
Лв180.67
List Price:  Лв252.95
You save:  Лв72.27
₩134,942
List Price:  ₩188,924.20
You save:  ₩53,982.20
₪366.06
List Price:  ₪512.51
You save:  ₪146.44
₱5,625.18
List Price:  ₱7,875.48
You save:  ₱2,250.29
¥15,118.78
List Price:  ¥21,166.90
You save:  ¥6,048.12
MX$1,663.06
List Price:  MX$2,328.35
You save:  MX$665.29
QR365.44
List Price:  QR511.63
You save:  QR146.19
P1,373.02
List Price:  P1,922.28
You save:  P549.26
KSh13,148.68
List Price:  KSh18,408.68
You save:  KSh5,260
E£4,723.53
List Price:  E£6,613.14
You save:  E£1,889.60
ብር5,694.53
List Price:  ብር7,972.57
You save:  ብር2,278.04
Kz83,612.73
List Price:  Kz117,061.17
You save:  Kz33,448.44
CLP$98,024.19
List Price:  CLP$137,237.79
You save:  CLP$39,213.60
CN¥722.49
List Price:  CN¥1,011.52
You save:  CN¥289.02
RD$5,928.03
List Price:  RD$8,299.48
You save:  RD$2,371.45
DA13,469.95
List Price:  DA18,858.47
You save:  DA5,388.52
FJ$225.25
List Price:  FJ$315.36
You save:  FJ$90.11
Q781.84
List Price:  Q1,094.62
You save:  Q312.77
GY$20,987.69
List Price:  GY$29,383.60
You save:  GY$8,395.91
ISK kr13,817.61
List Price:  ISK kr19,345.21
You save:  ISK kr5,527.60
DH1,010.57
List Price:  DH1,414.84
You save:  DH404.26
L1,763.81
List Price:  L2,469.40
You save:  L705.59
ден5,677.01
List Price:  ден7,948.05
You save:  ден2,271.03
MOP$807.66
List Price:  MOP$1,130.76
You save:  MOP$323.09
N$1,895.98
List Price:  N$2,654.45
You save:  N$758.46
C$3,689.21
List Price:  C$5,165.04
You save:  C$1,475.83
रु13,357.88
List Price:  रु18,701.57
You save:  रु5,343.68
S/371.02
List Price:  S/519.45
You save:  S/148.42
K383.60
List Price:  K537.05
You save:  K153.45
SAR375.02
List Price:  SAR525.04
You save:  SAR150.02
ZK2,548.47
List Price:  ZK3,567.96
You save:  ZK1,019.49
L458.83
List Price:  L642.38
You save:  L183.55
Kč2,334.38
List Price:  Kč3,268.22
You save:  Kč933.84
Ft36,425.52
List Price:  Ft50,997.18
You save:  Ft14,571.66
SEK kr1,059.50
List Price:  SEK kr1,483.35
You save:  SEK kr423.84
ARS$85,666.43
List Price:  ARS$119,936.42
You save:  ARS$34,269.99
Bs695.09
List Price:  Bs973.16
You save:  Bs278.06
COP$389,673.99
List Price:  COP$545,559.18
You save:  COP$155,885.18
₡50,509.65
List Price:  ₡70,715.54
You save:  ₡20,205.88
L2,474.33
List Price:  L3,464.17
You save:  L989.83
₲736,638.54
List Price:  ₲1,031,323.43
You save:  ₲294,684.88
$U3,781
List Price:  $U5,293.55
You save:  $U1,512.55
zł398.17
List Price:  zł557.46
You save:  zł159.28
Already have an account? Log In

Transcript

Hello guys, welcome back to the next tutorial. In our last class, we talked about strings and how we declare strings, initialize them, and we talked about the strings are immutable. And in this one, we're going to talk about some inbuilt methods that Java providers to deal with strings to manipulate them and do all those different actions on the strings. So I created a small document, which is not the complete set of inbuilt functions provided by Java. But indeed, it is a very good one, because these are the most used ones, usually. So what I'm thinking to go in this tutorial is to go over these methods and show you them in action.

So you can get detailed understanding of how to use these methods. So instead of just talking about these, what I'm going to do is I'm just going to open my Eclipse and show you these things and have them implemented. So this is a string method class that I just fine and we're just gonna define a string. Let's define a string literal, maybe a string str And just maybe just name it, just give it something. So this is the test string. That should be good enough, right?

This is our test string that we define a string and gave it a reference variable name str, and we have testing. So the first one was length, right? So length, the length method gives the length of the string that we have. So we're gonna just do is just gonna give me the length, right? So see, so and we can just say str dot length. So as soon as guys when we type the name of the variable, str is the name of the variable and then dot write, it gives us all the available methods on the strings, right, all of these.

So we're going to take a look at some of the important ones. So let's first start with length. So I'm going to type and length. Two should just give me return return me the length of the string. So just run the code and it returns me 23 which means the length This thing is cool. So let's take a look at the second one.

What do we have? Second one is cat. So what it does is it returns the character value at the given index number, right? So let's see. Again, let's do the C. So to print all this stuff, and str dot cat ad, so you guys can very easily see that it says, The cat value at the specified index, and it accepts one integer variable, right? Any index.

So what I'm gonna do is I'm just going to provide one index, maybe just randomly two. So let's see what it gives on that second index. So basically, it's not the second index, it's the third index because the index in Java starts from zero. So 012 is the third index and which should be i think i. So let's run the code. And see Yeah, it didn't this the category, this specified index, right.

So let's Move ahead, and let's see the next one. So concat, what it does is it combines the specified string at the end of the string that we're performing the action on. So what it does is if I say str dot concat, it accepts a string, as you can see up here. So any string that I provide here, so let's say this is the append string, right? So just carefully look at this, that I provided a space up there. So what's going to happen is, it's just going to append this string at the end of our personnel string, because that's where we're performing the actions on right.

So let's save it and run the code. And there you go. We see the complete string. This is the test string and this is the appended string. There you go. So let's take a look at the next one.

It says contains right. So what it does is it returns true if characters are found in the string So we're going to look for some characters or sub strings in this particular string using the contains method. And if the string contains that particular character substring, it's going to return true, otherwise false. So let's again do the seaso. And str dot contains, not the content one, contains, contains. And let's see, I'm trying to find is, so I think is there, it's there, right?

It's there in the string. So it should return true. Let's save it and run the code. There you go at a dance true. And let's take one more example to see the false, right. So if I try to find some character, which does not exist, I think si does not exist, right?

There's a substring, basically not the character, right? So it returns false. So that's how we use the contains method. So now let's go to the next one. starts with. What it does is it checks if this string starts with a given prefix.

So basically whichever prefix that we're gonna give in this, right, so let's quickly see it, it's pretty simple. So if I say str dot starts with, if again, a very check, so what our prefix I'm going to give. So since I know that this thing starts with this, if I provide this here, it's going to give me true, right? And let's see that quickly run the code. There you go. The last one is true, right?

And the same thing. If I provide something else, instead of this, I can say, does it start with this? It does not right. So it says gonna give me false up here, right? So this is just a quick way to check if the string starts with whatever you are expecting, right? So that's how it works.

The next one is answer when it exactly is the same. It just sticks if the string ends with your desired property. Instead of To start with, it's just the reverse. So str dot and swift, and let's, we know that it ends with string. So let's first provide string. So it should give us true, right?

And then if we say something else, let's say str dot ends with. Maybe again, let's use s, then it should just give false. The last value is false, right? So that's pretty much it. Right? Great, guys.

Great going. So let's see the next one. The next one is equals. So what it does is it compares the contents of two given strings. All right, so let's see. Let's define few more strings up there.

So let's say str one equals two. Hello. And let's copy this one and define one more and more This is gonna be str two, this is going to be str three. And this is gonna be welcome, right. So what we have is str one is Hello str two is hello and str three is welcome. So the dot equals method, what it does is it compares the contents of two strings.

So let's say str one dot equals five, I say this and I pass in str, what it's gonna do is it's gonna compare the contents of str one and str two and return me true and the contents are equal, and return false or the contents are not equal. So let's run it. And we expected to write because both of them are hello and we got it. So exactly same if I just copy this line and changes here to str three. Now the contents are different because Hello is not equal to welcome. So we expect false here.

And there you go it return false. So that's how the dot equals works it these things can be used a lot in Any if condition when we have programs going on, and these are really helpful. So let's take a look at the next one. The next one is index up. What it does is it returns the index of the given character value or the substring. So let's say I want to find the index of some random arbitrary character that I can think of in this thing.

So if I say C, so right, and I say str dot index of, and let's say I give the character, maybe edge, right? So what I expect for it is first, zero and one. So I expect the indexed one, right. So let's run the code and see Yeah, we got the one right. And let's say if we give something like Q, Q does not exist, right? So we, then it's going to give the index minus one.

There you go minus one because q does not exist in the complete string. So it's going to return the index. Next, if it can find the character, there you go, right? If it doesn't find the character, it just gives minus one, right? So let's take a look at the next one. The next one is, is empty, right?

So let's see what it does. It just checks if this thing is empty. So let's again use the C So, and C str.is empty. So definitely we know that this thing is not empty, it's just going to return false, right? It's false. And if by any chance we have a string that is empty, it's let's say a string is empty.

This is an empty string. This is how we define an empty string. Even the space should not be there. So just make sure that you're not even putting space. So str for is empty. It's going to give true so let's run the code.

And this is true, right? So let's quickly take the next one. With this replace, so what it does is, or maybe just let's take the, the easy one, let's take the trim right now. So what it does is it eliminates leading and trailing spaces, so which is pretty easy. So let's define one more string and str five. And let's say paces are around.

So what I'm gonna do is I'm just gonna give some spaces before and after, right, so it has some extra spaces as soon as we got to do see, so it's just gonna trim this basis str five dot trim, right. So what it does is it's just gonna get rid of the leading and trailing spaces. So let me run the code and you can easily see the front spaces are gone. Let me just add some more. And still it's in the front, right. So there are no spaces In the front or the back, so all the leading and trailing spaces are gone.

So this is our trim box. So I want to keep this one short guys not too much. So these are the remaining ones. Let's have one more lecture lecture to cover these very few. So this is pretty much it for now. Stay tuned for the next one guys and thanks for attending this one.

Sign Up

Share

Share with friends, get 20% off
Invite your friends to LearnDesk learning marketplace. For each purchase they make, you get 20% off (upto $10) on your next purchase.