Helper class to split a long word into a single one. : String Split « Data Types « C# / C Sharp






Helper class to split a long word into a single one.

    


/*
 * Author: Kishore Reddy
 * Url: http://commonlibrarynet.codeplex.com/
 * Title: CommonLibrary.NET
 * Copyright: ? 2009 Kishore Reddy
 * License: LGPL License
 * LicenseUrl: http://commonlibrarynet.codeplex.com/license
 * Description: A C# based .NET 3.5 Open-Source collection of reusable components.
 * Usage: Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
using System;
using System.Collections.Generic;
using System.Text;



namespace GenericCode
{
    /// <summary>
    /// Helper class to split a long word into a single one.
    /// Alternative to possibly using Regular expression.
    /// </summary>
    public class TextSplitter
    {
        /// <summary>
        /// Determine how many times the word has to be split.
        /// </summary>
        /// <param name="wordLength"></param>
        /// <param name="maxCharsInWord"></param>
        /// <returns></returns>
        internal static int GetNumberOfTimesToSplit(int wordLength, int maxCharsInWord)
        {
            // Validate.
            if (wordLength <= maxCharsInWord) return 0;

            // Now calc.
            int splitCount = wordLength / maxCharsInWord;
            int leftOver = wordLength % maxCharsInWord;

            if (leftOver > 0) splitCount++;

            return splitCount;
        }
        /// <summary>
        /// Split the word, N number of times.
        /// </summary>
        /// <param name="word">The text to split.</param>
        /// <param name="charsPerWord">40 chars in each word.</param>
        /// <param name="spacer">" "</param>
        /// <returns></returns>
        internal static string SplitWord(string text, int charsPerWord, string spacer)
        {
            // Validate.
            if (string.IsNullOrEmpty(text)) { return text; }

            // Determine how many times we have to split.
            int splitCount = GetNumberOfTimesToSplit(text.Length, charsPerWord);

            // Validate.
            if (splitCount == 0) return text;

            // Use buffer instead of string concatenation.
            StringBuilder buffer = new StringBuilder();
            int currentPosition = 0;

            // Split N number of times.
            for (int count = 1; count <= splitCount; count++)
            {
                string word = (count < splitCount) ? text.Substring(currentPosition, charsPerWord) : text.Substring(currentPosition);

                buffer.Append(word);

                // Condition to prevent adding spacer at the end.
                // This is to leave the supplied text the same except for splitting ofcourse.
                if (count < splitCount) buffer.Append(spacer);

                // Move to next split start position.
                currentPosition += charsPerWord;
            }

            return buffer.ToString();
        }
        /// <summary>
        /// Check the single line of text for long word that exceeds the
        /// maximum allowed.
        /// If found, splits the word.
        /// </summary>
        /// <param name="text"></param>
        /// <param name="maxCharsInWord"></param>
        /// <returns></returns>
        public static string CheckAndSplitText(string text, int maxCharsInWord)
        {
            // Validate.
            if (string.IsNullOrEmpty(text)) return text;

            bool isSpacerNewLine = false;
            int currentPosition = 0;
            int ndxSpace = GetIndexOfSpacer(text, currentPosition, ref isSpacerNewLine);

            // Case 1: Single long word.
            if (ndxSpace < 0 && text.Length > maxCharsInWord) return SplitWord(text, maxCharsInWord, " ");

            StringBuilder buffer = new StringBuilder();

            // Now go through all the text and check word and split.
            while ((currentPosition < text.Length && ndxSpace > 0))
            {
                //Lenght of word 
                int wordLength = ndxSpace - (currentPosition);
                string currentWord = text.Substring(currentPosition, wordLength);
                string spacer = isSpacerNewLine ? Environment.NewLine : " ";

                if (wordLength > maxCharsInWord)
                {
                    string splitWord = SplitWord(currentWord, maxCharsInWord, " ");
                    buffer.Append(splitWord + spacer);
                }
                else
                {
                    buffer.Append(currentWord + spacer);
                }

                currentPosition = (isSpacerNewLine) ? ndxSpace + 2 : ndxSpace + 1;
                ndxSpace = GetIndexOfSpacer(text, (currentPosition), ref isSpacerNewLine);
            }

            // Final check.. no space found but check complete length now.
            if (currentPosition < text.Length && ndxSpace < 0)
            {
                //Lenght of word 
                int wordLength = (text.Length) - currentPosition;
                string currentWord = text.Substring(currentPosition, wordLength);
                string spacer = isSpacerNewLine ? Environment.NewLine : " ";

                if (wordLength > maxCharsInWord)
                {
                    string splitWord = SplitWord(currentWord, maxCharsInWord, " ");
                    buffer.Append(splitWord);
                }
                else
                {
                    buffer.Append(currentWord);
                }
            }
            return buffer.ToString();
        }
        /// <summary>
        /// Get the index of a spacer ( space" " or newline )
        /// </summary>
        /// <param name="txt"></param>
        /// <param name="currentPosition"></param>
        /// <returns></returns>
        public static int GetIndexOfSpacer(string txt, int currentPosition, ref bool isNewLine)
        {
            // Take the first spacer that you find. it could be eithr
            // space or newline, if space is before the newline take space
            // otherwise newline.            
            int ndxSpace = txt.IndexOf(" ", currentPosition);
            int ndxNewLine = txt.IndexOf(Environment.NewLine, currentPosition);
            bool hasSpace = ndxSpace > -1;
            bool hasNewLine = ndxNewLine > -1;
            isNewLine = false;

            // Found both space and newline.
            if (hasSpace && hasNewLine)
            {
                if (ndxSpace < ndxNewLine) { return ndxSpace; }
                isNewLine = true;
                return ndxNewLine;
            }

            // Found space only.
            if (hasSpace && !hasNewLine) { return ndxSpace; }

            // Found newline only.
            if (!hasSpace && hasNewLine) { isNewLine = true; return ndxNewLine; }

            // no space or newline.
            return -1;
        }
    }
}

   
    
    
    
  








Related examples in the same category

1.Split with space
2.Split with a list of dividers
3.Split spaces with space
4.Show string in proper case
5.use the Split() method to split strings
6.Splitting StringsSplitting Strings
7.Split and join stringsSplit and join strings
8.Tokenize stringsTokenize strings
9.Char String foreachChar String foreach
10.Split a string delimited by another string and return 2 non-empty elements
11.Split a string delimited by another string and return 2 elements
12.Split the original string at the delimiter and return all non-empty elements
13.Split a string delimited by another string and return all elements
14.Split a string delimited by characters and return all elements
15.Split a string delimited by characters and return 2 non-empty elements
16.Split a string delimited by characters and return 2 elements
17.Split a string delimited by characters and return all non-empty elements
18.Use StringSplitOptions enumeration to include or exclude substrings generated by the Split method.
19.Implements a StringTokenizer class for splitting a string into substrings using a set of delimiters.
20.String Tokenizer
21.Can parse a string representing a string[] into an actual string[].
22.Converts a collection of tags to a single string representation containing the tags separated by a comma
23.Takes in any string and convert it into a Byte array, suitable for e.g. insertion into a REG_BINARY Registry value.
24.Splits the string into lines.
25.Splits the string into words (all white space is removed).
26.Split Quoted String
27.Splits the string into an array, using the separator.
28.Splits string name into a readable string based on camel casing.