Returns an arraylist of strings that contains the substrings in this instance that are delimited by 'delimiter'. - CSharp System

CSharp examples for System:Array String

Description

Returns an arraylist of strings that contains the substrings in this instance that are delimited by 'delimiter'.

Demo Code

//  Licensed under the Apache License, Version 2.0 (the "License");
using System.Collections;
using System.Text;
using System;//from   w w w . j ava  2  s  . c o m

public class Main{
    /// <summary>
      /// Returns a arraylist of strings that contains the substrings in this instance that are delimited by 'delimiter'.
      /// </summary>
      /// <param name="array"></param>
      /// <param name="delimiter">Bytes that delimit the substrings.</param>
      /// <param name="startIndex">The search start position.</param>
      /// <param name="length">The number of bytes to search.</param>
      /// <param name="toLower">Will the convert the substrings to lower case if 'true'.</param>
      /// <returns>An arraylist containing the substrings found in this instance. Invalid paramaters result in a return of an empty arraylist.</returns>
      public static ArrayList Split(this byte[] array, byte[] delimiter, int startIndex, int length, bool toLower = false)
      {
         if (array == null || startIndex < 0 || length < 0 || array.Length < (startIndex + length))
            return new ArrayList();

         ArrayList list = new ArrayList();

         int posMax = startIndex + length;
         int posDelimiter = 0;
         while ((startIndex + delimiter.Length) <= posMax && (posDelimiter = IndexOf(array, delimiter, startIndex)) >= 0)
         {
            if (toLower)
            {
               list.Add((new string(Encoding.UTF8.GetChars(array, startIndex, posDelimiter - startIndex))).ToLower());
            }
            else
            {
               list.Add(new string(Encoding.UTF8.GetChars(array, startIndex, posDelimiter - startIndex)));
            }
            startIndex = posDelimiter + delimiter.Length;
         }
         return list;
      }
        /// <summary>
      /// Returns a arraylist of strings that contains the substrings in this instance that are delimited by 'delimiter'.
      /// </summary>
      /// <param name="array"></param>
      /// <param name="delimiter">Character that delimits the substrings.</param>
      /// <param name="startIndex">The search start position.</param>
      /// <param name="length">The number of bytes to search.</param>
      /// <param name="toLower">Will the convert the substrings to lower case if 'true'.</param>
      /// <returns>An arraylist containing the substrings found in this instance. Invalid paramaters result in a return of an empty arraylist.</returns>
      public static ArrayList Split(this byte[] array, char delimiter, int startIndex, int length, bool toLower=false)
      {
         if (array == null || startIndex < 0 || length < 0 || array.Length < (startIndex + length))
            return new ArrayList();

         byte[] byteSeparator = new byte[] { (byte)delimiter };
         return Split(array, byteSeparator, startIndex, length, toLower);
      }
        /// <summary>
      /// Returns a arraylist of strings that contains the substrings in this instance that are delimited by 'delimiter'.
      /// </summary>
      /// <param name="array"></param>
      /// <param name="delimiter">String that delimits the substrings.</param>
      /// <param name="startIndex">The search start position.</param>
      /// <param name="length">The number of bytes to search.</param>
      /// <param name="toLower">Will the convert the substrings to lower case if 'true'.</param>
      /// <returns>An arraylist containing the substrings found in this instance. Invalid paramaters result in a return of an empty arraylist.</returns>
      public static ArrayList Split(this byte[] array, string delimiter, int startIndex, int length, bool toLower = false)
      {
         if (array == null || startIndex < 0 || length < 0 || array.Length < (startIndex + length))
            return new ArrayList();

         byte[] byteSeparator = delimiter.ToByteArray();
         return Split(array, byteSeparator, startIndex, length, toLower);
      }
}

Related Tutorials