Generate ToString for given object

image_pdfimage_print
   
 


/*
 License:  Microsoft Public License (Ms-PL)  
 http://c4fdevkit.codeplex.com/license
 C4F Developer Kit

*/
using System;
using System.IO;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Media.Imaging;


public class MainClass{
        /// <summary>
        /// Generates ToString functionality for a struct.  This is an expensive way to do it,
        /// it exists for the sake of debugging while classes are in flux.
        /// Eventually this should just be removed and the classes should
        /// do this without reflection.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="object"></param>
        /// <returns></returns>
        public static string GenerateToString<T>(T @object) where T : struct
        {
            StringBuilder sbRet = new StringBuilder();
            foreach (PropertyInfo property in typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance))
            {
                if (0 != sbRet.Length)
                {
                    sbRet.Append(", ");
                }
               // Assert.AreEqual(0, property.GetIndexParameters().Length);
                object value = property.GetValue(@object, null);
                string format = null == value ? "{0}: <null>" : "{0}: "{1}"";
                sbRet.AppendFormat(format, property.Name, value);
            }
            return sbRet.ToString();
        }
}

   
     


String Tokenizer

image_pdfimage_print

using System;
/*
* $Id: StringTokenizer.cs,v 1.4 2006/06/16 10:52:26 psoares33 Exp $
*
* Copyright 2006 by Paulo Soares.
*
* The contents of this file are subject to the Mozilla Public License Version 1.1
* (the “License”); you may not use this file except in compliance with the License.
* You may obtain a copy of the License at http://www.mozilla.org/MPL/
*
* Software distributed under the License isp distributed on an “AS IS” basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the License.
*
* The Original Code isp 'iText, a free JAVA-PDF library'.
*
* The Initial Developer of the Original Code isp Bruno Lowagie. Portions created by
* the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie.
* All Rights Reserved.
* Co-Developer of the code isp Paulo Soares. Portions created by the Co-Developer
* are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved.
*
* Contributor(s): all the names of the contributors are added in the source code
* where applicable.
*
* Alternatively, the contents of this file may be used under the terms of the
* LGPL license (the “GNU LIBRARY GENERAL PUBLIC LICENSE”), in which case the
* provisions of LGPL are applicable instead of those above. If you wish to
* allow use of your version of this file only under the terms of the LGPL
* License and not to allow others to use your version of this file under
* the MPL, indicate your decision by deleting the provisions above and
* replace them with the notice and other provisions required by the LGPL.
* If you do not delete the provisions above, a recipient may use your version
* of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE.
*
* This library isp free software; you can redistribute it and/or modify it
* under the terms of the MPL as stated above or under the terms of the GNU
* Library General Public License as published by the Free Software Foundation;
* either version 2 of the License, or any later version.
*
* This library isp 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 Library general Public License for more
* details.
*
* If you didn't download this code from the following link, you should check if
* you aren't using an obsolete version:
* http://www.lowagie.com/iText/
*/

namespace System.util {

// a replacement for the StringTokenizer java class
// it's more or less the same as the one in the GNU classpath
public class StringTokenizer {
private int pos;
private String str;
private int len;
private String delim;
private bool retDelims;

public StringTokenizer(String str) : this(str, ”

f”, false) {
}

public StringTokenizer(String str, String delim) : this(str, delim, false) {
}

public StringTokenizer(String str, String delim, bool retDelims) {
len = str.Length;
this.str = str;
this.delim = delim;
this.retDelims = retDelims;
this.pos = 0;
}

public bool HasMoreTokens() {
if (! retDelims) {
while (pos < len && delim.IndexOf(str[pos]) >= 0)
pos++;
}
return pos < len; } public String NextToken(String delim) { this.delim = delim; return NextToken(); } public String NextToken() { if (pos < len && delim.IndexOf(str[pos]) >= 0) {
if (retDelims)
return str.Substring(pos++, 1);
while (++pos < len && delim.IndexOf(str[pos]) >= 0);
}
if (pos < len) { int start = pos; while (++pos < len && delim.IndexOf(str[pos]) < 0); return str.Substring(start, pos - start); } throw new IndexOutOfRangeException(); } public int CountTokens() { int count = 0; int delimiterCount = 0; bool tokenFound = false; int tmpPos = pos; while (tmpPos < len) { if (delim.IndexOf(str[tmpPos++]) >= 0) {
if (tokenFound) {
count++;
tokenFound = false;
}
delimiterCount++;
}
else {
tokenFound = true;
while (tmpPos < len && delim.IndexOf(str[tmpPos]) < 0) ++tmpPos; } } if (tokenFound) count++; return retDelims ? count + delimiterCount : count; } } } [/csharp]

Implements a StringTokenizer class for splitting a string into substrings using a set of delimiters.

image_pdfimage_print
   
 

#region Using Directives
using System;
using System.Collections.Generic;
#endregion

/*
 * $author: QmQ
 * $source: http://www.codeproject.com/useritems/SimpleStringTokenizer.asp
 * $date:   10-June-2006
 */
namespace CBRSystem.Data
{

    #region [Summary and remarks]
    /// <summary>
    /// Implements a StringTokenizer class for splitting a string
    /// into substrings using a set of delimiters.
    /// </summary>
    /// <remarks>
    /// C# version of the java.util.StringTokenizer class.
    /// Basicly it&#039;s a wrapper class around the <c>String.Split</c> method.<pare/>
    /// It implements all of it&#039;s Java equivalent methods apart from those only needed by the Enumeration interface.
    /// All implemented Java-compilant methods have their C# equivalents in properties. They however differ in names
    /// since Java uses the (Hungarian-like) notation <c>runMe()</c> while C# uses Camel-cased <c>RunMe()</c> and thus
    /// Java&#039;s <c>nextToken()</c> method is just an alias of the <c>NextToken</c> property.
    /// </remarks>
    #endregion
    public class StringTokenizer : IEnumerable<string>
    {
        /// <summary>
        /// String conatining the default set of delimiters which are <c>" 	

f"</c>:
        /// the space character, the tab character, the newline character, the carriage-return character, and the form-feed character.
        /// </summary>
        public const string DefaultDelimiters = " 	

f";

        private readonly string delims = DefaultDelimiters;
        private string[] tokens = null;
        private int index = 0;
        private string empty = String.Empty;

        #region [Constructors]
        /// <summary>
        /// Constructs a string tokenizer for the specified string using the <see cref="F:DefaultDelimiters">default delimiters</see>.
        /// </summary>
        /// <param name="str">The string to be tokenized.</param>
        /// <exception cref="System.NullReferenceException">Thrown when the passed string is <c>null</c></exception>
        public StringTokenizer(string str)
        {
            Tokenize(str, false, false);
        }

        /// <summary>
        /// Constructs a string tokenizer for the specified string using the given delimiters.
        /// </summary>
        /// <param name="str">The string to be tokenized.</param>
        /// <param name="delims">The delimiters used to tokenize the string (each <see cref="!:char"/> will be used as a delimiter).</param>
        /// <exception cref="System.NullReferenceException">Thrown when the passed string is <c>null</c></exception>
        public StringTokenizer(string str, string delims)
        {
            if(delims!=null) this.delims = delims;
            Tokenize(str, false, false);
        }

        /// <summary>
        /// Constructs a string tokenizer for the specified string using the given delimiters.
        /// </summary>
        /// <param name="str">The string to be tokenized.</param>
        /// <param name="delims">The delimiters used to tokenize the string.</param>
        public StringTokenizer(string str, params char[] delims)
        {
            if (delims != null) this.delims = new string(delims);
            Tokenize(str, false, false);
        }

        /// <summary>
        /// Constructs a string tokenizer for the specified string using the given delimiters and optionally returning them as tokens.
        /// </summary>
        /// <param name="str">The string to be tokenized.</param>
        /// <param name="delims">The delimiters used to tokenize the string (each <see cref="!:char"/> will be used as a delimiter).</param>
        /// <param name="returnDelims">If set to <c>true</c> the encountered delimiters will also be returned as tokens.</param>
        /// <exception cref="System.NullReferenceException">Thrown when the passed string is <c>null</c></exception>
        public StringTokenizer(string str, string delims, bool returnDelims)
        {
            if (delims != null) this.delims = delims;
            Tokenize(str, returnDelims, false);
        }

        /// <summary>
        /// Constructs a string tokenizer for the specified string using the given delimiters,
        /// optionally returning them as tokens. Also empty tokens may be returned using the <see cref="!:String.Empty"/> string.
        /// </summary>
        /// <param name="str">The string to be tokenized.</param>
        /// <param name="delims">The delimiters used to tokenize the string (each <see cref="!:char"/> will be used as a delimiter).</param>
        /// <param name="returnDelims">If set to <c>true</c> the encountered delimiters will also be returned as tokens.</param>
        /// <param name="returnEmpty">If set to <c>true</c> empty tokens will also be returned.</param>
        /// <exception cref="System.NullReferenceException">Thrown when the passed string is <c>null</c></exception>
        public StringTokenizer(string str, string delims, bool returnDelims, bool returnEmpty)
        {
            if (delims != null) this.delims = delims;
            Tokenize(str, returnDelims, returnEmpty);
        }

