CopyPastehas never been so tasty!

Truncate string in C# - CSharp

by anonymous

  • 0
  • 0
  • 0
66 views

Orignal Source:http://codingvilla.com/truncate-string-in-csharp-single-article-619.aspx

 

Truncate String:

 

Truncate string is used to show the specific part of the string. If you want to truncate the string then you have to specify that substring by giving the length. You have to specify the length of substring.

 

To understand this let us consider an example in which we will make a static class and then define a method which truncates the string. Then we will call that method in the main method and pass a string and length of substring because it takes 2 parameters. And it will return a substring of that size in which we pass to the method.

 

To demonstrate make console application and write the following code.

 

Code:

 

classTruncate_string

    {

staticvoid Main(string[] args)

        {

Console.WriteLine(StringTool.Truncate("Bloood",3));

Console.WriteLine(StringTool.Truncate("whoiam", 3));

Console.ReadLine();

        }

publicstaticclassStringTool

        {

publicstaticstring Truncate(string source, int length)

            {

if (source.Length> length)

                {

source = source.Substring(0, length);

                }

return source;

            }

  

        }

    }

Add A Comment: