Strings

Learn the Basic Java Concepts Working with variables and operators
15 minutes
Share the link to this page
Copied
  Completed
You need to have access to the item to view this lesson.
One-time Fee
$69.99
List Price:  $99.99
You save:  $30
€65.24
List Price:  €93.20
You save:  €27.96
£55.93
List Price:  £79.91
You save:  £23.97
CA$95.58
List Price:  CA$136.56
You save:  CA$40.97
A$107.13
List Price:  A$153.05
You save:  A$45.92
S$95.25
List Price:  S$136.08
You save:  S$40.82
HK$547.90
List Price:  HK$782.75
You save:  HK$234.85
CHF 63.85
List Price:  CHF 91.21
You save:  CHF 27.36
NOK kr770.13
List Price:  NOK kr1,100.23
You save:  NOK kr330.10
DKK kr486.51
List Price:  DKK kr695.05
You save:  DKK kr208.53
NZ$117.65
List Price:  NZ$168.07
You save:  NZ$50.42
د.إ257.06
List Price:  د.إ367.25
You save:  د.إ110.18
৳7,679.19
List Price:  ৳10,970.75
You save:  ৳3,291.55
₹5,834.32
List Price:  ₹8,335.10
You save:  ₹2,500.78
RM333.81
List Price:  RM476.90
You save:  RM143.08
₦91,422.33
List Price:  ₦130,608.93
You save:  ₦39,186.60
₨19,487.31
List Price:  ₨27,840.21
You save:  ₨8,352.89
฿2,585.90
List Price:  ฿3,694.31
You save:  ฿1,108.40
₺2,277.50
List Price:  ₺3,253.72
You save:  ₺976.21
B$361.10
List Price:  B$515.88
You save:  B$154.78
R1,320.17
List Price:  R1,886.05
You save:  R565.87
Лв127.48
List Price:  Лв182.13
You save:  Лв54.64
₩96,293.43
List Price:  ₩137,567.94
You save:  ₩41,274.51
₪266.53
List Price:  ₪380.78
You save:  ₪114.24
₱4,038.21
List Price:  ₱5,769.12
You save:  ₱1,730.90
¥10,975.59
List Price:  ¥15,680.08
You save:  ¥4,704.49
MX$1,206.90
List Price:  MX$1,724.22
You save:  MX$517.31
QR255.26
List Price:  QR364.68
You save:  QR109.41
P965.80
List Price:  P1,379.77
You save:  P413.97
KSh9,448.65
List Price:  KSh13,498.65
You save:  KSh4,050
E£3,352.63
List Price:  E£4,789.68
You save:  E£1,437.04
ብር3,985.10
List Price:  ብር5,693.24
You save:  ብር1,708.14
Kz58,489.70
List Price:  Kz83,560.30
You save:  Kz25,070.60
CLP$66,385.51
List Price:  CLP$94,840.51
You save:  CLP$28,455
CN¥507.19
List Price:  CN¥724.59
You save:  CN¥217.40
RD$4,111.01
List Price:  RD$5,873.12
You save:  RD$1,762.11
DA9,398.61
List Price:  DA13,427.17
You save:  DA4,028.55
FJ$158.17
List Price:  FJ$225.97
You save:  FJ$67.79
Q544.39
List Price:  Q777.73
You save:  Q233.34
GY$14,639.41
List Price:  GY$20,914.34
You save:  GY$6,274.92
ISK kr9,791.60
List Price:  ISK kr13,988.60
You save:  ISK kr4,197
DH708.11
List Price:  DH1,011.63
You save:  DH303.52
L1,243.72
List Price:  L1,776.81
You save:  L533.09
ден4,016.98
List Price:  ден5,738.79
You save:  ден1,721.81
MOP$563.94
List Price:  MOP$805.67
You save:  MOP$241.72
N$1,340.22
List Price:  N$1,914.68
You save:  N$574.46
C$2,575.16
List Price:  C$3,678.96
You save:  C$1,103.79
रु9,316.74
List Price:  रु13,310.19
You save:  रु3,993.45
S/261.07
List Price:  S/372.97
You save:  S/111.90
K269.52
List Price:  K385.04
You save:  K115.52
SAR262.50
List Price:  SAR375.02
You save:  SAR112.51
ZK1,842.03
List Price:  ZK2,631.59
You save:  ZK789.55
L324.63
List Price:  L463.78
You save:  L139.14
Kč1,640.70
List Price:  Kč2,343.96
You save:  Kč703.26
Ft25,605.88
List Price:  Ft36,581.40
You save:  Ft10,975.51
SEK kr762.91
List Price:  SEK kr1,089.92
You save:  SEK kr327.01
ARS$61,153.48
List Price:  ARS$87,365.86
You save:  ARS$26,212.38
Bs484.56
List Price:  Bs692.26
You save:  Bs207.70
COP$275,828.59
List Price:  COP$394,057.74
You save:  COP$118,229.14
₡35,172.98
List Price:  ₡50,249.28
You save:  ₡15,076.29
L1,727.65
List Price:  L2,468.18
You save:  L740.53
₲519,796.34
List Price:  ₲742,598.03
You save:  ₲222,801.68
$U2,684.12
List Price:  $U3,834.62
You save:  $U1,150.50
zł281.95
List Price:  zł402.81
You save:  zł120.85
Already have an account? Log In