        /// <summary>
        /// Constructs a string tokenizer for the specified string using the given delimiters,
        /// optionally returning them as tokens. Also empty tokens may be returned using the <paramref name="empty"/> string.
        /// </summary>
        /// <param name="str">The string to be tokenized.</param>
        /// <param name="delims">The delimiters used to tokenize the string (each <see cref="!:char"/> will be used as a delimiter).</param>
        /// <param name="returnDelims">If set to <c>true</c> the encountered delimiters will also be returned as tokens.</param>
        /// <param name="returnEmpty">If set to <c>true</c> empty tokens will also be returned.</param>
        /// <param name="empty">The string to be returned as an empty token.</param>
        /// <exception cref="System.NullReferenceException">Thrown when the passed string is <c>null</c></exception>
        public StringTokenizer(string str, string delims, bool returnDelims, bool returnEmpty, string empty)
        {
            if (delims != null) this.delims = delims;
            this.empty = empty;
            Tokenize(str, returnDelims, returnEmpty);
        }
    #endregion

        #region [The big tokenization method]
        private void Tokenize(string str, bool returnDelims, bool returnEmpty)
        {
            if(returnDelims)
            {
                this.tokens = str.Split(this.delims.ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
                List<string> tmp = new List<string>(tokens.Length << 1);

                int delimIndex = str.IndexOfAny(this.delims.ToCharArray());
                int tokensIndex = 0;
                int prevDelimIdx = delimIndex-1;

                if (delimIndex == 0)
                    do
                    {
                        tmp.Add(new string(str&#91;delimIndex&#93;, 1));
                        prevDelimIdx = delimIndex++;
                        delimIndex = str.IndexOfAny(this.delims.ToCharArray(), delimIndex);
                        if (returnEmpty &amp;&amp; delimIndex == prevDelimIdx + 1)
                            tmp.Add(this.empty);
                    } while (delimIndex == prevDelimIdx + 1);

                while (delimIndex > -1)
                {
                    tmp.Add(this.tokens[tokensIndex++]);

                    do
                    {
                        tmp.Add(new string(str[delimIndex], 1));
                        prevDelimIdx = delimIndex++;
                        delimIndex = str.IndexOfAny(this.delims.ToCharArray(), delimIndex);
                        if (returnEmpty &amp;&amp; delimIndex == prevDelimIdx + 1)
                            tmp.Add(this.empty);
                    } while (delimIndex == prevDelimIdx + 1);

                }
                if (tokensIndex < tokens.Length)
                    tmp.Add(this.tokens&#91;tokensIndex++&#93;);

                this.tokens = tmp.ToArray();
                tmp = null;
            }
            else if (returnEmpty)
            {
                this.tokens = str.Split(this.delims.ToCharArray(), StringSplitOptions.None);
                if (this.empty != String.Empty)
                    for(int i=0; i<this.tokens.Length; i++)
                        if (this.tokens&#91;i&#93; == String.Empty) this.tokens&#91;i&#93; = this.empty;
            }
            else
                this.tokens = str.Split(this.delims.ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
        }
        #endregion

        #region &#91;Properties covering Java methods&#93;
        /// <summary>
        /// Tests if there are more tokens available from this tokenizer&#039;s string.
        /// If this method returns <c>true</c>, then a subsequent
        /// use of the <see cref="P:NextToken"/> property will successfully return a token.
        /// </summary>
        /// <value>
        ///   <c>true</c> if more tokens are available; otherwise <c>false</c>.
        /// </value>
        public bool HasMoreTokens
        {
            get { return this.index < this.tokens.Length; }
        }

        /// <summary>
        /// Gets the next token.
        /// </summary>
        /// <value>The next token.</value>
        /// <exception cref="System.IndexOutOfRangeException">Thrown when trying to get a token which doesn&#039;t exist.
        /// Usually caused by not checking if the <see cref="P:HasMoreTokens"/> property returns <c>true</c> before trying to get the next token.</exception>
        public string NextToken
        {
            get { return this.tokens[index++]; }
        }

        /// <summary>
        /// Counts the <see cref="!:remaining"/> tokens - the number of times the
        /// <see cref="P:NextToken"/> property can be used before it throws an exception.
        /// </summary>
        /// <value>The number of remaining tokens.</value>
        /// <seealso cref="P:Count"/>
        public int CountTokens
        {
            get { return this.tokens.Length - this.index; }
        }
    #endregion

        #region [New methods/properties]
        /// <summary>
        /// Gets the total number of tokens extracted.
        /// </summary>
        /// <remarks>
        /// <see cref="!:Equivalent not available in Java!"/>
        /// This property returns the total number of extracted tokens,
        /// contrary to <see cref="P:CountTokens"/>.
        /// </remarks>
        /// <value>The number of tokens extracted.</value>
        /// <seealso cref="P:StringTokenizer.CountTokens"/>
        public int Count
        {
            get { return this.tokens.Length; }
        }

        /// <summary>
        /// Gets the token with the specified index from the tokenizer without moving the current position index.
        /// </summary>
        /// <remarks><see cref="!:Equivalent not available in Java!"/></remarks>
        /// <param name="index">The index of the token to get.</param>
        /// <value>The token with the given index</value>
        /// <exception cref="System.IndexOutOfRangeException">Thrown when trying to get a token which doesn&#039;t exist, that is when <see cref="!:index"/> is equal or greater then <see cref="!:Count"/> or <see cref="!:index"/> is negative.</exception>
        public string this[int index]
        {
            get { return this.tokens[index]; }
        }

        /// <summary>
        /// Resets the current position index so that the tokens can be extracted again.
        /// </summary>
        /// <remarks><see cref="!:Equivalent not available in Java!"/></remarks>
        public void Reset()
        {
            this.index = 0;
        }

        /// <summary>
        /// Gets the currently set string for empty tokens.
        /// </summary>
        /// <remarks>Default is <c>System.String.Empty</c></remarks>
        /// <value>The empty token string.</value>
        public string EmptyString
        {
            get { return this.empty; }
        }
        #endregion

        #region [Java-compilant methods]
    /*
        /// <summary>
        /// Tests if there are more tokens available from this tokenizer&#039;s string.
        /// If this method returns <c>true</c>, then a subsequent call to <see cref="M:nextToken"/> will successfully return a token.
        /// </summary>
        /// <returns>
        ///   <c>true</c> if and only if there is at least one token in the string after the current position; otherwise <c>false</c>.
        /// </returns>
        /// <seealso cref="M:nextToken"/>
        public bool hasMoreTokens()
        {
            return HasMoreTokens;
        }

        /// <summary>
        /// Returns the next token from this string tokenizer.
        /// </summary>
        /// <returns>The next token from this string tokenizer.</returns>
        public string nextToken()
        {
            return NextToken;
        }

        /// <summary>
        /// Calculates the number of times that this tokenizer&#039;s <see cref="M:nextToken"/> method can be called before it generates an exception. The current position is not advanced.
        /// </summary>
        /// <returns>The number of tokens remaining in the string using the current delimiter set.</returns>
        public int countTokens()
        {
            return CountTokens;
        }
    */
        #endregion

        #region [IEnumerable implementation]
        /// <summary>
        /// Returns an enumerator that iterates through the collection.
        /// </summary>
        /// <returns>
        /// A <see cref="T:System.Collections.Generic.IEnumerator"/> that can be used to iterate through the collection.
        /// </returns>
        public IEnumerator<string> GetEnumerator()
        {
            while (this.HasMoreTokens)
                yield return this.NextToken;
        }

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
    {
      return GetEnumerator();
    }
        #endregion

    }
}

   
     


Use StringSplitOptions enumeration to include or exclude substrings generated by the Split method.

image_pdfimage_print
   
  
using System;
class Sample 
{
    public static void Main() 
    {
       string s1 = ",A,,B,,,C,,";
       char[] charSeparators = new char[] {&#039;,&#039;};
       string[] result;
        
        result = s1.Split(charSeparators, StringSplitOptions.None);
        Show(result);       
    }
    public static void Show(string[] entries)
    {
        foreach (string entry in entries)
        {
            Console.WriteLine(entry);
        }
    }
}

   
    
     


Split a string delimited by characters and return all non-empty elements

image_pdfimage_print

 

Split a string delimited by characters and return 2 non-empty elements

image_pdfimage_print
   
  

using System;
class Sample 
{
    public static void Main() 
    {
       string s1 = ",A,TWO,,,THREE,,";
       char[] charSeparators = new char[] {&#039;,&#039;};
       string[] result;

        result = s1.Split(charSeparators, 2, StringSplitOptions.RemoveEmptyEntries);
        Show(result);
    
    }
    public static void Show(string[] entries)
    {
        foreach (string entry in entries)
        {
            Console.WriteLine(entry);
        }
    }
}