/src/Shiloh.Utils/StringExtensions.cs

https://github.com/ChrisEdwards/Fluency · C# · 181 lines · 77 code · 31 blank · 73 comment · 5 complexity · 349c43bb253443a9909e71de14c76d47 MD5 · raw file

  1. // Copyright 2011 Chris Edwards
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. using System;
  15. using System.IO;
  16. using System.Linq;
  17. using System.Text;
  18. using System.Text.RegularExpressions;
  19. using System.Xml.Serialization;
  20. namespace Shiloh.Utils
  21. {
  22. public static class StringExtensions
  23. {
  24. /// <summary>
  25. /// Pads or truncates the string such that is has the specified width. Prepends the string with spaces if it
  26. /// is not long enough, or truncates the right side of the string if it exceeds the specified width.
  27. /// </summary>
  28. /// <param name="input">The input.</param>
  29. /// <param name="width">The width.</param>
  30. /// <returns></returns>
  31. public static string ToFixedWidth( this string input, int width )
  32. {
  33. return input.Left( width ).PadLeft( width );
  34. }
  35. /// <summary>
  36. /// Gets the specified number of leftmost characters from the string. If the string is not as long as the requested
  37. /// number of characters, the string is returned unmodified.
  38. /// </summary>
  39. /// <param name="text">The text.</param>
  40. /// <param name="length">The length.</param>
  41. /// <returns></returns>
  42. public static string Left( this string text, int length )
  43. {
  44. if ( text.Length <= length )
  45. return text;
  46. return text.Substring( 0, length );
  47. }
  48. /// <summary>
  49. /// Surrounds a string with brackets.
  50. /// </summary>
  51. /// <param name="theString">The string.</param>
  52. /// <returns></returns>
  53. public static string WithinBrackets( this string theString )
  54. {
  55. return theString.Within( "[", "]" );
  56. }
  57. /// <summary>
  58. /// Surrounds a string with braces
  59. /// </summary>
  60. /// <param name="theString">The name.</param>
  61. /// <returns></returns>
  62. public static string WithinBraces( this string theString )
  63. {
  64. return theString.Within( "{", "}" );
  65. }
  66. /// <summary>
  67. /// Surrounds a string with opening and closing strings.
  68. /// <code>"Bob".Within( "{", "}" ) = "{Bob}"</code>
  69. /// </summary>
  70. /// <param name="theString">The string.</param>
  71. /// <param name="opening">The opening.</param>
  72. /// <param name="closing">The closing.</param>
  73. /// <returns></returns>
  74. public static string Within( this string theString, string opening, string closing )
  75. {
  76. var toReturn = new StringBuilder();
  77. if ( !theString.StartsWith( opening ) )
  78. toReturn.Append( opening );
  79. toReturn.Append( theString );
  80. if ( !theString.EndsWith( closing ) )
  81. toReturn.Append( closing );
  82. return toReturn.ToString();
  83. }
  84. public static string Format( this string format, params object[] args )
  85. {
  86. return String.Format( format, args );
  87. }
  88. /// <summary>
  89. /// Gets the camelCase version of this text.
  90. /// </summary>
  91. /// <param name="source">The source.</param>
  92. /// <returns></returns>
  93. public static string ToCamelCase( this string source )
  94. {
  95. if (source == String.Empty)
  96. return String.Empty;
  97. var firstChar = source.Substring( 0, 1 );
  98. var restOfString = source.Remove( 0, 1 );
  99. return firstChar.ToLower() + restOfString;
  100. }
  101. public static string ToXml< T >( this T toSerialize, Type contract )
  102. {
  103. Stream xmlData = new MemoryStream();
  104. var serializer = new XmlSerializer( contract );
  105. serializer.Serialize( xmlData, toSerialize );
  106. xmlData.Seek( 0, SeekOrigin.Begin );
  107. return new StreamReader( xmlData ).ReadToEnd();
  108. }
  109. /// <summary>
  110. /// Validates wheter a string is a valid IPv4 address.
  111. /// </summary>
  112. /// <param name="s">The s.</param>
  113. /// <returns>
  114. /// <c>true</c> if the specified string is a valid ip address; otherwise, <c>false</c>.
  115. /// </returns>
  116. public static bool IsValidIpAddress( this string s )
  117. {
  118. return Regex.IsMatch( s,
  119. @"\b(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b" );
  120. }
  121. /// <summary>
  122. /// Strips the double quotes from the string.
  123. /// </summary>
  124. /// <param name="value">The value.</param>
  125. /// <returns></returns>
  126. public static string StripQuotes( this string value )
  127. {
  128. return value.Replace( "\"", "" );
  129. }
  130. /// <summary>
  131. /// Strips any whitespace chars from the string.
  132. /// </summary>
  133. /// <param name="value">The value.</param>
  134. /// <returns></returns>
  135. public static string StripWhitespace( this string value )
  136. {
  137. return Regex.Replace( value, @"\s", "" );
  138. }
  139. /// <summary>
  140. /// Checks if this string value exists in a list of string values.
  141. /// </summary>
  142. /// <param name="value">The value.</param>
  143. /// <param name="expectedValues">The expected values.</param>
  144. /// <returns></returns>
  145. public static bool In( this string value, params string[] expectedValues )
  146. {
  147. return expectedValues.Contains( value );
  148. }
  149. }
  150. }