Project:C#/VB - Automated WebSpider / WebRobot
Author:David Cruwys
using System;
namespace Mf.Util
{
/**//// <summary>
/// Summary description for StrUtil.
/// </summary>
public class StrUtil
{
/**//// <summary> Class cannot be instantiated</summary>
private StrUtil()
{
}
Left#region Left
/**//// <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)
{
if (len < 0 || str == null || str.Length <= len )
{
return str;
}
else
{
return str.Substring(0, len);
}
}
#endregion
LeftIndexOf#region LeftIndexOf
/**//// <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 )
{
return LeftIndexOf( str, search, 0, false );
}
/**//// <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 )
{
return LeftIndexOf( str, search, 0, includeSearchString );
}
/**//// <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 )
{
return LeftIndexOf( str, search, fromIndex, false );
}
/**//// <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 )
{
if ( Is.EmptyString( str ) || Is.EmptyString( search ) )
{
return str;
}
else
{
if ( fromIndex > str.Length )
{
return str;
}
int pos = str.IndexOf( search, fromIndex );
if ( pos == -1 )
{
return str;
}
else
{
return str.Substring( 0, pos + ( includeSearchString ? search.Length : 0 ) );
}
}
}
#endregion
LeftOf#region LeftOf
/**//// <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)
{
if ( str == null )
{
return null;
}
else
{
if ( pos == -1 ) return "";
if ( pos > str.Length ) return str;
return Substring( str, 0, pos );
}
}
#endregion
Right#region Right
/**//// <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