using System;
using System.Data;
using System.Text.RegularExpressions;
using System.Text;
class Class1{
static void Main(string[] args){
Console.WriteLine(IsInRange('c', 'a', 'G'));
Console.WriteLine(IsInRange('c', 'a', 'g'));
Console.WriteLine(IsInRange('c', 'c', 'g'));
Console.WriteLine(IsInRange((char)32, 'a', 'b'));
}
public static bool IsInRange(char testChar, char startOfRange, char endOfRange)
{
if (testChar >= startOfRange && testChar <= endOfRange)
{
// testChar is within the range
return (true);
}
else
{
// testChar is NOT within the range
return (false);
}
}
}
[/csharp]
Month: February 2011
Get char type: control, digit, letter, number, punctuation, surrogate, symbol and white space
using System;
using System.Data;
using System.Text.RegularExpressions;
using System.Text;
class Class1{
static void Main(string[] args){
Console.WriteLine(GetCharKind('f'));
Console.WriteLine(GetCharKind('0'));
Console.WriteLine(GetCharKind('.'));
Console.WriteLine(GetCharKind('}'));
}
public static String GetCharKind(char theChar)
{
if (Char.IsControl(theChar))
{
return "Control";
}
else if (Char.IsDigit(theChar))
{
return "Digit";
}
else if (Char.IsLetter(theChar))
{
return "Letter";
}
else if (Char.IsNumber(theChar))
{
return "Number";
}
else if (Char.IsPunctuation(theChar))
{
return "Punctuation";
}
else if (Char.IsSeparator(theChar))
{
return "Separator";
}
else if (Char.IsSurrogate(theChar))
{
return "Surrogate";
}
else if (Char.IsSymbol(theChar))
{
return "Symbol";
}
else if (Char.IsWhiteSpace(theChar))
{
return "Whitespace";
}
else
{
return "Unknown";
}
}
}
Returns a System.String representation of the value object
#region License and Copyright
/*
* Dotnet Commons Reflection
*
* This library is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* This library is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this library; if not, write to the
* Free Software Foundation, Inc.,
* 59 Temple Place,
* Suite 330,
* Boston,
* MA 02111-1307
* USA
*
*/
#endregion
using System;
using System.Collections;
using System.IO;
using System.Reflection;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
namespace Dotnet.Commons.Reflection
{
/// -----------------------------------------------------------------------
/// <summary>
/// This utility class contains a rich sets of utility methods that perform operations
/// on objects during runtime such as copying of property and field values
/// between 2 objects, deep cloning of objects, etc.
/// </summary>
/// -----------------------------------------------------------------------
public abstract class ObjectUtils
{
/// ------------------------------------------------------------------------
/// <summary>
/// Returns a <see cref="System.String"/> representation of the value object
/// </summary>
/// <param name="valueObject"></param>
/// <returns>string that represents the <see cref="System.Object"/></returns>
/// ------------------------------------------------------------------------
public static string ToString(object valueObject)
{
StringBuilder buffy = new StringBuilder();
buffy.Append("[");
buffy.Append(valueObject.GetType().FullName);
buffy.Append("]
");
// null objects cannot be reflected
if(valueObject == null)
{
buffy.Append(" is null.");
return buffy.ToString();
}
foreach(PropertyInfo objProperty in valueObject.GetType().GetProperties())
{
string propName;
propName = objProperty.Name+"=";
buffy.Append(propName);
if (objProperty != null)
{
object propValue = valueObject.GetType().InvokeMember(objProperty.Name,
BindingFlags.GetProperty,
null,
valueObject,
null);
if (propValue != null)
buffy.Append(propValue.ToString()+"
");
else
buffy.Append("<null>
");
}
else
buffy.Append("<null>
");
}
foreach(FieldInfo objField in valueObject.GetType().GetFields())
{
string fieldName;
fieldName = objField.Name +"=";
buffy.Append(fieldName);
if (objField != null)
{
object fieldValue = valueObject.GetType().InvokeMember(objField.Name,
BindingFlags.GetField,
null,
valueObject,
null);
buffy.Append(fieldValue.ToString()+"
");
}
else
buffy.Append("<null>
");
}
// ----------- End ---------
return buffy.ToString();
}
// ---------------------------------------------------------------------------
}
}
Converts a numeric value into number expressed as a size value in bytes, kilobytes, megabytes, gigabytes, or terabytes depending on the size.
#region License
// Copyright (c) 2007 James Newton-King
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the “Software”), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
#endregion
using System;
using System.Globalization;
namespace Newtonsoft.Utilities.Text
{
public class FormatUtils
{
///
/// expressed as a size value in bytes, kilobytes, megabytes, gigabytes,
/// or terabytes depending on the size. Output is identical to
/// StrFormatByteSize() in shlwapi.dll. This is a format similar to
/// the Windows Explorer file Properties page. For example:
/// 532 becomes 532 bytes
/// 1240 becomes 1.21 KB
/// 235606 becomes 230 KB
/// 5400016 becomes 5.14 MB
///
///
/// It was surprisingly difficult to emulate the StrFormatByteSize() function
/// due to a few quirks. First, the function only displays three digits:
/// – displays 2 decimal places for values under 10 (e.g. 2.12 KB)
/// – displays 1 decimal place for values under 100 (e.g. 88.2 KB)
/// – displays 0 decimal places for values under 1000 (e.g. 532 KB)
/// – jumps to the next unit of measure for values over 1000 (e.g. 0.97 MB)
/// The second quirk: insiginificant digits are truncated rather than
/// rounded. The original function likely uses integer math.
/// This implementation was tested to 100 TB.
///
public static string FileSizeToString(long fileSize)
{
if (fileSize < 1024)
{
return string.Format("{0} bytes", fileSize);
}
else
{
double value = fileSize;
value = value / 1024;
string unit = "KB";
if (value >= 1000)
{
value = Math.Floor(value);
value = value / 1024;
unit = “MB”;
}
if (value >= 1000)
{
value = Math.Floor(value);
value = value / 1024;
unit = “GB”;
}
if (value >= 1000)
{
value = Math.Floor(value);
value = value / 1024;
unit = “TB”;
}
if (value < 10) { value = Math.Floor(value * 100) / 100; return string.Format("{0:n2} {1}", value, unit); } else if (value < 100) { value = Math.Floor(value * 10) / 10; return string.Format("{0:n1} {1}", value, unit); } else { value = Math.Floor(value * 1) / 1; return string.Format("{0:n0} {1}", value, unit); } } } } } [/csharp]
Converts a number value into a string that represents the number expressed in whole kilobytes.
#region License
// Copyright (c) 2007 James Newton-King
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
#endregion
using System;
using System.Globalization;
namespace Newtonsoft.Utilities.Text
{
public class FormatUtils
{
/// <summary>
/// Converts a number value into a string that represents the number
/// expressed in whole kilobytes. This is a format similar to the
/// Windows Explorer "Size" column.
/// </summary>
public static string FileSizeToStringKB(long fileSize)
{
return string.Format("{0:n0} KB", Math.Ceiling((double)fileSize / 1024));
}
}
}
Convert To Int 32
#region License
// (c) Intergen.
// This source is subject to the Microsoft Public License (Ms-PL).
// Please see http://go.microsoft.com/fwlink/?LinkID=131993 for details.
// All other rights reserved.
#endregion
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace TextGlow.Control.Utilities
{
public static class BitUtils
{
public static int ConvertToInt32(string input)
{
if (input == null)
throw new ArgumentNullException(“input”);
int len = input.Length;
int sum = 0, position = 0;
for (int i = len – 1; i >= 0; i–)
{
if (input[i] == '1')
sum = sum + (1 << position);
position++;
}
return sum;
}
}
}
[/csharp]
Converts String to Any Other Type
//http://sb2extensions.codeplex.com/
//Apache License 2.0 (Apache)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Sb2.Extensions
{
public static class NullableExtensions
{
/// <summary>
/// Converts String to Any Other Type
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="input">The input.</param>
/// <returns></returns>
public static T? ConvertTo<T>(this string input) where T : struct
{
T? ret = null;
if (!string.IsNullOrEmpty(input))
{
ret = (T)Convert.ChangeType(input, typeof(T));
}
return ret;
}
/// <summary>
/// Converts String to Any Other Type
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="input">The input.</param>
/// <param name="provider">The provider.</param>
/// <returns></returns>
public static T? ConvertTo<T>(this string input, IFormatProvider provider) where T : struct
{
T? ret = null;
if (!string.IsNullOrEmpty(input))
{
ret = (T)Convert.ChangeType(input, typeof(T), provider);
}
return ret;
}
public static string ToString(this char? input)
{
return input.HasValue ? input.Value.ToString() : String.Empty;
}
public static char? ToNullableChar(this string input)
{
if (input.Trim().Length == 0)
return new char?();
else if (input.Trim().Length > 1)
throw new ArgumentException("Cannot convert string(" + input.Trim().Length + ") to char?");
else
return input[0];
}
}
}

