From the course: Learning C#

String parsing - C# Tutorial

From the course: Learning C#

String parsing

- [Instructor] In this example, we're going to see how to parse the contents of a string into native C# data types and again, this is a pretty common operation that programs have to perform. Your program might be given something like numerical data in string format and you need to convert it into an actual number. So let's take a look in the Parsing folder. Let's open the code up. So here in my code, I have a few string variables defined that represent different number types. So I've got an integer, a floating point number, a number with a comma in it for thousands, and then the same thing but with a decimal value. So the different basic data types in C# provide a parse function that you can use to parse a string into that particular type. And you can also specify that certain formats are allowed as well. So let's start off with a simple integer example. Now, the reason I'm going to put this inside a try catch block is because the parse function might throw an exception, which we learned about earlier. So I have to have a try catch handler to catch any exceptions that might happen. So for our first example, I have this integer named targetNum and what I'm going to try to do is call targetNum equals and then I'm going to use the int type .Parse. And I'm going to parse, what is it called? numStr1. And then we'll just simply print out the output. So the parse function's going to try to convert that string into this case an integer and it might throw an exception, so we have to make sure that we catch that and it's just a generic catch handler that's going to catch any exception that happens. So let's go ahead and run this. Let's open up the terminal. And we'll run. Yep, and sure enough, there's the output, the number one, right there, so it worked. So now let's try parsing a floating point number into an integer. The code is pretty much the same. What I'm going to do here is I'm going to tell the parser to allow the floating point format. And to do that, I need to use what's called a NumberStyles class and that is located here in the System.Globalization namespace. So that's why I'm including this up here. So here's how this works. And by the way, you can only do this with a floating point number into an integer if the decimal point value is zero. So that's why I have this here. If this was nonzero, this wouldn't work. So here's what I'm going to do. I'm going to write targetNum is equal to int.Parse. And once again, I'll call it with numStr2 this time. And I'm going to call NumberStyles.Float. And that tells the parser expect to see a decimal point number in here and then just like before, I'll just print out the output. The NumberStyles class lets me specify that expect the number to be in a certain format. So in this case, I've specified a floating point number, but I can also expect other formats. So for example, I have a number with a thousands marker, which in this case is a comma, 'cause I'm in the US. So I'm going to try the same thing. So I'll copy this line and paste it in here and this time I'll try to parse numStr3 but with numStr3, what I'm going to do is instead of calling Float, I'm going to call NumberStyles.AllowThousands. So that tells the parser hey, there might be a comma in there and once again, I will copy and print out the result. And I can combine these styles. So one more time, let's do something for numStr4. And for numStr4, remember, we've got both. So we have a thousands marker and a decimal point. So what I'm going to do is call this with numStr4 and I'm going to pass in NumberStyles.Float and then or, a single vertical bar, NumberStyles.AllowThousands. So now I'm telling the parser hey, you can expect to see a potential thousands marker and a decimal value. So let's go ahead and save and let's run. So I'll save this and then down here in the terminal one more time, we're going to run this. And you can see that in the output, we get the expected results. So remember, we're building integers here, so each of these is an integer and if we scroll back up, what we should be seeing is a one, a two and then a couple of 3,000s, right? So here's our one, here's our two and then 3,000s. So it looks like our operations are successful and we're parsing these pieces of data in strings into an integer number. Now, this isn't just for integers. You could do this with other data types too. So let's go ahead and scroll down. So for example, we can do this with a Boolean. So I can do something like this. I can WriteLine and what I'm going to do is I'm going to write out, and I'm going to use string interpolation for this. I'm going to write out bool.Parse and then I'm going to try to parse the string True. So that should result in a Boolean value that's true and then I can do the same thing, let's try this with a floating point number, so in this case, I'm going to call float.Parse and what I'm going to try to parse instead here is 1.235 and of course, once again, I can format this using my formatting knowledge that we learned earlier. So I'll format this as a two-decimal place fixed point number. So now let's save and let's run again. And you can see that those work as well. So here's our Boolean true, so that worked. And you can see that 1.235 is being rounded up 'cause I specified two decimal places. So that's being rounded up to 1.24. That's how we can parse strings into numbers. There actually is a convenience form of the parse function called TryParse, which combines the exception handling code so that you don't need to handle any errors. You can just check to see if the conversion succeeded. So let's go ahead and try that out. I'm going to have a statement that says succeeded equals and then on the Int32 class, I'm going to call the TryParse function and TryParse, now, here's the thing, it's already returning whether or not it's succeeding, so we have to have some way of getting the value back but we can't use the return value. So what I'm going to do is I'm going to call this on numStr1 and then this is a little bit strange 'cause we have to get the value back somehow. We're going to use a feature of C# called an out parameter, which is a function parameter that is used to return a value from the function call. We're not going to be supplying this value, it's going to be giving it back to us. Now, we're going to learn more about this when we get to the chapter on functions but for the moment, just bear with me as I write this code. And all you need to know is that the function will return the converted value here. I'm going to write the code as targetNum. So out targetNum means that's the return value or another return value in addition to this return value. So let's go ahead and save. And then let's run it again. And you can see that that worked. Here's the value one down here. So this statement is obviously a lot less code than having a try catch handler. Now, before moving on, I would suggest trying some of your own combinations here to get a better feel for how the parsing function works. And of course, you can look in the .NET docs for more information on this. At this link, you can read up on how to parse strings in .NET and I would try out some different formats, different kinds of data types just to get a feel for how the parsing function works.

Contents