• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 151
  • Last Modified:

c# equivalent for VB Mid Function?

Hi!  I am trying to insert a string into another string, and cannot figure out the correct syntax.  The code here does not work.  Please show me a work around.
Thanks!

public void Say(int iColumn, string s)
           {
            strData.Substring(iColumn,s.Length ) = s;
           }

Open in new window

0
Sheryl Landis
Asked:
Sheryl Landis
  • 2
2 Solutions
 
Mike EghtebasDatabase and Application DeveloperCommented:
public void Say(int iColumn, string s)
{
    string temp = "";
    temp = strData.Substring(0, iColumn) + s + strData.Substring(iColumn);
    MessageBox.Show(temp.ToString());
    strData = temp;
}

Open in new window


this works.
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Why not just do?...
public void Say(int iColumn, string s)
{
    strData = strData.Insert(iColumn, s);
}

Open in new window


See String.Insert():
http://msdn.microsoft.com/en-us/library/system.string.insert(v=vs.110).aspx
0
 
Mike EghtebasDatabase and Application DeveloperCommented:
Sheryl,

strData = strData.Insert(iColumn, s);

is better solution. You may need to use like

strData = strData.Insert(iColumn + 1, s);

because the first character index in your string starts from 0.
0
 
Sheryl LandisAuthor Commented:
Great solution!  Thank you
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now