Very often when migrating traditional windows application written in VB6, we may come across the inbuilt functions which were available in VB6, but not available in C#. One such function is Mid Function.
Visual Basic has a Mid function and a Mid statement. These elements both operate on a specified number of characters in a string, but the Mid function returns the characters while the Mid statement replaces the characters.
Mid Function has following parameters
str
Required. String from which characters will be returned.
Start
Required. Integer. Start position of the characters to be returned. If Start position is greater than the number of characters in str, the Mid function returns a zero-length string (""). Start is one based.
Length
Optional. Integer. Number of characters to be returned. If this parameter is omitted or if number of characters is less than length of text (including the character at position Start), all characters from the start position to the end of the string are returned.
Mid Statement
Replaces a specified number of characters in a String variable with characters from another string.
Mid Statement has the following parameters.
Target
Required. Name of the String variable to be modified.
Start
Required. Integer. Position in target where the replacement of text begins. Start uses a one-based index.
Length
Optional. Integer. Number of characters to be replaced. If this parameter is omitted, all of the String is used.
StringExpression
Required. String expression that replaces part of Target.
Here is the C# version for the same code.
- /// <param name="newChar"> Character to be replaced.</param>
- /// <returns></returns>
- public static string Mid(string input, int index, char newChar)
- {
- if (input == null)
- {
- throw new ArgumentNullException("input");
- }
- char[] chars = input.ToCharArray();
- chars[index-1] = newChar;
- return new string(chars);
- }
- /// <summary>
- /// This is equivalent to Mid Function in VB6
- /// </summary>
- /// <param name="s"> String to Check.</param>
- /// <param name="a">Position of Character</param>
- /// <param name="b">Length </param>
- /// <returns></returns>
- public static string Mid(string s, int a, int b)
- {
- string temp = s.Substring(a - 1, b);
- return temp;
- }
- }

Tomasz ChyraPosted Jun 10, 2019, 5:49 AM
How to change it to code? :-/ Public static string Mid(string str, int start) { return Mid(str, start, str.Length - start + 1); } /// <summary> /// Att!: first position in string is 1 not 0! /// </summary> /// <param name="str">String to cut</param> /// <param name="start">1-based (first char is on position 1)</param> /// <param name="length">How many chars is to take</param> /// <returns></returns> public static string Mid(string str, int start, int length) { var ret = ""; try { if (str == null) { ret = ""; } else if (length <= 0) { ret = ""; } else if (start > (str ?? "").Length) { ret = ""; } else if (start < 1) { //Start from 1st char, but take less chars because don't take chars at minus positions ret = Mid(str, 1, length - (Math.Abs(start) - 1) ); } else if (start + length - 1 > str.Length) { //read chars only to end of string ret = Mid(str, start); } else { //Normally read piece of string ret = str.Substring(start - 1, length); } } catch (Exception ex) { ret = ""; } return ret; }
Tomasz ChyraPosted Jun 8, 2019, 5:59 AM
It is not equivalent cause in some cases this statement causes exception, byt Mid in VB does not!