returns the last word of the string, using separators space ,;!?:

image_pdfimage_print
   
 
//CruiseControl is open source software and is developed and maintained by a group of dedicated volunteers. 
//CruiseControl is distributed under a BSD-style license.
//http://cruisecontrol.sourceforge.net/
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;

namespace ThoughtWorks.CruiseControl.Core.Util
{
    /// <summary>
    /// Class with handy stirng routines
    /// </summary>
    public class StringUtil
    {
        /// <summary>
        /// checks if a string is null or empty or is made only of spaces 
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public static bool IsWhitespace(string input)
        {
            if (string.IsNullOrEmpty(input))
            {
                return true;
            }
            else
            {
                // Check each character to see if it is whitespace or not
                var isWhiteSpace = true;
                for (var loop = 0; loop < input.Length; loop++)
                {
                    if (!char.IsWhiteSpace(input&#91;loop&#93;))
                    {
                        isWhiteSpace = false;
                        break;
                    }
                }

                return isWhiteSpace;
            }
        }

        /// <summary>
        /// returns the last word of the string, using separators space ,;!?:
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public static string LastWord(string input)
        {
            return LastWord(input, " .,;!?:");
        }

        /// <summary>
        /// returns the last word of the string, using the specified separators 
        /// </summary>
        /// <param name="input"></param>
        /// <param name="separators"></param>
        /// <returns></returns>
        public static string LastWord(string input, string separators)
        {
            if (input == null)
                return null;

            string[] tokens = input.Split(separators.ToCharArray());
            for (int i = tokens.Length - 1; i >= 0; i--)
            {
                if (IsWhitespace(tokens[i]) == false)
                    return tokens[i].Trim();
            }

            return String.Empty;
        }
   }
}

   
     


This entry was posted in Data Types. Bookmark the permalink.