//Microsoft Public License (Ms-PL) //http://visualizer.codeplex.com/license using System; using System.Collections.Generic; namespace Redwerb.BizArk.Core.StringExt { /// <summary> /// Provides extension methods for strings. /// </summary> public static class StringExt { /// <summary> /// Shortcut for string.Format. /// </summary> /// <param name="str"></param> /// <param name="args"></param> /// <returns></returns> public static string Format(this string str, params object[] args) { if (str == null) return null; return string.Format(str, args); } } }
Data Types
Truncates the string from left side
//Microsoft Public License (Ms-PL)
//http://visualizer.codeplex.com/license
using System;
using System.Collections.Generic;
namespace Redwerb.BizArk.Core.StringExt
{
///
///
public static class StringExt
{
///
///
///
///
///
public static string Left(this string str, int length)
{
if (str == null) return null;
if (str.Length <= length) return str;
return str.Substring(0, length);
}
}
}
[/csharp]
Gets the right side of the string.
//Microsoft Public License (Ms-PL)
//http://visualizer.codeplex.com/license
using System;
using System.Collections.Generic;
namespace Redwerb.BizArk.Core.StringExt
{
///
///
public static class StringExt
{
///
///
///
///
///
public static string Right(this string str, int length)
{
if (str == null) return null;
if (str.Length <= length) return str;
return str.Substring(str.Length - length);
}
}
}
[/csharp]
Determines if a string consists of all valid ASCII values.
//Microsoft Public License (Ms-PL) //http://visualizer.codeplex.com/license using System; using System.Collections.Generic; namespace Redwerb.BizArk.Core.StringExt { /// <summary> /// Provides extension methods for strings. /// </summary> public static class StringExt { /// <summary> /// Determines if a string consists of all valid ASCII values. /// </summary> /// <param name="str"></param> /// <returns></returns> public static bool IsAscii(this string str) { if (string.IsNullOrEmpty(str)) return true; foreach (var ch in str) if ((int)ch > 127) return false; return true; } } }
Splits the string into words (all white space is removed).
//Microsoft Public License (Ms-PL) //http://visualizer.codeplex.com/license using System; using System.Collections.Generic; namespace Redwerb.BizArk.Core.StringExt { /// <summary> /// Provides extension methods for strings. /// </summary> public static class StringExt { /// <summary> /// Splits the string into words (all white space is removed). /// </summary> /// <param name="str"></param> /// <returns></returns> public static string[] Words(this string str) { return str.Split(new char[] { }, StringSplitOptions.RemoveEmptyEntries); } } }
Splits the string into lines.
//Microsoft Public License (Ms-PL) //http://visualizer.codeplex.com/license using System; using System.Collections.Generic; namespace Redwerb.BizArk.Core.StringExt { /// <summary> /// Provides extension methods for strings. /// </summary> public static class StringExt { /// <summary> /// Splits the string into lines. /// </summary> /// <param name="str"></param> /// <returns></returns> public static string[] Lines(this string str) { var lines = new List<string>(); using (var sr = new System.IO.StringReader(str)) { string line = sr.ReadLine(); while (line != null) { lines.Add(line); line = sr.ReadLine(); } } return lines.ToArray(); } } }
Forces the string to word wrap so that each line doesn't exceed the maxLineLength.
//Microsoft Public License (Ms-PL) //http://visualizer.codeplex.com/license using System; using System.Collections.Generic; namespace Redwerb.BizArk.Core.StringExt { /// <summary> /// Provides extension methods for strings. /// </summary> public static class StringExt { /// <summary> /// Forces the string to word wrap so that each line doesn't exceed the maxLineLength. /// </summary> /// <param name="str">The string to wrap.</param> /// <param name="maxLength">The maximum number of characters per line.</param> /// <returns></returns> public static string Wrap(this string str, int maxLength) { return Wrap(str, maxLength, ""); } /// <summary> /// Forces the string to word wrap so that each line doesn't exceed the maxLineLength. /// </summary> /// <param name="str">The string to wrap.</param> /// <param name="maxLength">The maximum number of characters per line.</param> /// <param name="prefix">Adds this string to the beginning of each line.</param> /// <returns></returns> public static string Wrap(this string str, int maxLength, string prefix) { if (string.IsNullOrEmpty(str)) return ""; if (maxLength <= 0) return prefix + str; var lines = new List<string>(); // breaking the string into lines makes it easier to process. foreach (string line in str.Split(" ".ToCharArray())) { var remainingLine = line.Trim(); do { var newLine = GetLine(remainingLine, maxLength - prefix.Length); lines.Add(newLine); remainingLine = remainingLine.Substring(newLine.Length).Trim(); // Keep iterating as int as we've got words remaining // in the line. } while (remainingLine.Length > 0); } return string.Join(Environment.NewLine + prefix, lines.ToArray()); } private static string GetLine(string str, int maxLength) { // The string is less than the max length so just return it. if (str.Length <= maxLength) return str; // Search backwords in the string for a whitespace char // starting with the char one after the maximum length // (if the next char is a whitespace, the last word fits). for (int i = maxLength; i >= 0; i--) { if (char.IsWhiteSpace(str[i])) return str.Substring(0, i).TrimEnd(); } // No whitespace chars, just break the word at the maxlength. return str.Substring(0, maxLength); } } }