Transcript

Hello there, and welcome back to the Java development course. So, last time we went over non numeric data types, so specifically char and Boolean, and you got some homework to do. So your job was to create a program, which has two characters and two Boolean, and then just print on one line first, the first character then the first Boolean, and then the second character and then a second Boolean, here essentially should have done just have two characters, see into two Booleans, true and false. And I just print them on one line with a string plus A plus A one plus people's B two. So pretty simple, essentially should have done. Alright, so let's go ahead and run this application.

And there we go. We get C. So the first character right here a, and then we have true, this is a one, this first Boolean. Then we have two which is the second character. Character Sorry about that. And then we have false, which is the second Boolean v2. There we go, that essentially, we should have done.

Really not difficult at all, personally could be completely honest with you. I couldn't really think of any exercises to do with characters in Billings that would be better than this one. I mean, I, I just don't know, I just don't know what else there could be. So once we start getting into, you know, some more useful sort of programming topics, you can start making more useful applications and the homework will start to get a little bit more fun. But for now, I mean, this is all I could think of. So yeah, without further ado, then let's get into today's lesson.

Alright, so today we're going to be going over strings, which is our final data type. And this is actually not a primitive data type. This is already a complex data type. And we'll go over the definitely what that means in a second here, but first of all, let's go ahead and create a new class called strings. There we go. Go as always public static, void, main and sharp.

Okay, so what is a string? A string is just a collection or a list of characters. So if you want to write a sentence, we would create a string, which is just a collection of characters. So let's go and create a very simple string here. So string a hw, equals, and inside double quotes, hello, world. Alright, there we go.

So we have a string and you'll notice how if you create an int, it is highlighted red. That's because it is a primitive datatype. Since a string is a complex data type, it isn't highlighted at all. So yeah, so yeah, so string, h w, equals and then in double quotes, hello world. So string is actually a very useful part of programming in general, it's how we work with letters. So that's essentially what how we do anything that has to do with, you know, sentences.

We're actually, if we're going to be working with letters, we would actually be doing characters. But if we're doing sentences, then we would do strings. So yeah, so now we can go ahead and print our strings. Our string, I'm sorry, out to print ln, hw All right, there we go. Let's go and run the application like this. are still with us.

We want to do like this. There we go. And we get hello world. So it's not Berlin, h, WH ws, hello world, and so it prints hello world. Okay, so there are quite a bit of things you can do with string. So we can first of all create int a equals 12.

Let's just say and now we can do hello world plus 12. Let's run this. And there we go. So as you can see, it automatically converts it to a string. But if we just have this right here, Then in this case, we get an error. But if we add in the double quotes, it'll automatically convert it to a string.

All right, so now we could also do, stew just just to that would make a little bit more sense. The stock price today is an N 12. There we go. Whoops, there we go. Okay, and so we're actually, you know, not 12 is gonna be a, there we go. Okay, let's go and run this.

And there we go. So the, the stock price today is 12. There we go. And let's see. All right, now, since a string is a complex data type, it means that there are certain things we can do with strings that are built into the actual string itself. So we can find this out by doing the name of the string.

And then putting a dot and eclipse will actually give us a lot of different options for what we can actually do with strings. So we have an equals method a link method and equals ignore case method, and so on and so on. There are a lot of different methods. And you actually take a look about exactly what what they do which eat what each of these do. You know, just to search it up on Google, there's a ton ton of documentation on this. But today, we're going to be going over sort of like the most useful and popular ones.

So first of all, we can get the actual length of string so we can do hw dot length. There we go. So this will just return the length the amount of characters inside the string. There we go. 27. So I'm not going to count it out.

But you know, there's 27 I trust I trust Java. Here. We can also do dot equals dot equals, and then we can do for example, pi, okay, so if we do high, we get false, right? But if we do if we change this string to high There we go. Now we get true since it does equal high. We can also do dogs index of so index of will essentially get the index of where it's located.

So if we do h in here, it will get us zero since H is at zero. If we do I, it'll give us one since eyes out one. Now, you may be wondering why give us 04 h wouldn't actually be the first letter. Yeah, it is actually the first letter, but computers actually start counting from zero and not from one. So that's why we get zero and not one. So you'll see here how h W dot index of gets us an H, I'm sorry, no index of and then inserting a string h inside it will get us the position of H inside strings in this case is going to be the first position or zero so it's character number zero.

And if we put a character inside there, that doesn't exist, it'll give us just negative ones with me that just doesn't exist. Okay, um, Now I'm sure you're wondering at this point exactly what all this is so hw index of what is that? Like, you know, what is that? Is that some kind of, I don't know, maybe crazy programming magic that I'm doing there with the hw dot, well no, actually h w in this case, it's a string, which means essentially, that is just a complex object. And the fact that it's a complex object actually means that it there was there was somebody that set on a computer and wrote a string class that utilized what we went over in Java before. So if you can actually go in and do f3, or a Fennec through fear on a Mac, and as you can see here, so we have our public final class string.

