Welcome to Kode CSharp

Kode CSharp website provides beginners to C# programming some examples to use the C# API (Application Programming Interface) to develop applications. Learning from some examples will hopefully decrease the time required to learn C#.

In this website you will find a lot of examples which grouped by the C# API namespace. You can easily find a solution to your problem.

Enjoy your study, come and visit the site regularly to find more and more examples of C# code.

--
I Wayan Saryada
Kode CSharp Webmaster

How do I limit number of characters in a string?

By using the StringBuilder object with a defined initial and maximum capacity information we can create a string with a limited number of characters in it. In the code below we set the initial and maximum capacity of the StringBuilder to 20 chars.

using System;
using System.Text;

namespace Kodecsharp.Example.System.Text
{
    class StringLimitLength
    {
        public static void Main(string[] args)
        {
            //
            // Create a string builder with 20 initial and max capacity.
            // This allow us to have a string with maximum 20 chars in
            // length
            //
            StringBuilder builder = new StringBuilder(20, 20);
            builder.Append("ABCDEFGHIJKLMNOPQRST");
            try
            {
                //
                // As this operation can cause exception we need to 
                // catch it.
                //
                builder.Append("U");
            }
            catch (ArgumentOutOfRangeException e)
            {
                Console.WriteLine("Error: " + e.Message);
            }

            Console.WriteLine(builder.ToString());
            Console.ReadLine();
        }
    }
}
Download Hundreds of Complimentary Industry Resources

Get hundreds of popular Industry magazines, white papers, webinars, podcasts, and more; all available at no cost to you. With more than 600 complimentary offers, you'll find plenty of titles to suit your professional interests and needs. Click Here and Sign up today!

Network Sites

Our Friends

Statistics

Locations of visitors to this page