Vorspire
Knight
[RUNUO X] StringUtility
Just something simple I had to write to provide some extra string formatting for DevHelper, someone should be able to use this, so here you go.
StringUtility class will provide a few varied ways to manipulate strings for ease of use.
Just something simple I had to write to provide some extra string formatting for DevHelper, someone should be able to use this, so here you go.
StringUtility class will provide a few varied ways to manipulate strings for ease of use.
Code:
using System;
using System.Text;
using System.Collections;
using System.Collections.Generic;
using Server;
namespace Server
{
public class StringUtility
{
public static char[]
Numerics = new char[] { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' },
NumericReplacers = new char[] { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J' },
SpecialChars = new char[]
{
'#', '!', '"', '£', '$', '%', '^', '&', '*','(', ')', '-', '[', ']', '{', '}',
':', ';', '@', '\'', '~', '`', '¬', '¦', '\\', '/','|', '?', '<', '>', ',', '.'
};
public static string[] Keywords = new string[]
{
"abstract", "event", "new", "struct",
"as", "explicit", "null", "switch", "base", "extern", "object",
"this", "bool", "false", "operator", "throw", "break", "finally",
"out", "true", "byte", "fixed", "override", "try", "case", "float",
"params", "typeof", "catch", "for", "private", "uint", "char",
"foreach", "protected", "ulong", "checked", "goto", "public",
"unchecked", "class", "if", "readonly", "unsafe", "const",
"implicit", "ref", "ushort", "continue", "in", "return", "using",
"decimal", "int", "sbyte", "virtual", "default", "interface",
"sealed", "volatile", "delegate", "internal", "short", "void",
"do", "is", "sizeof", "while", "double", "lock", "stackalloc",
"else", "long", "static", "enum", "namespace", "string"
};
public static string SafeCodeString(string value, bool capWords)
{
return SafeCodeString(value, capWords, 1);
}
public static string SafeCodeString(string value, bool capWords, int minLength)
{
if (value == null)
return null;
if (value.Length < minLength)
return null;
for (int i = 0; i < SpecialChars.Length; i++)
{
value = value.Replace(SpecialChars[i].ToString(), "");
}
if (capWords)
value = CapitalizeWords(value);
value = value.Replace(' ', '_');
value = value.TrimStart(new char[] { '_' });
value = value.TrimEnd(new char[] { '_' });
value = Regex.Replace(value, @"\W", "");
value = value.Trim();
for (int i = 0; i < Keywords.Length; i++)
{
if (String.Compare(value, Keywords[i], false) == 0)
value = value.Replace(Keywords[i].ToString(), "");
}
for (int i = 0; i < Numerics.Length; i++)
{
if (value.StartsWith(Numerics[i].ToString(), false, CultureInfo.CurrentCulture))
{
value = value.Substring(1, value.Length - 1);
value = NumericReplacers[i].ToString() + value;
break;
}
}
if (value.Length < minLength)
return null;
return value;
}
public static string CapitalizeWords(string value)
{
if (value == null)
throw new ArgumentNullException("value");
if (value.Length == 0)
return value;
StringBuilder sb = new StringBuilder(value.Length);
// Upper the first char.
sb.Append(char.ToUpper(value[0]));
for (int i = 1; i < value.Length; i++)
{
// Get the current char.
char c = value[i];
// Upper if after a space.
if (char.IsWhiteSpace(value[i - 1]))
c = char.ToUpper(c);
else
c = char.ToLower(c);
sb.Append(c);
}
value = sb.ToString();
return value;
}
public static bool IsUpperCase(string str, bool digits)
{
string pat = String.Format(@"[A-Z{0}]*", digits ? "0-9" : "");
Regex regxp = new Regex(pat);
if (regxp.IsMatch(str))
{
return true;
}
return false;
}
public static bool IsLowerCase(string str, bool digits)
{
string pat = String.Format(@"[a-z{0}]*", digits ? "0-9" : "");
Regex regxp = new Regex(pat);
if (regxp.IsMatch(str))
{
return true;
}
return false;
}
public static string ToString(byte[] data)
{
StringBuilder sb = new StringBuilder(data.Length);
for (int i = 0; i < data.Length; i++)
{
sb.Append(ToString(data[i]));
}
return sb.ToString();
}
public static string ToString(byte b)
{
return ToString((char)b);
}
public static string ToString(char c)
{
return c.ToString();
}
public static string ToHexidecimal(int input, int length)
{
return String.Format("0x{0:X{1}}", input, length);
}
public static string ValidateFilePath(string filePath, string fileName)
{
string validPath = "";
int lookup = filePath.Substring(filePath.Length - 1, 1).IndexOfAny(new char[] { '\\', '/' });
bool hasEndSep = false;
if (lookup != -1)
hasEndSep = true;
if (hasEndSep)
filePath = filePath.Substring(0, lookup);
string[] split = filePath.Split(new char[] { '\\', '/' });
for (int i = 0; i < split.Length; i++)
{
validPath += split[i] + "\\";
}
if (fileName.Length > 0)
{
if (fileName.StartsWith("\\") || fileName.StartsWith("/"))
fileName = fileName.TrimStart(new char[] { '\\', '/' });
validPath += fileName;
}
else
{
if (validPath.EndsWith("\\") || validPath.EndsWith("/"))
validPath = validPath.TrimEnd(new char[] { '\\', '/' });
}
return validPath;
}
}
}