Now, a lot of this is going to be very difficult to understand we didn't go over a lot of this. So a lot of this is going to be some kind of crazy magic to you. But um, it isn't difficult. And what's important to understand generally, is that this is just a class just like we ever class strings. We Have a class string as well. And so in this case, we have a lot of different methods.

So what is a method? Well, well generally going to be going to have a set a completely separate sort of lesson for methods where we're going to go over exactly what they are and how we can create them. But for now, we'll insert understand is that a method, the way it's called, we do the object name, and then a dot and then the method name. And then we can insert some info into the method inside the inside the parentheses. So in this case, we're gonna do h w equals Hi. So you can see we call our object object h, w, then we do dot and then the method name.

And then inside the method, we put in some kind of attribute, in this case, hi. And then the method does, it does what it does. So in this case, it equals actually we'll just do this. And this is written by somebody else. This is just by the crew. Java that, you know, wrote this exactly what this does, again, it's probably going to be pretty confusing.

So other is so again, don't you know get too into this. And if you don't understand Don't worry about it too much. But just understand that in this case equals just checks if it's equal to another string. So yeah, so other than that, then that is essentially how strings work. Now there are a couple of other things we can do for strings, we can do hw dot char at char at, and then we can do zero. So this is going to return the character that is as a that is at a certain point in the string at a certain index in the string, forgot how to talk there.

So in this case, zero is going to return the first letter of the string. So in this case is going to be h. So let's go and run this. And there we go. We get Ah, all right. So, a couple other methods that we can go over, we can do h W dot substring. So let's just actually, let's give us this just some more text.

So The quick brown fox jumped over the lazy dog, I think that's the, you know, placeholder that people usually use. So in this case, this will just get from a specific point in a string to a specific point in a string and it will return just the substring of those two specific points. So let me give an example. Whoops, actually hit my microphone there. Let me give you an example. So I can do from zero to two.

This is going to return back a string from zero so from here that's gonna return H and then e as well. So let's go ahead and run this and there we go. Well, look at that actually doesn't return he since I told you forgot that actually is not inclusive. So in this case, we would have to do three in order to return the this case returns from zero inclusively to E. And then the it doesn't include this one. Yeah, so it's from zero. inclusively to three, not inclusively.

Okay. Then we can also change this to be, this could be three, four from four to four is going to be 1112. No, it's gonna be 1010. There we go. So then that brings the next word so quick. And so we change this to nine.

It Alright, so Okay, so 10 prints, including this one. Okay, so, eight. There we go. So now we get quick, quit, see, without Okay, so before we actually have nine here, the reason that nine and 10 prints the same exact thing is because 10 just includes this space here. So if we actually like This as you can see, there's no space. But then if we add 10 here, there is a space.

Yeah, as you can see right here. Yeah. Okay, so that is the substring method, oh, then we can also do dot replace, so we can do h W dot replace. And then we're just going to insert a character here, T. And then we're going to replace it with a lowercase t. Okay, so now we're in this application. As you can see, right here T, capital T was replaced with regular t. So that is essentially how we do that. We can change this to be q, for example, as well.

So change capital Q to lowercase Q. There we go. As you can see, it's now a lowercase Q. But what if you want to replace more than one letters We can we would have to put a string in here, right? But then in this case we have to do quick. Let's replace it with slow.

Okay? And so now we get the slow brown fox jumped over the lazy dog. Okay, what if we have two words here? Quick. In this case, it'll automatically replace all of it. Okay, so that is essentially all the methods, one of the, those are all the methods of strings that are those are the main methods of string.

So let's go and actually, just to get organized, I'm going to plot them all down hw dot equals no whoops, dot equals th, e quick and then let's put a comment right here. checks if string. Okay, and there we go. So I think that's all the methods. So equals checks of strings are equal index of returns the index of a string, char and returns the character at a specific index. And replace replaces an old char with a new char, length returns the amount of characters in a string.

So again, there are a lot of different, a lot more different attributes of a string. So we can do h w contains, contains, we can do h W dot split, which essentially just returns two separate strings. We can also do h w.is empty as well, which is checks if the string is empty or not. And then we can also do hw dot, let's see what else what else was there to lowercase with which will just you know, completely put everything you know lowercase the entire string. There are a lot of different useful methods with strings. But luckily, if you start if you you know, maybe you need to do something with A string you can go to and actually very easily look it up online.

And there'll be a ton of different sources which will tell you exactly how to do it. So that's, that's, you know, a good thing about strings. Okay, so without further ado, let's say I need some homework. Alright, so for homework, your job is to take two strings, so string a, just signing some random values and then string B, again to assign it some random values. And your job is to combine the strings into a new string. And then and then print that string uppercase.

So with all uppercase letters, so remember to combine strings, we just do string c equals a plus b, just in case you forgot. That's how we combine strings. Okay, so without further ado, then I wish you luck and I'll see you next time.

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.