Code Simplification: String Maximum Length

I was looking at some old code and ran across something that struck a chord of wrong with me.

string temp; 
temp = "There can only be one!";
if (temp.length > 15) temp = temp.substring(0, 15);

Then I thought we could use a little math to handle the problem:

string temp; 
temp = "There can only be one!";
temp = temp.substring(0, Math.Min(15, temp.Length));

I think Microsoft should have included this kind of method to the String class. Hope this helps.

2 thoughts on “Code Simplification: String Maximum Length

  1. Hello would you mind letting me know which web host you’re using? I’ve loaded your web site in 3
    different internet browsers and I will have to say this website loads a lot faster then most.
    Can you suggest a good hosting company at a reasonable price?
    Many thanks, I appreciate it! Plz also excuse if my english is not good enough.

    • I use WordPress hosted. It is an easy to use and makes posting much easier than some of the others I have used. Your English is quite good so no worries.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s