From the course: Debugging in C#

Unlock the full course today

Join today to access over 23,200 courses taught by industry experts.

Solution: Find the bug in string truncation

Solution: Find the bug in string truncation

From the course: Debugging in C#

Solution: Find the bug in string truncation

(upbeat music) - [Instructor] Let's find the bug in this truncate function. In the code, we access the string and run the substring method on it. The substring we create runs from index zero to what this Math.Max expression returns. It will return either the length of the string or the maxLength value given in the input. Let's see what the code outputs when we run it as is. We'll change the ShowExpectedResult variable to true so we can see what our code is supposed to return. Let's run it. With this execution, the input string was, This is a sample string, and the maxLength was three. This means the string returned should only be Thi, or a string with the characters from index zero to index two in order. Thi is what appears in the expected results, but our code returned the full input string. It didn't truncate it. Let's add some print statements so we can see how our code was evaluated given our input values. We'll print out s.Length in the Math.Max expression. (keyboard clattering)…

Contents