locked
Mid statement in C# RRS feed

  • Question

  • Is there an equivalent of Mid statement from vb in C#?

    for example i wanna do:

    string myval = abcde;

    mid(myval,2,1) = "x"; // myval = "axcde"

     

    Monday, May 15, 2006 9:40 PM

Answers

  • As VB.NET and C# are OO, they don't have these sort of non class methods.  Instead, in this case, it's the SubString method of the string class, or to do a replace, use the Replace method.

     Beware - this is an expensive method, a new string is created each time a string is modified.

     

    Monday, May 15, 2006 9:50 PM

All replies

  • As VB.NET and C# are OO, they don't have these sort of non class methods.  Instead, in this case, it's the SubString method of the string class, or to do a replace, use the Replace method.

     Beware - this is an expensive method, a new string is created each time a string is modified.

     

    Monday, May 15, 2006 9:50 PM
  • I believe converting the string to an array and work from there will be a better solution.
    Monday, May 15, 2006 9:57 PM
  • The following C# version produced with our Instant C# VB to C# converter is slightly more complex, but works:

    string myval = abcde;
    myval = myval.Remove(1, 1).Insert(1, "x";); // myval = "axcde"

    David Anton
    www.tangiblesoftwaresolutions.com
    Instant C#: VB to C# converter
    Instant VB: C# to VB converter
    Instant C++: C# to C++ converter and VB to C++ converter
    Instant J#: VB to J# converter
    Clear VB: Cleans up VB.NET code
    Clear C#: Cleans up C# code

    Tuesday, May 16, 2006 12:59 AM