String Left, Right And Mid Functionalities In C# Winforms

In this post, we will learn how to handle string operations for common methods.

In this post, we will learn how to handle string operations for common methods.

When I planned to migrate the VB6 products to .NET technologies, I thought VB.NET is a better choice since it has the similarity of VB6 and it has most predefined functionalities.

I have the knowledge of Visual Basic 6 and wished to learn a new language so I preferred C# .NET. But I faced a lot of problems to find the functionalities equivalent to VB6 while migrating to C# Winforms. Finally, after a lot of research, I got the solution, that is shared below with you.

Here, I have included the methods for String operations of Left, Right, Mid, and Instring.
 
Find the below code for the Right string.
  1. public static string strRight(string strValue, int intLength)  
  2. {  
  3.    string strRet = string.Empty;  
  4.    try  
  5.    {  
  6.       strRet = strValue.Substring(strValue.Length - intLength);  
  7.    }  
  8.    catch (Exception ex)  
  9.    {  
  10.       strRet = strValue;  
  11.    }  
  12. return strRet;  
  13. }  
Find the below code for  the Left string.
  1. public static string strLeft(string strValue, int intLength)  
  2. {  
  3.    string strRet = string.Empty;  
  4.    try  
  5.    {  
  6.       if (string.IsNullOrEmpty(strValue)) strRet = strValue;  
  7.       intLength = Math.Abs(intLength);  
  8.       strRet = strValue.Length <= intLength ? strValue : strValue.Substring(0, intLength);  
  9.    }  
  10.    catch (Exception ex)  
  11.    {  
  12.       strRet = strValue;  
  13.    }  
  14. return strRet;  
  15. }  
 Find the below code for the Middle string.
  1. public static string strMid(string strValue, int intStartPos, int intCharCnt)  
  2. {  
  3.    string strRet = string.Empty;  
  4.    try  
  5.    {  
  6.       strRet = strValue.Substring(intStartPos - 1, intCharCnt);  
  7.    }  
  8.    catch (Exception ex)  
  9.    {  
  10.       strRet = strValue;  
  11.    }  
  12. return strRet;  
  13. }  
 Find the below code for finding the particular character position in a given string.
  1. private int instr(int StartPos, String SearchString, char chrInput, int IgnoreCaseFlag)  
  2. {  
  3.    int result = 1;  
  4.    if (IgnoreCaseFlag == 1)  
  5.    {  
  6.       result = SearchString.IndexOf(chrInput.ToString(), StartPos, StringComparison.OrdinalIgnoreCase);  
  7.    }  
  8.    else  
  9.       result = SearchString.IndexOf(chrInput.ToString(), StartPos);  
  10.       return result;  
  11. }  
 You can write these above procedures in class files and use them all over your project .
 
Note
The other way of getting these functionalities without writing these methods include references of Microsoft.VisualBasic in your project and include "using Microsoft.VisualBasic;" this line in your .cs file where you want to use VB6 equivalent functionalities and follow below codes,
  • For Left ---> Strings.Left("VisualBasic', 2)
  • For Right ---> Strings.Right("VisualBasic', 2)
  • For Mid ---> Strings.Mid("VisualBasic',7, 5)
  • For Instring --> Strings.InStr(1, "Visual/Basic", "/")