0 Replies - 3114 Views - Last Post: 22 November 2014 - 08:21 AM

#1 kepp  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 8
  • View blog
  • Posts: 41
  • Joined: 18-November 10

Useful custom string functions

Posted 22 November 2014 - 08:21 AM

String parsing can be tedious work. I wrote this class a few years ago when i needed to parse ALOT of data from sockets!
I added another function a few months ago that i call SplitPortion
just like string.Split() except that you should know the total number of delimeters in the data to split:

A few examples:

BeforeFirst(string source, string delimeter)
    public static void Main (string[] args)
    {
         string example = "Name;Middlename;Lastname;Age";
         string result = StringFunctions.BeforeFirst(example, ";");
         Console.WriteLine(result);   
    }

// Output from console: Name



BeforeLast(string source, string delimeter)
    public static void Main (string[] args)
    {
         string example = "Name;Middlename;Lastname;Age";
         string result = StringFunctions.BeforeLast(example, ";");
         Console.WriteLine(result);   
    }

// Output from console: Name;Middlename;Lastname



AfterFirst(string source, string delimeter)
    public static void Main (string[] args)
    {
         string example = "Name;Middlename;Lastname;Age";
         string result = StringFunctions.AfterFirst(example, ";");
         Console.WriteLine(result);   
    }

// Output from console: Middlename;Lastname;Age



AfterLast(string source, string delimeter)
    public static void Main (string[] args)
    {
         string example = "Name;Middlename;Lastname;Age";
         string result = StringFunctions.AfterLast(example, ";");
         Console.WriteLine(result);   
    }

// Output from console: Age



SplitPortion(string source, char delimeter, int index)
    public static void Main (string[] args)
    {
         //               0    1          2        3
         string example = "Name;Middlename;Lastname;Age";
         // Get the string after the second delimeter
         string result = StringFunctions.SplitPortion(example, ';', 2);
         Console.WriteLine(result);   
    }

// Output from console: Lastname




The StringFunctions class itself:

using System;
using System.Text;
using System.Collections.Generic;

namespace KCore
{
	public static class StringFunctions
	{
		/// <summary>
		/// Split the source string by a delimeter at a specific location
		/// </summary>
		/// <returns>A string before the delimeter location or null if fail</returns>
		/// <param name="source">The string to split</param>
		/// <param name="delimeter">char delimeter.</param>
		/// <param name="index">The delimeter count</param>
		public static string SplitPortion(string source, char delimeter, int index)
		{
			if (string.IsNullOrEmpty(source) == true || string.IsNullOrEmpty(source) == true)
				return null;

			int delimeterCount = 0; // Increases by one each time delimeter is found
			bool foundDelimeter = false;
			StringBuilder returnValue = new StringBuilder();

			// loop through the string one character at a time
			for(int i = 0; i < source.Length; i++)
			{
				// if index is set to zero then build string til it encounters the first delimeter!
				if (index == 0)
				{
					if (source[i] == delimeter) // found, break out
					{
						break;
					}
					else
					{
						returnValue.Append(source[i]);
					}
				}
				// if delimeter was found on a previous character build string til we encounter the next one
				else if (foundDelimeter == true)
				{
					if (source[i] != delimeter)
					{
						returnValue.Append(source[i]);
					}
					else
					{
						break;
					}
				}
				// if delimeter is found increase the count
				else if (source[i] == delimeter)
				{
					delimeterCount++;
					if (delimeterCount == index)
					{
						foundDelimeter = true;
					}
				}
			}
			return returnValue.ToString();
		}

		/// <summary>
		/// Parses a string before the last encountered delimeter.
		/// </summary>
		/// <returns>The string after the first delimeter encountered or null if not found.</returns>
		/// <param name="source">Source string</param>
		/// <param name="delimeter">Delimeter string</param>
		public static string BeforeLast(string source, string delimeter)
		{
			if (string.IsNullOrEmpty(source) == true || string.IsNullOrEmpty(source) == true)
				return null;

			int location = source.LastIndexOf(delimeter);
			if (location > -1)
			{
				return source.Substring(0, location);
			}
			return null;
		}

		/// <summary>
		/// Parses a string after the last encountered delimeter.
		/// </summary>
		/// <returns>The string after the last delimeter encountered or null if not found.</returns>
		/// <param name="source">Source string</param>
		/// <param name="delimeter">Delimeter string</param>
		public static string AfterLast(string source, string delimeter)
		{
			if (string.IsNullOrEmpty(source) == true || string.IsNullOrEmpty(source) == true)
				return null;

			int location = source.LastIndexOf(delimeter);
			if (location > -1)
			{
				int startPosition = location + delimeter.Length;
				int length = source.Length - startPosition;
				return source.Substring(startPosition, length);
			}
			return null;
		}

		/// <summary>
		/// Parses a string before the first encountered delimeter.
		/// </summary>
		/// <returns>The string before the first delimeter encountered or null if not found.</returns>
		/// <param name="source">Source string</param>
		/// <param name="delimeter">Delimeter string</param>
		public static string BeforeFirst(string source, string delimeter)
		{
			if (string.IsNullOrEmpty(source) == true || string.IsNullOrEmpty(source) == true)
				return null;

			int location = source.IndexOf(delimeter);
			if (location > -1)
			{
				return source.Substring(0, location);
			}
			return null;
		}

		/// <summary>
		/// Parses a string after the first encountered delimeter.
		/// </summary>
		/// <returns>The string after the first delimeter encountered or null if not found.</returns>
		/// <param name="source">Source string</param>
		/// <param name="delimeter">Delimeter string</param>
		public static string AfterFirst(string source, string delimeter)
		{
			if (string.IsNullOrEmpty(source) == true || string.IsNullOrEmpty(source) == true)
				return null;

			int location = source.IndexOf(delimeter);
			if (location > -1)
			{
				int startPosition = location + delimeter.Length;
				int length = source.Length - startPosition;
				return source.Substring(startPosition, length);
			}
			return null;
		}
	}
}



Is This A Good Question/Topic? 0
  • +

Page 1 of 1