In this video we'll be looking at string manipulation in C sharp.
I've already created my project and the name of the project is string manipulation so you may pause
the video here and create your console application project.
Call it string manipulation and then we can pick up from there.
Now today we just want to focus on some of the tips and tricks and quick ways to process strings in
C sharp no strings are an essential part of any programming language they represent.
Well I would say 90 percent of the content that would be passing through your application everything
that is input into your program through the use of a keyboard pretty much comes in as a string.
And it is important to understand the tools that C Sharp has available to us.
Now I've started off by declaring a string variable and I'm calling it full name and I have a sentence
here.
My name is and I have a spelling error is Trevor Williams.
Now we've been doing this for a while now and if not then that's fine to print to screen.
We say console which is a static class given to us by C sharp and then we have the function instead
of that static class called the right line and then we specify the string to be printed.
No this string can be something that we're going to type specifically and F string always comes inside
quotation marks or it can be the contents of a variable that is of type String so full name is off type
string.
And of course you have to make sure we get our cases properly and then this will print the contents
of our variable.
So the console lets us test how that works.
So I'm went to change my start up project string manipulation and start.
And there we go.
Hi my name is Trevor Williams.
So our consulate the red line printed the contents of our variable to our console.
Now let's move on to concatenation.
Now I just declared two more variables one called First Name one called last name and then I would like
to print out one contiguous block of string contiguous is another way of seeing continue was and that
string should have my name is and then have the first name and the last name.
So concatenation is the technique by which we join the contents of strings together.
So I'm going to console dot right line and then I'm going to put in some static string and then this
is going to say my full name is.
So when we say static string that means that unless we come into our code and make an adjustment here
this will never change.
However variables are easier to change because then we can use them to collect user inputs and its contents
would be defined based on what is being assigned to it.
However there are times when we want to dynamically print what's in a variable alongside something that
is static so concatenation allows us to join static text onto variable text or variable takes until
variable takes or even a static text on a static text.
The underlying principle is that there are all strings.
Now there are a few ways to concatenate and the first one that I would want to show you is by using
a curly brace and then we're going to close.
So we have curly brace zero and then curly brace and that and when to put the space car to brace one
current a brace no in effect.
These are what you call place holders and they start counting from zero.
So as many placeholders as you have or you're going to create as many playschool as for as many variables
as you'd like to concatenate into the static string and you start counting from zero 0 zero will be
the first one then one the second and the third one would be two.
But then we only have two variables.
So it's zero and one first name and last name not the space is also important because it will print
but the string exactly how it is formatted inside of the quotation marks.
So after placing our place holders there we have to come out of the string and press comma and then
we put in the variables in the very order to match the place holders as intended.
So I want to say my full name is first name and last name.
So that means I need to put the variable that has the content for the first name followed by the variable
that has the content for the last name.
And then this can happen for as many strings as we have so many as many string variables as we have.
Another way to concatenate and I'll just duplicate this nine I just rewrite it.
So console the right line and rewrite our sentence my full name is and then I can use the plus sign.
So I would write my full name is as my static text press the plus sign and there's a first name and
then a plus sign and then because I would want to add a space between the first name and the last name
values I would have to put another block of string for space and then close up block of string and then
plus sign.
And then last name.
So essentially I'm doing like string mathematics I'm just adding as many strings as I need to to join
them.
And then all of this will be printed out in one continuous line.
So I'm want to get the static text my full name is with that space see that space between the colon
and the end quotation mark.
Then I add the first name and then I have to add another space and then I'm adding the last name the
third way to concatenate after writing console.
Right.
Line is by doing what we call interpolation.
So in this instance we actually start off our string with a dollar sign then we open quotation marks
and then this allows us to use a curly brace and input our variable directly inside of the quotation
marks.
So then I can see my full name is space and then just inject the first name have a space and then inject
last name.
All of this still inside of the quotation marks.
Now let's test all three of these and see that they work the same way there we go.
Three different techniques.
Same sentence my full name is Trevor or Williams.
Notice the species are all there and once again it all depends on how you format your text in the static
parts.
Now let's move on to counting the length of four string.
If I wanted to know how many letters were in my first name then I would need to find the cones off the
contents so remember that I'm putting it in a variable and if we were allowing the user to enter content
into this variable I would not know at any point what the value of first name is however I do want to
know.
I wanted to probably build a program that allowed people to just type in their name and then tell them
your name is X characters long.
So to find the length of a string I would need firstly an integer variable and I'm just gonna call this
int length because I need somewhere to store the length value that I wouldn't just call the string variable
by its name.
So first name then I can see a dot and then I go to the function or property or other length.
So the difference between our function on a property is that you may call functions using the power
indices but then there are times when you have to use the dot operator and when we get into the explanation
of object oriented programming then explain that dot operator a bit more.
But once we see some variable some string variable and then we see a doctor and I just remove the links
and then we control space we can see that we have a host of properties shown by the wrench and function
shown by the cube.
So if I see a dot length it is no getting back the length of whatever string or whatever value this
string variable has.
So I'm winter on this program and I believe my name is seven characters long so I'm going to put that
theory to the test.
I did that prematurely I did not put the console dot right line so console dot right line and we're
writing the value of the variable length and there we go.
So my name is indeed seven characters long.
Please not that space is also code as values.
So if I phoned the length of full name and I'll just duplicate these two lines and then do another print.
But this time I'm going to say I want the length of full name and let me just ceiling one some printing
bringing the value of length one this time and then I'm going to see what but at that use then my sentence
that is in the variable full name is actually thirty one characters long because each I comma space
that's four characters and then all of them add up so everything that is between the quotation marks
will be counted in the calculation of the length C sharp makes it easy for us to replace the contents
are parts of us true but I'm going to explain what I mean by that.
So 1 once a string is cleared then you cannot just run the function and change the value of the string
you have to put an operation like a replacement instead of another variable and then you may use that
variable.
So I'm going to have to declare a new variable string new name and then I'm going to make it equal to
the first name not replace and then replace function takes two characters it takes the old char and
the new charter old char meaning what letter R character or symbol whatever it is that is in the string
that I want to remove.
What did to what is it.
So I want to replace the letter T.
And then I see a comma.
I notice that the char has a single quotation marks.
So char is not double quotation marks like string but it's a single quotation marks.
And then we are replacing the T with let's say a kid.
I've never seen my name but like that or any variation of my name that starts with a K.
But the new name is going to be first name not replace any t with the letter K and then let's just write
that to the console and then see the output of that operation.
All right.
There we go.
So it looked instead of my name on it phoned the T and replaced it with a key.
Now let's try one with some more letters so let's say I wanted to replace all the i's in Williams with
O's someone to just repeat this operation down below.
And then this said I'm doing it on last name.
So last name to replace and I'm replacing I with O and then I'm doing new name one and I'm printing
you name one to the console and let's see what happens.
All right.
So every instance often I got replaced by the new char which is all like I said this can be with any
character say if I wanted to replace the i's with an add sign or a hashtag or a number whatever it is
as long as it can fit in between these quotation marks and it's just one output.
If I put 12 let's see what happens if I put twelve then I'm getting an error seeing too many characters
in character literal.
So once again our character is only singular.
All right so it's either going to be empty or half it's going to be even if it's empty.
I wanted a space I have to put that space but I can't have it with no content or with more than one
characters are.
So I'll leave that out us all.
Now let's move on to comparing strings from time to time.
We probably will want to compare some thing that is entered maybe by user and it's stored in a variable
with some string that we know and expect.
So we want to do a computer or we want to compare two bits of input like maybe when somebody types in
a password the first time and then day after retype it the second time you want to compare the two just
to make sure that they type the same thing both times.
No you can always use an if statement.
We went through if statements or you can say if and I want to use two variables that they have you're
already first name last name so what if I wanted to compare and see if the first name and last name
had the same values.
So I can always say if first name is equivalent to last name so that's an easy way to compare them.
So of course we can see if they are equal and we see a console dot right line.
You have the same name for first and last.
All right.
So telling them that okay you have the same name for first and last else and then I'm going to see you
have a different name for first and last so there's always that option of the if else that's.
That's perfectly fine.
But then sometimes this might be kind of accurate.
So then sometimes you want to say something like string and that's kept up a string and you can actually
read come on a string.
The the C Sharp will see them interchangeably and then you have an option that says compare so you can
see a string dot compare and then you pass in the strings that you want to compare string a and string
B so string a would be first name and that is the one that you want to compare with last name.
So you see there are many ways to do the same operation but then this one is kind of OK.
It's straightforward it's going to see if they are equivalent.
Then do this as do that.
This is going to return an integer.
So I'll have to store it the return value inside of a variable.
So I went to see it result is equal to string and compare first name common last name and then I'm going
to have to do an if statement our own results.
So you can have three potential results from this operation and it can either be equal to zero.
It can be our one or negative one when it is zero.
That means that the two strings are equal.
All right.
So when it's a zero when this operation string compare with two strings returns a zero.
So I can see if result is equivalent to zero then it would evaluate the same thing as first name being
equal to last name.
Then I can print you have the same first name and last name.
Right.
And then I can see else also.
Because then if it's a one or a negative one those simply mean that they're not equal.
But then when it's one it means that the first string is probably bigger or has more characters on the
second string.
All right.
So when it's I want it just means that OK the first string is longer than the second string.
I wouldn't think it's one is the other way our own.
But the essential thing to note is that if you do this approach then results should be equivalent to
zero to certify that the two strings are the same.
All right.
So these are two ways that you can compare strings.
You can use whichever one is appropriate you at the time no.
Previously when you were looking at input and output programs you would see where when you're receiving
a number or you intend to receive something to store into a numeric variable or some variable that is
not already a string that you had to convert from the string to that data type.
Well sometimes you will need to do the reverse.
So you would have seen that in concatenation earlier that you can actually concatenate your string with
almost any data type on it would accept it.
But there are times when you need to convert to string.
So converting to string is really easy though.
So I have an integer variable here already so I'm just going to reuse this variable and naughty if you
have a variable already and you want to change the value you don't have to specify the data type again.
I can just see a result is equal to zero right here to force it to that are one two three four five
six seven eight nine.
All right.
I notice s plus signs.
So I'm going to have to remove those.
Sorry.
All right.
No spaces so no I have a result which is an integer being equal to a value of one two three four five
seven eight nine.
That's one big integer result.
Now if I wanted to sort this in a string I wanted to a string big number is equal to and then I wanted
it to have the contents of result.
Then I will get an arrow simply because it's going to say I cannot convert or implicitly convert an
integer into a string which is fine.
That's fair.
So remember that integers are used for math pretty much.
I wanted to string you won't be doing that kind of math.
So if I wanted to convert my integer into maybe a string for whatever reason I can just look at the
variable that I intend to convert and see Dot I then there's a magical function that's is so strong.
So I just see results dot the string and then it will convert that into a string and then once it's
a string.
It is a less hassle to concatenate because there's not every student on that is going to be so forgiving
that you can just plug it into a sentence and it won't throw an arrow.
All right.
So there are times when you will want to do a conversion.
So I'm just going to print my bank account my bank balance is big number.
All right so my bank balance is big number.
And then that will just print out that sentence with this newly created string.
Now once again once it's a string even though the content looks numeric I cannot go ahead and perform
an operation that says int total maybe I got a one dollar payment today.
So I want to add that one dollar so I to say one plus and then I can just say one plus big number
all right.
That would throw an error because I'm trying to do arithmetic here where I'm adding the number one to
something that isn't all a string.
I cannot do that so I will have to of course just keep it into integer in nature.
And then if I need toto to be a string afterwards then I can convert my seem to string.
No I mean this example with numbers.
But then this is not you need two numbers you can convert char to string you can convert double its
a string you can convert almost anything to string.
There are restrictions on maybe arrays and other collection types.
But the fact is once you see this operation or if you want to know if you have that operation available
to you you can just type your variable out and then use your dot operator and then you can just see
if it is listed there for you in intelligence.
All right.
So I'm just going to run this program and then we're going to look at all of our examples printing to
screen.
All right so just a quick run through we've printed our first sentence which was phoned in the full
name The invariable we looked at the three different types of concatenation one with the place holders
one with plus signs and one with interpolation.
We phoned the length of our first name variable value as well as the length of a sentence.
We did a string replace on all Ts in my name and all I's in my surname.
And then we did a string comparison where both results would have had the fact that it's a different
name for first and last.
After comparing the first name value with the last name value.
And then we did a string conversion for our variable for integer variable that represents our account
balance.
In closing this list I'm just going to see that this is just the touch off the surface C sharp string
manipulation capabilities are very extensive and it would require a much longer explanation and video
to actually go through every single thing that it is capable of.
But that being said I encourage you to experiment and just type all its creates a string variable and
then just use a dot operator to see all of the other functions that are available to you.
So you can just have your dot your variable sorry first name and then you see a dot and then everything
that is here you see the visual studio sorry has an explanation as to what it is for.
All right so I encourage you to just go ahead and experiment.
Just type a sentence and or on each one of these and print to the console and see what the outcome is.