[MUSIC]
>> Greetings everyone, and welcome back to
Microsoft's Java for Beginner series.
We're going to be talking about formatting strings.
Specifically, we're going to talk about converting strings,
string arrays, and
tokens and some of the methods used to do that.
There's two parts for this topic.
In part 1,
we're going to talk about the concepts,
and in part 2, I'm going to show you some examples.
I'm going to cover different options for
converting chars to strings,
strings to chars,
string to string arrays,
and arrays to strings as well.
The reason why we're going to cover
so many different formats and
so many different options is because in Java,
there are so many different formats and so many different options.
I'm going to present some of
the most common scenarios that
you'll find and how you would actually deal with them,
but there are many more.
Now, one of the reasons why you need to convert
things and not just copy things from one to the other,
is because you're dealing with two different things.
The char data type, first of all,
is a primitive data type where data is stored directly onto
Java behind the scenes with very fixed size.
Also, strings have a fixed size when you create them.
Strings though, are not a primitive data type.
They're actually an object that you can interact with.
But for both of them,
chars and strings,
and string arrays for that matter,
when you create them,
they are fixed in size and you can't change them.
There is a slight exception with something called an array list,
and I'm going to show you how you can write
things to array list later,
where you can actually add or remove elements
after you've created the actual object.
But for strings, chars,
and string arrays, once you've created the object,
you can't change it,
you can't make it a different size,
or add or remove anything from it.
You can copy it and make a new copy and
change that copy when you're making it,
but you can't actually change the object after creation.
Let's dive into some of the ways you can do this.
Char and strings.
Taking a char primitive data type and convert it to a string,
the easiest way to do that is just say return character to string.
Character is a wrapper
that actually goes around the primitive data type.
Remember, char it's just a copy,
a single character to
a string object and return that string object.
Let's say you want to take a string and copy it to a char.
Remember, you can only copy one character of that string.
Here we have the string class,
there's a charAt method,
and in my case, it's zero,
so it's going to take the first letter
in the string, whatever that is.
But if you specify another number here,
it starts at zero and comes to the right and
grabs that letter or character for you,
and puts it into a char.
Let's say, you want to create a array of chars from your string.
You can use the toCharArray method.
What that's going to do is create a char array behind the scenes.
By the way, when you're working with
a string object behind the scenes internally,
it's actually an array of chars,
but you don't have access to that array directly.
You can only interact with the objects
that you've created the string.
If you want to have a char array that
you need to work with or manipulate,
you can do that with a toCharArray method.
It's useful when you're working with
a non-delimited string so you can split the string into
arrays of chars and then you can evaluate
each element in that char array,
and decide what you want to do with it.
Now, let's say instead of copying
the entire string to a char array,
you only want to copy part of the string to a char array.
Well, that's the getChars method of the string class.
Basically there, you've got four parameters;
the first one is the start
of the string that you want to work with,
the end of the string that you want to work with,
the name of the char array that you want to create,
and where in that char array you want to place the elements.
So if I put a 10 here,
it's going to place my chars into the char array starting at 10,
so 0-9 would be empty and
10 and plus would be where
my string chars would go when they're converted.
There's also going to be times when
you need to work with a string.
If you have another application that
consumes strings, for example,
and you want to take a string from a string object,
you can use the chars in the string class.
Basically, what that's going to do is
create a new array of chars.
You can use map to object to decide
what you want to do with each element,
decide as the array is
created if you want to include a character or not,
and then toArray will
actually produce the actual char array for you.
That is a string,
from a string into a char array.
The next section here, we're going to talk about
string arrays to strings.
You've got an array of strings,
you need to convert it into a string.
Simplest way to do that is just with the arrays class to
string method copies the entire string array
into a single string for you.
But let's say you want to conditionally convert
elements in the string array into that new string.
The best way to do that is, you StringBuilder
and append so you can go through,
evaluate each element in that string array,
and copy it to a string if it meets the criteria you need.
Also, if you want to do things like
add things to the new string that aren't part of the elements,
then what you would do is use string joiner.
String joiner allows for three parameters; a delimiter.
It also allows for
something to go at the beginning of the string
and something to go at the end of the string.
When it actually builds this string,
it's going to take all of the elements,
put them together using whatever you specify as the delimiter.
In the case of mine,
I've got three semicolons, and then that's going to put a,
in this case, a square bracket at
the beginning and a square bracket at the end.
Let's say, you are provided with a string,
a string that you need to consume in a stream.
You can also use this particular method to create an array list.
I mentioned that before.
Strings and string arrays are fixed as soon as you create them.
You can't change them once they're created.
Array lists, you can.
You can create an array list and then afterwards
add and remove elements as you need
and then convert it to a stream or a string later.
This is a really simple way to do that as well,
you just create a stream of string array,
use a collector to join all the elements together.
That is string arrays to strings.
Let's talk about strings to string arrays.
Easiest way is just split.
You take a string and you split it based on
the delimiter that you specify
in your parameters here, in this case, a comma.
This would just take everything in
a string and copy it into a new string array,
and each element would be
delimited by a comma in that new string array.
Let's say, you had
a string that is not necessarily so well delimited,
so it's not very clearly delimited.
In this case, I've got two spaces that
separate each element that you want to split into an array.
In that case, you can say split
pattern and use the pattern method
and compile using this as delimiter for each new element.
Well, let's say, there's a scenario where you
have no delimiters provided at all
and you need to figure out how you're going to split that
into elements in a new array.
You can use the StringTokenizer class and
the java.util.StringTokenizer must be imported into that class.
That's just a real quick summary of different scenarios that you
might want to use when you're converting chars,
strings, and string arrays.
Please do check out the next recording
with some provided classes that I created,
and we're going to go through them one-by-one
and explain what each one does.
This is part 1 where we described
some of the concepts, and in part 2,
I'm going to show you some actual examples in action,
so please do check that out as well.
Also, check out the other topics
that we have at Microsoft's Java for Beginner series.
[MUSIC]