Truncates a string to the specified length - CSharp System

CSharp examples for System:String Shorten

Description

Truncates a string to the specified length

Demo Code

// Copyright (c) 2012 Computer Technology Solutions, Inc.  ALL RIGHTS RESERVED
using System.Text.RegularExpressions;
using System.Text;
using System.Linq;
using System.Globalization;
using System.Collections.Generic;
using System;/*from w  w  w . j a v a2s  .  c o m*/

public class Main{
        /// <summary>
        /// Truncates a string to the specified length
        /// </summary>
        /// <param name="str">String to truncate</param>
        /// <param name="length">Resulting maximum string length</param>
        /// <returns>String truncated to the specified length</returns>
        public static string Truncate(this string str, int length)
        {
            return (str == null ? string.Empty : str.Length > length ? str.Substring(0, length) : str);
        }
}

Related Tutorials