Project:C#/VB - Automated WebSpider / WebRobot
Author:David Cruwys
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
using System;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
namespace Mf.Util
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// Summary description for StrUtil.
/// </summary>
public class StrUtil
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary> Class cannot be instantiated</summary>
private StrUtil()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
Left#region Left
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// Gets the leftmost n characters of a String. If n characters are not
/// available, or the String is <code>null</code>, the String will be
/// returned without an exception.
/// </summary>
/// <param name="str">The String to get the leftmost characters from</param>
/// <param name="int">The length of the required String</param>
/// <returns>The leftmost characters</returns>
public static string Left(string str, int len)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (len < 0 || str == null || str.Length <= len )
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return str;
}
else
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return str.Substring(0, len);
}
}
#endregion
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
LeftIndexOf#region LeftIndexOf
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// Gets the left most characters starting from the index of <var>search</var>
/// <br/>
/// If <var>str</var> is <code>null</code>, then <var>str</var> will be returned without an exception.
/// <br/><br/>
/// If <var>search</var> is not found then <var>str</var> will be returned without an exception.
/// </summary>
/// <param name="str">The string to get the left most characters off</param>
/// <param name="search">The string to search for</param>
/// <returns>The left most characters</returns>
public static string LeftIndexOf( string str, string search )
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return LeftIndexOf( str, search, 0, false );
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// Gets the left most characters starting from the index of <var>search</var>
/// <br/>
/// If <var>str</var> is <code>null</code>, then <var>str</var> will be returned without an exception.
/// <br/><br/>
/// If <var>search</var> is not found then <var>str</var> will be returned without an exception.
/// </summary>
/// <param name="str">The string to get the left most characters off</param>
/// <param name="search">The string to search for</param>
/// <param name="includeSearchString">If true will include the search string in the result, if false then the search string is excluded</param>
/// <returns>The left most characters</returns>
public static string LeftIndexOf( string str, string search, bool includeSearchString )
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return LeftIndexOf( str, search, 0, includeSearchString );
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// Gets the left most characters starting from the index of <var>search</var>
/// <br/>
/// If <var>str</var> is <code>null</code>, then <var>str</var> will be returned without an exception.
/// <br/><br/>
/// If <var>search</var> is not found then <var>str</var> will be returned without an exception.
/// </summary>
/// <param name="str">The string to get the left most characters off</param>
/// <param name="search">The string to search for</param>
/// <param name="fromIndex">The index to start the search from</param>
/// <returns>The left most characters</returns>
public static string LeftIndexOf( string str, string search, int fromIndex )
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return LeftIndexOf( str, search, fromIndex, false );
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// Gets the left most characters starting from the index of <var>search</var>
/// <br/>
/// If <var>str</var> is <code>null</code>, then <var>str</var> will be returned without an exception.
/// <br/><br/>
/// If <var>search</var> is not found then <var>str</var> will be returned without an exception.
/// </summary>
/// <param name="str">The string to get the left most characters off</param>
/// <param name="search">The string to search for</param>
/// <param name="fromIndex">The index to start the search from</param>
/// <param name="includeSearchString">If true will include the search string in the result, if false then the search string is excluded</param>
/// <returns>The left most characters</returns>
public static string LeftIndexOf( string str, string search, int fromIndex, bool includeSearchString )
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if ( Is.EmptyString( str ) || Is.EmptyString( search ) )
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return str;
}
else
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if ( fromIndex > str.Length )
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return str;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
int pos = str.IndexOf( search, fromIndex );
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
if ( pos == -1 )
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return str;
}
else
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return str.Substring( 0, pos + ( includeSearchString ? search.Length : 0 ) );
}
}
}
#endregion
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
LeftOf#region LeftOf
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// Gets the characters to the left of <var>pos</var>.
/// </summary>
/// <param name="string">The String to get the left most characters from</param>
/// <param name="int">The position to cut from</param>
/// <returns>The left most characters</returns>
public static string LeftOf(string str, int pos)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if ( str == null )
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return null;
}
else
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if ( pos == -1 ) return "";
if ( pos > str.Length ) return str;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
return Substring( str, 0, pos );
}
}
#endregion
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
Right#region Right
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// Gets the right most n characters of a String. If n characters are not
/// available, or the String is <code>null</code>, the String will be
/// returned without an exception.
/// </summary>
/// <param name="str">The String to get the right most characters from</param>
/// <param name="int">The length of the required String</param>
/// <returns>The right most characters</returns>
public static string Right(string str, int len)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (len < 0 || str == null || str.Length <= len )
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return str;
}
else
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return str.Substring( str.Length - len);
}
}
#endregion
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
RightIndexOf#region RightIndexOf
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// Gets the right most characters starting from the index of <var>search</var>.
/// <br/>
/// If <var>str</var> is <code>null</code>, then <var>str</var> will be returned without an exception.
/// <br/><br/>
/// If <var>search</var> is not found then <var>str</var> will be returned without an exception.
/// </summary>
/// <param name="str">The string to get the right most characters off</param>
/// <param name="search">The string to search for</param>
/// <returns>The right most characters</returns>
public static string RightIndexOf( string str, string search )
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return RightIndexOf( str, search, 0, false );
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// Gets the right most characters starting from the index of <var>search</var>
/// <br/>
/// If <var>str</var> is <code>null</code>, then <var>str</var> will be returned without an exception.
/// <br/><br/>
/// If <var>search</var> is not found then <var>str</var> will be returned without an exception.
/// </summary>
/// <param name="str">The string to get the right most characters off</param>
/// <param name="search">The string to search for</param>
/// <param name="includeSearchString">If true will include the search string in the result, if false then the search string is excluded</param>
/// <returns>The right most characters</returns>
public static string RightIndexOf( string str, string search, bool includeSearchString )
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return RightIndexOf( str, search, 0, includeSearchString );
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// Gets the right most characters starting from the index of <var>search</var>
/// <br/>
/// If <var>str</var> is <code>null</code>, then <var>str</var> will be returned without an exception.
/// <br/><br/>
/// If <var>search</var> is not found then <var>str</var> will be returned without an exception.
/// </summary>
/// <param name="str">The string to get the right most characters off</param>
/// <param name="search">The string to search for</param>
/// <param name="fromIndex">The index to start the search from</param>
/// <returns>The right most characters</returns>
public static string RightIndexOf( string str, string search, int fromIndex )
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return RightIndexOf( str, search, fromIndex, false );
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// Gets the right most characters starting from the index of <var>search</var>
/// <br/>
/// If <var>str</var> is <code>null</code>, then <var>str</var> will be returned without an exception.
/// <br/><br/>
/// If <var>search</var> is not found then <var>str</var> will be returned without an exception.
/// </summary>
/// <param name="str">The string to get the right most characters off</param>
/// <param name="search">The string to search for</param>
/// <param name="fromIndex">The index to start the search from</param>
/// <param name="includeSearchString">If true will include the search string in the result, if false then the search string is excluded</param>
/// <returns>The right most characters</returns>
public static string RightIndexOf( string str, string search, int fromIndex, bool includeSearchString )
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if ( Is.EmptyString( str ) || Is.EmptyString( search ) || fromIndex > str.Length )
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return str;
}
else
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
int pos = str.IndexOf( search, fromIndex );
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
if ( pos == -1 )
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return str;
}
else
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return str.Substring( pos + ( includeSearchString ? 0 : search.Length ) );
}
}
}
#endregion
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
RightLastIndexOf#region RightLastIndexOf
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// Gets the right most characters starting from the last index of <var>search</var>.
/// <br/>
/// If the String is <code>null</code>, the String will be returned without an exception.
/// <br/><br/>
/// If <var>search</var> is not found then <var>str</var> will be returned without an exception.
/// </summary>
/// <param name="str">The string to get the right most characters off</param>
/// <param name="search">The last occurence of search to start from</param>
/// <returns>The right most characters</returns>
public static string RightLastIndexOf( string str, string search )
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if ( ( str == null ) || (search == null ) )
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return str;
}
else
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
int pos = str.LastIndexOf( search );
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
if ( pos == -1 )
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return str;
}
else
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return str.Substring( pos + search.Length );
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
#endregion
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
RightOf#region RightOf
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// Gets the characters to the right of <var>pos</var>.
/// </summary>
/// <param name="string">The String to get the right most characters from</param>
/// <param name="int">The position to cut from</param>
/// <returns>The right most characters</returns>
public static string RightOf(string str, int pos)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if ( str == null )
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return null;
}
else
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if ( pos == -1 )
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return str;
}
return Substring( str, pos+1 );
}
}
#endregion
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
Substring#region Substring
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// Gets a substring from the specified string avoiding exceptions from invalid start indexes or null strings.
/// </summary>
/// <param name="str">The String to get the substring from</param>
/// <param name="int">The index of the start of the substring</param>
/// <returns>substring from start position</returns>
public static string Substring(string str, int start)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if ( str == null ) return null;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
if ( start < 0) start = 0; // Start must be 0 or greater
if ( start > str.Length ) return ""; // Start to big
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
return str.Substring( start );
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// Gets a substring from the specified string avoiding exceptions from invalid start indexes, lengths or null strings.
/// </summary>
/// <param name="string">The String to get the substring from</param>
/// <param name="int">The index of the start of the substring</param>
/// <param name="int">The number of characters in the substring. </param>
/// <returns>substring from start position for <var>length</var> characters</returns>
public static string Substring(string str, int start, int length )
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if ( str == null ) return null;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
if ( start < 0 )
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
start = 0; // Start must be 0 or greater
}
if ( length > str.Length-start )
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
length = str.Length-start;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
return str.Substring( start, length );
}
#endregion
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
StartsWith#region StartsWith
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// Similer to String.StartsWith except that it is case-insensitive.
/// </summary>
/// <param name="str">The string to test</param>
/// <param name="startsWith">The string to seek</param>
/// <returns><b>true</b> if value matches the beginning of this string or is Empty; otherwise <b>false</b></returns>
public static bool StartsWith( string str, string startsWith )
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if ( str == null || startsWith == null || startsWith.Length > str.Length )
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return false;
}
// REFACTOR: Would be quicker to test each character in a loop
return str.ToLower( ).StartsWith( startsWith.ToLower( ) );
}
#endregion
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)