using System;
using System.Data;
using System.Text.RegularExpressions;
using System.Text;
class Class1{
static void Main(string[] args){
Console.WriteLine(IsInRangeExclusive('c', 'c', 'g'));
Console.WriteLine(IsInRangeExclusive('c', 'c', 'g'));
Console.WriteLine(IsInRangeExclusive((char)32, (char)31, 'Z'));
}
public static bool IsInRangeExclusive(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]
ASP.NET & Csharp (C#)
ASP.NET & Csharp (C#) , Here you can find my posts about .NET c# .
Is a char in a range: Case Insensitive
using System;
using System.Data;
using System.Text.RegularExpressions;
using System.Text;
class Class1{
static void Main(string[] args){
Console.WriteLine(IsInRangeCaseInsensitive('c', 'a', 'G'));
Console.WriteLine(IsInRangeCaseInsensitive('c', 'a', 'c'));
Console.WriteLine(IsInRangeCaseInsensitive('c', 'g', 'g'));
Console.WriteLine(IsInRangeCaseInsensitive((char)32, 'a', 'b'));
}
public static bool IsInRangeCaseInsensitive(char testChar, char startOfRange, char endOfRange)
{
testChar = char.ToUpper(testChar);
startOfRange = char.ToUpper(startOfRange);
endOfRange = char.ToUpper(endOfRange);
if (testChar >= startOfRange && testChar <= endOfRange) { // testChar is within the range return (true); } else { // testChar is NOT within the range return (false); } } } [/csharp]
Determining If A Character Is Within A Specified Range
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]
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)); } } }