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
€67.10
List Price:  €95.86
You save:  €28.76
£55.67
List Price:  £79.54
You save:  £23.86
CA$100.49
List Price:  CA$143.56
You save:  CA$43.07
A$111.96
List Price:  A$159.95
You save:  A$47.99
S$94.87
List Price:  S$135.54
You save:  S$40.66
HK$544.16
List Price:  HK$777.41
You save:  HK$233.24
CHF 62.56
List Price:  CHF 89.37
You save:  CHF 26.81
NOK kr792.29
List Price:  NOK kr1,131.89
You save:  NOK kr339.60
DKK kr500.54
List Price:  DKK kr715.08
You save:  DKK kr214.54
NZ$123.74
List Price:  NZ$176.78
You save:  NZ$53.04
د.إ257.07
List Price:  د.إ367.26
You save:  د.إ110.19
৳8,330.24
List Price:  ৳11,900.85
You save:  ৳3,570.61
₹5,945.56
List Price:  ₹8,494.03
You save:  ₹2,548.46
RM315.51
List Price:  RM450.75
You save:  RM135.24
₦108,149.19
List Price:  ₦154,505.46
You save:  ₦46,356.27
₨19,403.53
List Price:  ₨27,720.51
You save:  ₨8,316.98
฿2,393.75
List Price:  ฿3,419.79
You save:  ฿1,026.04
₺2,454.67
List Price:  ₺3,506.82
You save:  ₺1,052.15
B$425.95
List Price:  B$608.53
You save:  B$182.58
R1,282.09
List Price:  R1,831.63
You save:  R549.54
Лв131.15
List Price:  Лв187.37
You save:  Лв56.21
₩101,234.93
List Price:  ₩144,627.53
You save:  ₩43,392.60
₪254.67
List Price:  ₪363.83
You save:  ₪109.16
₱4,117.86
List Price:  ₱5,882.91
You save:  ₱1,765.05
¥10,949.58
List Price:  ¥15,642.93
You save:  ¥4,693.35
MX$1,405.49
List Price:  MX$2,007.92
You save:  MX$602.43
QR254.12
List Price:  QR363.05
You save:  QR108.92
P963.49
List Price:  P1,376.48
You save:  P412.98
KSh8,999.72
List Price:  KSh12,857.29
You save:  KSh3,857.57
E£3,561.31
List Price:  E£5,087.81
You save:  E£1,526.49
ብር8,689.79
List Price:  ብር12,414.52
You save:  ብር3,724.72
Kz64,250.82
List Price:  Kz91,790.82
You save:  Kz27,540
CLP$69,143.42
List Price:  CLP$98,780.55
You save:  CLP$29,637.13
CN¥510.67
List Price:  CN¥729.56
You save:  CN¥218.89
RD$4,244.94
List Price:  RD$6,064.47
You save:  RD$1,819.52
DA9,440.04
List Price:  DA13,486.35
You save:  DA4,046.31
FJ$162.13
List Price:  FJ$231.62
You save:  FJ$69.49
Q537.12
List Price:  Q767.35
You save:  Q230.22
GY$14,584.29
List Price:  GY$20,835.60
You save:  GY$6,251.30
ISK kr9,693.35
List Price:  ISK kr13,848.23
You save:  ISK kr4,154.88
DH701.59
List Price:  DH1,002.31
You save:  DH300.72
L1,285.64
List Price:  L1,836.70
You save:  L551.06
ден4,127.89
List Price:  ден5,897.23
You save:  ден1,769.34
MOP$558.06
List Price:  MOP$797.27
You save:  MOP$239.20
N$1,283.39
List Price:  N$1,833.49
You save:  N$550.10
C$2,565.21
List Price:  C$3,664.75
You save:  C$1,099.53
रु9,482.30
List Price:  रु13,546.73
You save:  रु4,064.42
S/259.58
List Price:  S/370.84
You save:  S/111.26
K282.68
List Price:  K403.85
You save:  K121.16
SAR262.90
List Price:  SAR375.59
You save:  SAR112.68
ZK1,929.21
List Price:  ZK2,756.13
You save:  ZK826.92
L333.95
List Price:  L477.10
You save:  L143.14
Kč1,686.22
List Price:  Kč2,408.98
You save:  Kč722.76
Ft27,781.83
List Price:  Ft39,690.03
You save:  Ft11,908.20
SEK kr772.17
List Price:  SEK kr1,103.14
You save:  SEK kr330.97
ARS$71,242.69
List Price:  ARS$101,779.64
You save:  ARS$30,536.94
Bs481.71
List Price:  Bs688.19
You save:  Bs206.47
COP$305,135.87
List Price:  COP$435,927.07
You save:  COP$130,791.20
₡35,171.10
List Price:  ₡50,246.58
You save:  ₡15,075.48
L1,769.55
List Price:  L2,528.04
You save:  L758.48
₲543,563.42
List Price:  ₲776,552.46
You save:  ₲232,989.03
$U3,109.25
List Price:  $U4,441.97
You save:  $U1,332.72
zł286.15
List Price:  zł408.81
You save:  zł122.65
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.