[MUSIC]
>> Greetings, everyone and welcome back to
Microsoft's Java for Beginners series.
This is Part 2 of string formatting.
In Part 1,
I described the concepts you'll need to
format strings and here in Part 2,
we're going to show you some of
the methods and the ways that you actually
convert strings and we're
going to show you some examples in action.
We're going to go use a Visual Studio code and some
of its interactive features to highlight what I've built here.
I've built just one example class,
I imported some packages and I've got
some methods that I created to
build the examples based on the scenarios I described in Part 1.
The first one is just simply converting a char to a string.
To do that, you take the character wrapper class
which takes a parameter data objects char,
wraps it into an object
and allows access to a set of methods that you can use.
One of those methods is toString,
so basically just takes a single character char and converts
it to a single character string that you name here.
String to char is very similar,
but in this case,
what we do is we use a charAt which is part of the string class.
Remember, a char parameter data type
only has one character in it,
so you're going to create one character,
a char data type from the string.
If the string is longer than one character,
you need to provide the index number.
In this case, I'm going to take the first character in
my string and convert it to a char parameter data type.
Next up, strings to char arrays.
When you actually create a string object in Java,
it creates an array of chars,
but you don't have access to that.
If you need an accessible array
of chars from a string, this is how you do it.
You can use the toCharArray method of the string class and it
just basically takes the entire string
and copies it into a new char array.
Now, what if you actually want to convert
just part of that string into a char array?
Well that's when you can use the getChar method of string.
The getChar method of string allows for four parameters,
the start of the string that you want to convert,
the end of the string that you want to convert,
the name of the array that you want to convert the string to,
and the index specifying wherein
that new char array you want to start creating elements.
Right now it's zero,
meaning that it's going to create an array of
eight characters in a char parameter data type array.
If I put 10 here,
it's going to create 18 elements array
and eight of those elements will be populated with
the values I provide and the first 10 elements will be empty.
Now getting a little more complicated,
there are times when you want to interact with a stream.
They might have an application that consumes a stream,
so how do you actually pass a stream of char array
to that application for consumption?
First thing you do is you create
a string class chars method
and once you've got that done you can use
mapToObj to conditionally select
the elements that you want to have in
that array and toArray
will just generate an array from that stream,
so this is the way to consume it as well.
The reason why I have toArray here is I'm actually going to use
this little four here to generate a result.
Once again, you just comment these
out or delete them if you want to use this in
production and you just simply return
a stream from this particular object.
Next, let's look at a few different ways that you
can convert string arrays to strings.
In this case, we are consuming
a pretty easy string array which basically
has everything well delivered inside,
there's every value in the string that we want to split is
in quotes and it's separated by commas.
In that case, if you got a really simple array like that,
you can just use the ArraysToString method.
So arrays is the class that you're going to use,
you import Java.util.Arrays for this,
and then you have the toString method which just
basically contracts it into a string for you from an array.
There might also be times when you want to evaluate
each element that you're going to add into your array.
In that case, you can use string builder and here's a similar,
it's the same string array basically,
but in this case, we're going to go
through and we're going to select each one
of those elements as we pass through
the string to decide whether or not we want to
actually create this element inside of the new string.
If you do want to add it,
you would put your conditions here basically,
inside the four and then
if it passes your condition you would do a
StringBuilder.append into the string array that you've created.
Maybe you want to build a string from a string array,
but you actually want to add
some extra things into the new string when you build it.
Now remember when you create a string,
once the string is created,
it can't be changed.
Before you create it,
what you want to do is iterate through the elements inside of
this array and figure out which ones you want to use,
how you want to split it up,
and maybe you want to add something to the beginning and the end.
In that case, use StringJoiner.
In the example I show here,
this is the delimiter that I'm going to use
to separate the different values when I put
them in the new string from the string array and I'm going to add
some square brackets at the beginning and
the end of my new string from the string array.
It's just a way to format things when you're
pulling them out of an array and
add a few things that weren't in the original array.
Next step is another stream example
and this one is pretty straightforward.
Let's say you've been provided with an array
and you want to create a stream and you just
want to basically take everything that's in the array and create
one single string that you're going
to pass in that stream, that's what you do here.
In this case, we are producing a string,
we have a stream using Java.util stream which must be imported,
and we use the collector to build
the string from the string array, pretty straightforward.
Let's go through a couple examples as well of strings so
you get provided with a string and you need
to produce a string array.
In this case, here's the string before it becomes an array, 1,
2, 3, 4, 5,
those are the elements inside,
you just use the String.split commands,
so the string class split command,
it's going to use whatever delimiter you provide,
in this case a comma,
and it's going to produce in this case,
five elements inside of a new array.
Let's say you don't have such a clean string
that you need to produce an array from,
maybe it's got some nonstandard delimiters,
in this case, I've got two spaces,
so each time there are two spaces inside of the string,
we want to create a new element in the array.
Basically, what you do then is you create
a pattern class and you use Pattern.compile and
provide it with a delimiter that you want to use to
decide where the original string gets split up.
It's going to use splitpattern which I
defined here as two spaces,
it's going to say Pattern.compile and it's
going to use a splitpattern value that I have there and
it's going to return an array
basically based on the pattern that I provide.
String, string array, pattern,
I'm going to use a split command now with
a pretty straightforward pattern
to break up that string into an array.
The last example, we're actually going to use
a StringTokenizer to break the string into tokens.
Once again, we've got an array here
but it's not each element that we want to split,
is just separated by a comma and not
quotes as some of our original simpler examples are.
We're going to use a StringTokenizer,
you can create a set of delimiters
and you can break up this string on a per token basis.
Basically, what we're going to do is
create the string tokenizer,
then we go through the elements one by
one and we decide whether or not this is a
token or not that we want to add into
our new array and once we're done,
once has more tokens has been satisfied,
we'll have a new array
based on the tokens and the delimiters that we specify.
That is an overview of how you can actually use code in
Java to split and
create and manipulate and convert and format strings,
chars and char arrays.
I hope that was useful and my name is Brian Benz,
please do check out Part 1 as well where I
cover a summary of
all the different scenarios and why you want to
actually use this code and feel free to use this code.
Enjoy. Folks, so that was Part 2 of formatting strings,
please check out the rest of the topics
in the Microsoft Java for Beginners series.
[MUSIC]