top of page
Search
  • Writer's pictureSiddharth Bhamare

How to print first, second occurrence or unique characters from string using Extension method

Updated: Jan 12, 2020


First of all, let's understand what is extension method :

Basically extension methods enables us to add a new method in existing type without modifying the existing type.


For example, we have string and it's methods like toUpper, toLower etc like this you can add your own methods in string. Extension methods are a special kind of static method, but they are called as if they were instance methods on the extended type.


Rules to create extension method

1. Extension method must be defined in static class, you have to create static class to define your extension method.


2. Extension method must have this instance to point your type.

e.g.

public static class MyExtensionClass

{

public static int GetCount(this string astr)

{

return astr.Lenght;

}

}

USE : string lstrInput = "My First Blog";

lstrInput.GetCount(); // It will return length.



Please refer below program to get occurrence of characters using Extension method.


 

using System;


namespace StringManipulation

{

public static class StringManipulation

{

public static string SecondOccurenceChars(this string astrInputString)

{

astrInputString = astrInputString.ToLower();

int[] arrAscii = new int[256];

string lstrFinalOutput = string.Empty;

for(int i=0;i<astrInputString.Length;i++)

{

if(arrAscii[Convert.ToInt32(astrInputString[i])] > 0)

{

lstrFinalOutput = lstrFinalOutput + astrInputString[i];

}

arrAscii[Convert.ToInt32(astrInputString[i])]++;

}

return lstrFinalOutput;

}

public static string UniqueChars(this string astrInputString)

{

astrInputString = astrInputString.ToLower();

int[] arrAscii = new int[256];

string lstrFinalOutput = string.Empty;

for (int i = 0; i < astrInputString.Length; i++)

{

arrAscii[Convert.ToInt32(astrInputString[i])]++;

}


for (int i = 0; i < astrInputString.Length; i++)

{

if(arrAscii[Convert.ToInt32(astrInputString[i])] == 1)

lstrFinalOutput = lstrFinalOutput + astrInputString[i];

}

return lstrFinalOutput;

}


public static string DupChars(this string astrInputString)

{

astrInputString = astrInputString.ToLower();

int[] arrAscii = new int[256];

string lstrFinalOutput = string.Empty;

for (int i = 0; i < astrInputString.Length; i++)

{

if (arrAscii[Convert.ToInt32(astrInputString[i])] > 0 && arrAscii[Convert.ToInt32(astrInputString[i])] != 9999)

{

lstrFinalOutput = lstrFinalOutput + astrInputString[i];

arrAscii[Convert.ToInt32(astrInputString[i])] = 9999;

}

else if (arrAscii[Convert.ToInt32(astrInputString[i])] != 9999)

{

arrAscii[Convert.ToInt32(astrInputString[i])]++;

}

}

return lstrFinalOutput;

}


}

class Program

{

static void Main(string[] args)

{

string input = "AAssjjkkLLlx";

Console.WriteLine("Second Occurence of Char " + input.SecondOccurenceChars());

Console.WriteLine("Unique Charcters in string " + input.UniqueChars());

Console.WriteLine("Duplicate Charcters once occurance " + input.DupChars());

Console.WriteLine("Hello World!");

}

}

}




33 views0 comments
Post: Blog2_Post
bottom of page