initial checkin of yaulw (locally)
This commit is contained in:
45
Tools/Convert.cs
Normal file
45
Tools/Convert.cs
Normal file
@@ -0,0 +1,45 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
namespace Yaulw.Tools
|
||||
{
|
||||
/// <remarks>
|
||||
/// Common Conversion Operations
|
||||
/// </remarks>
|
||||
public static class Convert
|
||||
{
|
||||
/// <summary>
|
||||
/// Convert a a String to a UTF Byte Array
|
||||
/// </summary>
|
||||
/// <param name="str">string to convert</param>
|
||||
/// <returns>a Byte Array from a String</returns>
|
||||
public static byte[] StrToByteArrayUTF(string str)
|
||||
{
|
||||
if (!String.IsNullOrEmpty(str))
|
||||
{
|
||||
System.Text.UTF8Encoding encoding = new System.Text.UTF8Encoding();
|
||||
return encoding.GetBytes(str);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Convert a a String to an ASCII Byte Array
|
||||
/// </summary>
|
||||
/// <param name="str">string to convert</param>
|
||||
/// <returns>a Byte Array from a String</returns>
|
||||
public static byte[] StrToByteArrayAscii(string str)
|
||||
{
|
||||
if (!String.IsNullOrEmpty(str))
|
||||
{
|
||||
System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();
|
||||
return encoding.GetBytes(str);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
143
Tools/EnumTool.cs
Normal file
143
Tools/EnumTool.cs
Normal file
@@ -0,0 +1,143 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
namespace Yaulw.Tools
|
||||
{
|
||||
/// <remarks>
|
||||
/// Common Operations on Enumerations
|
||||
/// </remarks>
|
||||
public static class EnumTool
|
||||
{
|
||||
#region Common Operations
|
||||
|
||||
/// <summary>
|
||||
/// Turns an Enum to a String (Replacing _ with ' ')
|
||||
/// </summary>
|
||||
/// <param name="_enum">an Enum which could possibly contain an '_'</param>
|
||||
/// <returns>a ' ' spaced String of Enum</returns>
|
||||
public static string Enum_ToString(Enum _enum)
|
||||
{
|
||||
return _enum.ToString().Replace('_', ' ');
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Using the '__' in the Enum Name allows to embed Categories in the Name
|
||||
/// </summary>
|
||||
/// <param name="_enum">an Enum with '__'</param>
|
||||
/// <param name="Category">The String Value before the '__', also replacing '_' with ' '</param>
|
||||
/// <param name="Key">The String Value after the '__', also replacing '_' with ' '</param>
|
||||
public static void Enum_ToCategoryNKey(Enum _enum, out string Category, out string Key)
|
||||
{
|
||||
Category = String.Empty;
|
||||
Key = String.Empty;
|
||||
|
||||
if (!Enum_HasCategoryNKey(_enum))
|
||||
throw new ArgumentException("_enum must contain a __ with category and key values");
|
||||
|
||||
string _enumStr = _enum.ToString();
|
||||
int nIndex = _enumStr.IndexOf("__");
|
||||
|
||||
// Assign out the Retrived Values
|
||||
Category = _enumStr.Substring(0, nIndex).Replace('_', ' ');
|
||||
Key = _enumStr.Substring(nIndex + 2).Replace('_', ' ');
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region HasCategoryNKey
|
||||
|
||||
/// <summary>
|
||||
/// Determines if the Passed in Enum has both a Category and Key
|
||||
/// </summary>
|
||||
/// <param name="_enum">an Enum with '__'</param>
|
||||
/// <returns>true if '__' is present with both a category and key value</returns>
|
||||
public static bool Enum_HasCategoryNKey(Enum _enum)
|
||||
{
|
||||
// Check to see there is only one __
|
||||
string _enumStr = _enum.ToString();
|
||||
int nIndexFirst = _enumStr.IndexOf("__");
|
||||
int nIndexLast = _enumStr.LastIndexOf("__");
|
||||
if (nIndexFirst == nIndexLast)
|
||||
{
|
||||
// Check to see that there are values infront and behind the __
|
||||
if (nIndexFirst != 0 && (nIndexLast < (_enumStr.Length - 1)))
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Determines if all the Enums of the Passed in Enum Type have both a Category and Key
|
||||
/// </summary>
|
||||
/// <param name="_enum">an Enum Type that has all enums with '__'</param>
|
||||
/// <returns>true if '__' is present with both a category and key value on all enums</returns>
|
||||
public static bool EnumType_HasCategoryNKey(Type _enumType)
|
||||
{
|
||||
if (_enumType.IsEnum)
|
||||
{
|
||||
foreach (string enumName in Enum.GetNames(_enumType))
|
||||
{
|
||||
bool bIsValid = Enum_HasCategoryNKey((Enum)Enum.Parse(_enumType, enumName));
|
||||
if (!bIsValid)
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Category N' Key Traversals
|
||||
|
||||
/// <summary>
|
||||
/// Retrieves all the Categories of an Enum
|
||||
/// </summary>
|
||||
/// <param name="_enum">an Enum Type that has all enums with '__'</param>
|
||||
/// <returns>An Array of all unique Categories, '_' replaced with ' ', or null if error occured</returns>
|
||||
public static string[] EnumType_GetCategories(Type _enumType)
|
||||
{
|
||||
if (_enumType.IsEnum && EnumType_HasCategoryNKey(_enumType))
|
||||
{
|
||||
List<String> retVal = new List<String>();
|
||||
foreach (string enumName in Enum.GetNames(_enumType))
|
||||
{
|
||||
string Category;
|
||||
string Key;
|
||||
Enum_ToCategoryNKey((Enum)Enum.Parse(_enumType, enumName), out Category, out Key);
|
||||
if(!retVal.Contains(Category))
|
||||
retVal.Add(Category);
|
||||
}
|
||||
return retVal.ToArray();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Retrieves all the Keys of an Enum for a specified Category
|
||||
/// </summary>
|
||||
/// <param name="_enum">an Enum Type that has all enums with '__'</param>
|
||||
/// <returns>An Array of all unique Keys, '_' replaced with ' ', or null if error occured</returns>
|
||||
public static string[] EnumType_GetKeys(Type _enumType, string Category)
|
||||
{
|
||||
if (_enumType.IsEnum && EnumType_HasCategoryNKey(_enumType))
|
||||
{
|
||||
List<String> retVal = new List<String>();
|
||||
foreach (string enumName in Enum.GetNames(_enumType))
|
||||
{
|
||||
string CategoryTemp;
|
||||
string Key;
|
||||
Enum_ToCategoryNKey((Enum)Enum.Parse(_enumType, enumName), out CategoryTemp, out Key);
|
||||
if (String.Compare(Category, CategoryTemp, true) == 0)
|
||||
retVal.Add(Key);
|
||||
}
|
||||
return retVal.ToArray();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
269
Tools/ObjTool.cs
Normal file
269
Tools/ObjTool.cs
Normal file
@@ -0,0 +1,269 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
namespace Yaulw.Tools
|
||||
{
|
||||
/// <remarks>
|
||||
/// Useful Utilities around objects
|
||||
/// </remarks>
|
||||
public static class ObjTool
|
||||
{
|
||||
/// <summary>
|
||||
/// Returns true if passed in object is valid and is not empty
|
||||
/// </summary>
|
||||
/// <param name="oToValidate">an object to validate</param>
|
||||
/// <returns>true if valid, false otherwise</returns>
|
||||
public static bool IsNotNullAndNotEmpty(object oToValidate)
|
||||
{
|
||||
if ((oToValidate != null) && (oToValidate.ToString() != String.Empty))
|
||||
return true;
|
||||
else
|
||||
return false;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Use this to just check if the object can be converted to a string
|
||||
/// via ConvertObjToString(). If false, this is most likely an object type that shouldn't be converted to a string.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">Can be Any type</typeparam>
|
||||
/// <param name="objToCheckConvert">an object of type T to check if String Convertible</param>
|
||||
/// <returns>true, if the object passed in a String Convertible type like string, bool, int32, double, decimal, etc..., false otherwise</returns>
|
||||
public static bool IsOfTypeConvertibleToString<T>(T objToCheckConvert)
|
||||
{
|
||||
bool retVal = false;
|
||||
T objToConvert = objToCheckConvert;
|
||||
|
||||
// Check if String Convertible
|
||||
if (objToConvert is System.Char)
|
||||
{
|
||||
retVal = true;
|
||||
}
|
||||
else if (objToConvert is System.String)
|
||||
{
|
||||
retVal = true;
|
||||
}
|
||||
else if (objToConvert is System.Decimal)
|
||||
{
|
||||
retVal = true;
|
||||
}
|
||||
else if (objToConvert is System.Int32)
|
||||
{
|
||||
retVal = true;
|
||||
}
|
||||
else if (objToConvert is System.Int64)
|
||||
{
|
||||
retVal = true;
|
||||
}
|
||||
else if (objToConvert is System.Single)
|
||||
{
|
||||
retVal = true;
|
||||
}
|
||||
else if (objToConvert is System.Double)
|
||||
{
|
||||
retVal = true;
|
||||
}
|
||||
else if (objToConvert is System.Boolean)
|
||||
{
|
||||
retVal = true;
|
||||
}
|
||||
else if (objToConvert is System.DateTime)
|
||||
{
|
||||
retVal = true;
|
||||
}
|
||||
#if NET4
|
||||
else if (objToConvert is System.Guid) // Only works in .Net 4.0
|
||||
{
|
||||
retVal = true;
|
||||
}
|
||||
#endif
|
||||
else if (objToConvert is System.IntPtr)
|
||||
{
|
||||
retVal = true;
|
||||
}
|
||||
else if (objToConvert is System.UInt32)
|
||||
{
|
||||
retVal = true;
|
||||
}
|
||||
else if (objToConvert is System.UInt64)
|
||||
{
|
||||
retVal = true;
|
||||
}
|
||||
|
||||
return retVal;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Convert a string to an Object of Type T
|
||||
/// </summary>
|
||||
/// <typeparam name="T">Should be a System Type like string, bool, int32, double, decimal, etc...</typeparam>
|
||||
/// <param name="strToConvert">String Value to Convert</param>
|
||||
/// <returns>an converted Object of Type t, or T Default if error occured</returns>
|
||||
public static T ConvertStringToObj<T>(string strToConvert)
|
||||
{
|
||||
|
||||
// Create a Default Type T
|
||||
T retVal = default(T);
|
||||
|
||||
try
|
||||
{
|
||||
#region Conversion
|
||||
|
||||
if (retVal is System.Char)
|
||||
{
|
||||
retVal = (T)((object)strToConvert[0]);
|
||||
}
|
||||
else if (retVal is System.String)
|
||||
{
|
||||
retVal = (T)((object)strToConvert);
|
||||
}
|
||||
else if (retVal is System.Decimal)
|
||||
{
|
||||
retVal = (T)((object)Decimal.Parse(strToConvert));
|
||||
}
|
||||
else if (retVal is System.Int32)
|
||||
{
|
||||
retVal = (T)((object)Int32.Parse(strToConvert));
|
||||
}
|
||||
else if (retVal is System.Int64)
|
||||
{
|
||||
retVal = (T)((object)Int64.Parse(strToConvert));
|
||||
}
|
||||
else if (retVal is System.Single)
|
||||
{
|
||||
retVal = (T)((object)Single.Parse(strToConvert));
|
||||
}
|
||||
else if (retVal is System.Double)
|
||||
{
|
||||
retVal = (T)((object)Double.Parse(strToConvert));
|
||||
}
|
||||
else if (retVal is System.Boolean)
|
||||
{
|
||||
retVal = (T)((object)Boolean.Parse(strToConvert));
|
||||
}
|
||||
else if (retVal is System.DateTime)
|
||||
{
|
||||
retVal = (T)((object)DateTime.Parse(strToConvert));
|
||||
}
|
||||
#if NET4
|
||||
else if (retVal is System.Guid) // Works in .Net 4.0 and up
|
||||
{
|
||||
retVal = (T)((object)Guid.Parse(strToConvert));
|
||||
}
|
||||
#endif
|
||||
else if (retVal is System.IntPtr)
|
||||
{
|
||||
if (IntPtr.Size <= 4)
|
||||
{
|
||||
Int32 i = Int32.Parse(strToConvert);
|
||||
retVal = (T)((object)new IntPtr(i));
|
||||
}
|
||||
else
|
||||
{
|
||||
Int64 i = Int64.Parse(strToConvert);
|
||||
retVal = (T)((object)new IntPtr(i));
|
||||
}
|
||||
}
|
||||
else if (retVal is System.UInt32)
|
||||
{
|
||||
retVal = (T)((object)UInt32.Parse(strToConvert));
|
||||
}
|
||||
else if (retVal is System.UInt64)
|
||||
{
|
||||
retVal = (T)((object)UInt64.Parse(strToConvert));
|
||||
}
|
||||
else
|
||||
{
|
||||
retVal = (T)((object)(strToConvert));
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
catch (Exception e) { string Message = e.Message; /* ignore */ }
|
||||
|
||||
// return T
|
||||
return retVal;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Convert an object of type t to a String
|
||||
/// </summary>
|
||||
/// <typeparam name="T">Should be a System Type like string, bool, int32, double, decimal, etc...</typeparam>
|
||||
/// <param name="objToConvert">Object Value to Convert</param>
|
||||
/// <returns>a string that contains the value of the Object</returns>
|
||||
public static string ConvertObjToString<T>(T objToConvert)
|
||||
{
|
||||
String retVal = String.Empty;
|
||||
try
|
||||
{
|
||||
#region Conversion
|
||||
|
||||
if (objToConvert is System.Char)
|
||||
{
|
||||
retVal = ((Char)((object)objToConvert)).ToString();
|
||||
}
|
||||
else if (objToConvert is System.String)
|
||||
{
|
||||
retVal = ((String)((object)objToConvert)).ToString();
|
||||
}
|
||||
else if (objToConvert is System.Decimal)
|
||||
{
|
||||
retVal = ((Decimal)((object)objToConvert)).ToString();
|
||||
}
|
||||
else if (objToConvert is System.Int32)
|
||||
{
|
||||
retVal = ((Int32)((object)objToConvert)).ToString();
|
||||
}
|
||||
else if (objToConvert is System.Int64)
|
||||
{
|
||||
retVal = ((Int64)((object)objToConvert)).ToString();
|
||||
}
|
||||
else if (objToConvert is System.Single)
|
||||
{
|
||||
retVal = ((Single)((object)objToConvert)).ToString();
|
||||
}
|
||||
else if (objToConvert is System.Double)
|
||||
{
|
||||
retVal = ((Double)((object)objToConvert)).ToString();
|
||||
}
|
||||
else if (objToConvert is System.Boolean)
|
||||
{
|
||||
retVal = ((Boolean)((object)objToConvert)).ToString();
|
||||
}
|
||||
else if (objToConvert is System.DateTime)
|
||||
{
|
||||
retVal = ((DateTime)((object)objToConvert)).ToString();
|
||||
}
|
||||
#if NET4
|
||||
else if (objToConvert is System.Guid)
|
||||
{
|
||||
retVal = ((Guid)((object)objToConvert)).ToString();
|
||||
}
|
||||
#endif
|
||||
else if (objToConvert is System.IntPtr)
|
||||
{
|
||||
retVal = ((IntPtr)((object)objToConvert)).ToString();
|
||||
}
|
||||
else if (objToConvert is System.UInt32)
|
||||
{
|
||||
retVal = ((UInt32)((object)objToConvert)).ToString();
|
||||
}
|
||||
else if (objToConvert is System.UInt64)
|
||||
{
|
||||
retVal = ((UInt64)((object)objToConvert)).ToString();
|
||||
}
|
||||
else
|
||||
{
|
||||
retVal = ((object)objToConvert).ToString();
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
catch (Exception) { /* ignore */ }
|
||||
|
||||
// return T
|
||||
return retVal;
|
||||
}
|
||||
}
|
||||
}
|
||||
259
Tools/PathNaming.cs
Normal file
259
Tools/PathNaming.cs
Normal file
@@ -0,0 +1,259 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.IO;
|
||||
|
||||
namespace Yaulw.Tools
|
||||
{
|
||||
/// <remarks>
|
||||
/// Tools For File/Directory Paths
|
||||
/// </remarks>
|
||||
public static class PathNaming
|
||||
{
|
||||
/// <summary>
|
||||
/// Get the Root Path for a given path string
|
||||
/// </summary>
|
||||
/// <param name="path">path</param>
|
||||
/// <returns>the root folder, with '\'</returns>
|
||||
public static string GetPathRoot(string path)
|
||||
{
|
||||
if(!String.IsNullOrEmpty(path))
|
||||
return PathEndsWithSlash(System.IO.Path.GetPathRoot(path));
|
||||
return "";
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Does Path contain file information
|
||||
/// </summary>
|
||||
/// <returns>returns true if the specified path information contains file information</returns>
|
||||
public static bool PathContainsFile(string path)
|
||||
{
|
||||
if (!String.IsNullOrEmpty(path) && (path.LastIndexOf('.') > path.LastIndexOf('\\')))
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Makes sure that the specified path begins with a '\'
|
||||
/// </summary>
|
||||
/// <returns>a path string that begins with \</returns>
|
||||
public static string PathBeginsWithSlash(string path)
|
||||
{
|
||||
if (!String.IsNullOrEmpty(path) && (path[0] != '\\'))
|
||||
return '\\' + path;
|
||||
return path;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Makes sure that the specified path does NOT begin with a '\'
|
||||
/// </summary>
|
||||
/// <returns>a path string that does NOT begin with \</returns>
|
||||
public static string PathBeginsWithNoSlash(string path)
|
||||
{
|
||||
if (!String.IsNullOrEmpty(path) && (path[0] == '\\'))
|
||||
return path.Substring(1);
|
||||
return path;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Makes sure that the specified path ends with a '\'
|
||||
/// </summary>
|
||||
/// <returns>a path string that ends with \</returns>
|
||||
public static string PathEndsWithSlash(string path)
|
||||
{
|
||||
if (!String.IsNullOrEmpty(path) && (path[path.Length - 1] != '\\'))
|
||||
return path + '\\';
|
||||
return path;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Makes sure that the specified path does NOT end with a '\'
|
||||
/// </summary>
|
||||
/// <returns>a path string that does NOT end with \</returns>
|
||||
public static string PathEndsWithNoSlash(string path)
|
||||
{
|
||||
if (!String.IsNullOrEmpty(path) && (path[path.Length - 1] == '\\'))
|
||||
return path.Substring(0, path.Length - 1);
|
||||
return path;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns True if the fileName is valid * Filename chars are More restrictive than Path * should always use this
|
||||
/// </summary>
|
||||
/// <param name="FileName">filename to check for</param>
|
||||
/// <returns>true, if valid, false otherwise</returns>
|
||||
public static bool FileNameIsValid(string FileName)
|
||||
{
|
||||
if (!String.IsNullOrEmpty(FileName))
|
||||
{
|
||||
// stripe out the filename, if possible, if not, just use what is
|
||||
// passed into us
|
||||
string file = Path.GetFileName(FileName);
|
||||
if (!String.IsNullOrEmpty(file))
|
||||
FileName = file;
|
||||
|
||||
char[] invalidChars = Path.GetInvalidFileNameChars();
|
||||
foreach (char c in invalidChars)
|
||||
{
|
||||
if (FileName.Contains(c))
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns True if the Directory Path is valid * Path chars are not as restrictive as File * should always use File Chars (better safe than sorry)
|
||||
/// Use FileNameIsValid()
|
||||
/// </summary>
|
||||
/// <param name="DirectoryPath">DirectoryPath to check for</param>
|
||||
/// <returns>true, if valid, false otherwise</returns>
|
||||
public static bool IsDirectoryPathValid(string DirectoryPath)
|
||||
{
|
||||
if (!String.IsNullOrEmpty(DirectoryPath))
|
||||
{
|
||||
// stripe the trailing '\\', if possible, if not, just use what is
|
||||
// passed into us
|
||||
string path = Path.GetDirectoryName(DirectoryPath);
|
||||
if (!String.IsNullOrEmpty(path))
|
||||
DirectoryPath = path;
|
||||
|
||||
// FileName Char restrictions contain everything we need,
|
||||
// except for a path we want to allow '\\'
|
||||
char[] invalidChars = Path.GetInvalidFileNameChars();
|
||||
|
||||
foreach (char c in invalidChars)
|
||||
{
|
||||
// allow '\\' for paths
|
||||
if (c != '\\' && DirectoryPath.Contains(c))
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// return a valid directory path, a path that is invalid and contains invalid
|
||||
/// characters is automatically converted, (illegal characters are skipped)
|
||||
/// </summary>
|
||||
/// <param name="DirectoryPath">DirectoryPath to change</param>
|
||||
/// <returns>the 'corrected' path string</returns>
|
||||
public static string MakeDirectoryPathValid(string DirectoryPath)
|
||||
{
|
||||
return MakeDirectoryPathValid(DirectoryPath, String.Empty);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// return a valid directory path, a path that is invalid and contains invalid
|
||||
/// characters is automatically converted, (illegal characters are skipped)
|
||||
/// </summary>
|
||||
/// <param name="DirectoryPath">DirectoryPath to change</param>
|
||||
/// <param name="replacementStr">the string to put in place instead of the invalid char found,
|
||||
/// it itself can't be an invalid char</param>
|
||||
/// <returns>the 'corrected' path string</returns>
|
||||
public static string MakeDirectoryPathValid(string DirectoryPath, string replacementStr)
|
||||
{
|
||||
if(!String.IsNullOrEmpty(DirectoryPath))
|
||||
{
|
||||
// stripe the trailing '\\', if possible, if not, just use what is
|
||||
// passed into us
|
||||
string path = Path.GetDirectoryName(DirectoryPath);
|
||||
if (!String.IsNullOrEmpty(path))
|
||||
DirectoryPath = path;
|
||||
|
||||
// FileName Char restrictions contain everything we need,
|
||||
// except for a path we want to allow '\\'
|
||||
char[] invalidChars = Path.GetInvalidFileNameChars();
|
||||
|
||||
// Make sure replacementStr is valid also, otherwise it somewhat defeats the purpose
|
||||
// of this whole function
|
||||
string strToUseForReplacement = "";
|
||||
bool replacementStrIsValid = false;
|
||||
if (replacementStr != null)
|
||||
{
|
||||
replacementStrIsValid = true;
|
||||
if (replacementStr != "")
|
||||
{
|
||||
foreach (char c in replacementStr)
|
||||
{
|
||||
invalidChars.Contains(c);
|
||||
replacementStrIsValid = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (replacementStrIsValid)
|
||||
strToUseForReplacement = replacementStr;
|
||||
|
||||
// Construct new String Path
|
||||
StringBuilder sb = new StringBuilder();
|
||||
foreach (char c in DirectoryPath)
|
||||
{
|
||||
// allow '\\' for paths
|
||||
if (c != '\\' && invalidChars.Contains(c))
|
||||
{
|
||||
sb.Append(strToUseForReplacement);
|
||||
}
|
||||
else
|
||||
{
|
||||
sb.Append(c);
|
||||
}
|
||||
}
|
||||
return sb.ToString();
|
||||
}
|
||||
return DirectoryPath;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Retrieve the last directory name from a path, can be a UNC path like \\{server}\Directory
|
||||
/// or a file and system path like C:\{directory}\{directory}, will always return the name of the
|
||||
/// last directory in the path. if it is not a path, i.e just a name is passed in without '\'s then
|
||||
/// it will just stripe out all '\' that maybe there and return the name
|
||||
/// </summary>
|
||||
/// <param name="DirectoryPath"></param>
|
||||
/// <returns>the name of the last directory in the path</returns>
|
||||
public static string RetrieveLastDirectoryNameInPath(string DirectoryPath)
|
||||
{
|
||||
if (!String.IsNullOrEmpty(DirectoryPath))
|
||||
{
|
||||
// Find the last folder and return that information
|
||||
// This is what this function was created for
|
||||
string dp = Path.GetDirectoryName(DirectoryPath);
|
||||
int nLast = dp.LastIndexOf('\\');
|
||||
if (nLast != -1 && dp.Length > 3)
|
||||
{
|
||||
dp = dp.Substring(nLast + 1);
|
||||
return dp;
|
||||
}
|
||||
|
||||
// Return what was passed into us, probably doesn't have a '\\',
|
||||
// but it could be that it just has one. like someone passed in \\{server},
|
||||
// then we'll just return the {server}, if someone passes in c:\directory, then,
|
||||
// we should pass back just the directory
|
||||
if (DirectoryPath.Length > 3 && DirectoryPath[1] == ':')
|
||||
{
|
||||
return DirectoryPath.Substring(3);
|
||||
}
|
||||
else
|
||||
{
|
||||
// try finding the last \\, again without using GetDirectoryName which
|
||||
// stripes it, return the last stuff found, otherwise just return what was passed to us
|
||||
nLast = DirectoryPath.LastIndexOf('\\');
|
||||
if (nLast != -1 && DirectoryPath.Length > 3)
|
||||
{
|
||||
return DirectoryPath.Substring(nLast + 1);
|
||||
}
|
||||
else
|
||||
{
|
||||
return DirectoryPath;
|
||||
}
|
||||
}
|
||||
}
|
||||
return String.Empty;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
165
Tools/StringTool.cs
Normal file
165
Tools/StringTool.cs
Normal file
@@ -0,0 +1,165 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
namespace Yaulw.Tools
|
||||
{
|
||||
/// <summary>
|
||||
/// Useful String Operations
|
||||
/// </summary>
|
||||
public static class StringTool
|
||||
{
|
||||
/// <summary>
|
||||
/// Searches for the 'value' occurences that occur in 'str'
|
||||
/// </summary>
|
||||
/// <param name="value">string to search occurences for</param>
|
||||
/// <param name="str">string to search in</param>
|
||||
/// <param name="bIgnoreCase">true to ignore case, false otherwise</param>
|
||||
/// <returns>the number of occurences of 'value' in 'str', 0 for none</returns>
|
||||
public static uint NumberOfSpecStringFoundInString(string value, string str, bool bIgnoreCase = false)
|
||||
{
|
||||
uint nCount = 0;
|
||||
if (!String.IsNullOrEmpty(str))
|
||||
{
|
||||
int nIndex = 0;
|
||||
StringComparison comparison = bIgnoreCase ? StringComparison.CurrentCultureIgnoreCase : StringComparison.CurrentCulture;
|
||||
do
|
||||
{
|
||||
nIndex = str.IndexOf(value, nIndex, comparison);
|
||||
if(nIndex != -1)
|
||||
++nCount;
|
||||
} while (nIndex != -1);
|
||||
}
|
||||
return nCount;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Check to see if string starts with a letter
|
||||
/// </summary>
|
||||
/// <param name="str">string to check</param>
|
||||
/// <returns>true if string starts with a letter</returns>
|
||||
public static bool StringStartsWithLetter(string str)
|
||||
{
|
||||
if (!String.IsNullOrEmpty(str))
|
||||
return char.IsLetter(str[0]);
|
||||
return false;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Check to see if string ends with a letter
|
||||
/// </summary>
|
||||
/// <param name="str">string to check</param>
|
||||
/// <returns>true if string ends with a letter</returns>
|
||||
public static bool StringEndsWithLetter(string str)
|
||||
{
|
||||
if (!String.IsNullOrEmpty(str))
|
||||
return char.IsLetter(str[str.Length - 1]);
|
||||
return false;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns the last word of a string (string must end with a letter)
|
||||
/// </summary>
|
||||
/// <param name="str">string to go thru</param>
|
||||
/// <returns>the last word of a string</returns>
|
||||
public static string StringFetchLastWord(string str)
|
||||
{
|
||||
if (!String.IsNullOrEmpty(str) && StringEndsWithLetter(str))
|
||||
{
|
||||
// Find the last word
|
||||
StringBuilder sb = new StringBuilder();
|
||||
for (int i = str.Length - 1; i >= 0; --i)
|
||||
{
|
||||
if (char.IsLetter(str[i]))
|
||||
sb.Append(str[i]);
|
||||
else
|
||||
break;
|
||||
}
|
||||
|
||||
// Reverse the String
|
||||
string strRet = sb.ToString();
|
||||
#if NET4
|
||||
sb.Clear();
|
||||
#else
|
||||
sb = new StringBuilder();
|
||||
#endif
|
||||
for (int i = strRet.Length - 1; i >=0; --i)
|
||||
sb.Append(strRet[i]);
|
||||
|
||||
// return the string
|
||||
return sb.ToString();
|
||||
}
|
||||
return String.Empty;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns the first word of a string (string must start with a letter)
|
||||
/// </summary>
|
||||
/// <param name="str">string to go thru</param>
|
||||
/// <returns>the first word of a string</returns>
|
||||
public static string StringFetchFirstWord(string str)
|
||||
{
|
||||
if (!String.IsNullOrEmpty(str) && StringStartsWithLetter(str))
|
||||
{
|
||||
// Find the last word
|
||||
StringBuilder sb = new StringBuilder();
|
||||
for (int i = 0; i < str.Length; ++i)
|
||||
{
|
||||
if (char.IsLetter(str[i]))
|
||||
sb.Append(str[i]);
|
||||
else
|
||||
break;
|
||||
}
|
||||
|
||||
// return the string
|
||||
return sb.ToString();
|
||||
}
|
||||
return String.Empty;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Stripes the last word from the referenced string (string must end with a letter)
|
||||
/// </summary>
|
||||
/// <param name="str">last word stripped from string</param>
|
||||
public static void StringStripeLastWord(ref string str)
|
||||
{
|
||||
if (!String.IsNullOrEmpty(str) && StringEndsWithLetter(str))
|
||||
{
|
||||
// Find the last word
|
||||
int i = 0;
|
||||
for (i = str.Length - 1; i >= 0; --i)
|
||||
{
|
||||
if (!char.IsLetter(str[i]))
|
||||
break;
|
||||
}
|
||||
|
||||
// Return string without last word
|
||||
str = str.Substring(0, i + 1);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Stripes the first word from the referenced string (string must start with a letter)
|
||||
/// </summary>
|
||||
/// <param name="str">first word stripped from string</param>
|
||||
public static void StringStripeFirstWord(ref string str)
|
||||
{
|
||||
if (!String.IsNullOrEmpty(str) && StringStartsWithLetter(str))
|
||||
{
|
||||
// Find the last word
|
||||
int i = 0;
|
||||
for (i = 0; i < str.Length; ++i)
|
||||
{
|
||||
if (!char.IsLetter(str[i]))
|
||||
break;
|
||||
}
|
||||
|
||||
// Return string without first word
|
||||
str = str.Substring(i);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user