CSharp examples for System:String Strip
Chops a line at a word boundary at or before .
using System.Text.RegularExpressions; using System.Text; using System.Linq; using System.Globalization; using System.ComponentModel; using System.Collections.Generic; using System;// ww w .j av a 2 s . c om public class Main{ /// <summary> /// Chops a line at a word boundary at or before <paramref name="maxlength"/>. /// </summary> /// <param name="line">Input text to chop.</param> /// <param name="maxlength">Maximum length of the output string. Final result may be fewer characters than this.</param> /// <param name="wordbreaks">Set of characters used to END words. For pairing characters (e.g. brackets) only include the ending pair character.</param> /// <returns></returns> public static string ChopToWord(this string line, int maxlength, params char[] wordbreaks) { if (string.IsNullOrEmpty(line)) return line; if ((wordbreaks == null) || (wordbreaks.Length == 0)) wordbreaks = WordBreakChars; if (line.Length <= maxlength) return line; var idx = line.LastIndexOfAny(wordbreaks, maxlength - 1); if (idx < 0) idx = maxlength; else ++idx; return line.Substring(0, idx); } /// <summary> /// Checks if the string is null or empty or just a bunch of worthless whitespace. /// </summary> /// <remarks>This works because extension methods can be called on null references.</remarks> /// <param name="self"></param> /// <returns></returns> public static bool IsNullOrEmpty(this string self) { if (self == null) return true; if (self.Length == 0) return true; // NOTE: This is the kicker over String.IsNullOrEmpty() return self.Trim().Length == 0; } }