Mid function in c#.net or equivalent-VBForums
Results 1 to 4 of 4

Thread: Mid function in c#.net or equivalent

  1. #1

    Thread Starter
    Fanatic Member daimous's Avatar
    Join Date
    Aug 2005

    Mid function in c#.net or equivalent

    is there any function in C#.net that work the same as Mid function in VB? or is there any function that work the same as MID? thanks in advance!!!

  2. #2
    Super Moderator RobDog888's Avatar
    Join Date
    Apr 2001
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™

    Re: Mid function in c#.net or equivalent

    Yes, its called the .Substring method of your string variable or .Text property of some control.

    VB Code:
    1. Dim strIP As String
    2. Dim strStart As Integer
    3. Dim strEnd As Integer
    4. Dim strString As String = "FRQ [ 859.3125]"
    5. strStart = strString.IndexOf("[") + 1
    6. strEnd = strString.LastIndexOf("]") - 1
    7. strIP = strString.Substring(strStart, strEnd - strStart)
    8. MessageBox.Show(strIP.Trim) '859.3125
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Star Wars Gangsta Rap Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Corsair H100i v2 water cooler, Geforce GTX1060, Samsung M.2 500 GB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2010, VS 2010

  3. #3
    Fanatic Member
    Join Date
    Jan 2006

    Re: Mid function in c#.net or equivalent

    As RobDob pointed out, the Mid function equivalent is easy in .NET, but if you were thinking of the Mid statement (e.g., Mid(x, 2, 3) = "abc"), that is *extremely complex* to generalize to a pure .NET solution.

    For example:
    Mid(x, 2) = "abc"

    is equivalent to:
    x = x.PadRight(x.Length + ("abc").Length).Remove(1, ("abc").Length).Insert(1, "abc").Substring(0, x.Length);

    You see my point - you would obviously want to write your own Mid statement.
    David Anton
    Convert between VB, C#, C++, & Java

  4. #4
    New Member
    Join Date
    Aug 2008

    Re: Mid function in c#.net or equivalent

    There is none, but it is quite simple to write one using Substring

    public static class strMid
    public static string Mid(string s, int a, int b)
    string temp = s.Substring(a - 1, b);
    return temp;


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts


Click Here to Expand Forum to Full Width