TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

C# String Between, Before, After

This C# article introduces the Between, Before and After extension methods.

Between, before, after. With extension methods, we easily locate Substrings.

We search for substrings between two strings, before a string or after a string. In these extension methods, we return relative substrings. This simplifies certain programs.

Based on:

.NET 4.5

Example. The first part of the code presents the SubstringExtensions static class. This contains the extension methods Between, Before and After. The Between method accepts two strings (a and b) and locates each in the source string.

Then: After some error checking, it returns the substring between them. The Before method and After method are similar.

SubstringExtensions.cs implementation: C#

static class SubstringExtensions
{
    /// <summary>
    /// Get string value between [first] a and [last] b.
    /// </summary>
    public static string Between(this string value, string a, string b)
    {
	int posA = value.IndexOf(a);
	int posB = value.LastIndexOf(b);
	if (posA == -1)
	{
	    return "";
	}
	if (posB == -1)
	{
	    return "";
	}
	int adjustedPosA = posA + a.Length;
	if (adjustedPosA >= posB)
	{
	    return "";
	}
	return value.Substring(adjustedPosA, posB - adjustedPosA);
    }

    /// <summary>
    /// Get string value after [first] a.
    /// </summary>
    public static string Before(this string value, string a)
    {
	int posA = value.IndexOf(a);
	if (posA == -1)
	{
	    return "";
	}
	return value.Substring(0, posA);
    }

    /// <summary>
    /// Get string value after [last] a.
    /// </summary>
    public static string After(this string value, string a)
    {
	int posA = value.LastIndexOf(a);
	if (posA == -1)
	{
	    return "";
	}
	int adjustedPosA = posA + a.Length;
	if (adjustedPosA >= value.Length)
	{
	    return "";
	}
	return value.Substring(adjustedPosA);
    }
}

Main method: C#

using System;

class Program
{
    static void Main()
    {
	// Input.
	const string test = "DEFINE:A=TWO";

	// Test Between.
	Console.WriteLine(test.Between("DEFINE:", "="));
	Console.WriteLine(test.Between(":", "="));

	// Test Before.
	Console.WriteLine(test.Before(":"));
	Console.WriteLine(test.Before("="));

	// Test After.
	Console.WriteLine(test.After(":"));
	Console.WriteLine(test.After("DEFINE:"));
	Console.WriteLine(test.After("="));
    }
}

Output

A
A
DEFINE
DEFINE:A
A=TWO
A=TWO
TWO

How does the example code in the Main entry point work? An input string 'test' is declared. The extensions Between, Before, and After are used on it. The methods seem to be parsing a programming language syntax.

And: The results are printed to the console (with Console.WriteLine) after the parts are extracted.

Use. Sometimes, a programmer decides a problem is best solved with a small language. However, often the requirements are not complex enough to warrant a full grammar and a parser engine.

So: You could use a simple loop along with these extensions to parse a small language.

Do they work? These extension methods are in use in a program I developed, and they seem to work correctly. There may be some errors in the error-handling logic in some cases—this has not been studied.

Summary. We provided the implementations for Between, Before, and After extensions on the string type. These implementations are not fully compatible with all requirements. They only search for the first or last instance of the parameters.

However: They are useful in simplifying parsing engines for simple custom languages.


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf