Check string word length - CSharp System

CSharp examples for System:String Parse

Description

Check string word length

Demo Code


using System.Text;
using System.Globalization;
using System.Collections.Specialized;
using System.Collections;
using System;// w  w w.  j a  v a 2s  . c  o  m

public class Main{
        /// <summary>
        /// Check string word length
        /// </summary>
        public static string CheckStringWordLength(
            string originalText, int maxLength )
        {
            if ( originalText == null )
                throw new ArgumentNullException( "originalText",
                    "Unable to execute method without valid text." );

            string[] splitted = originalText.Split( new char[] { ' ' } );
            string ret = "";
            foreach ( string word in splitted )
            {
                if ( word.Length <= maxLength )
                    ret += word + " ";
                else
                {
                    for ( int i = 0; i < word.Length / maxLength; i++ )
                        ret += word.Substring( i * maxLength, maxLength ) + " ";
                } // else
            } // foreach (word, splitted)
            return ret.TrimEnd();
        } // CheckStringWordLength(originalText, maxLength)
}

Related Tutorials