c# equivalent for VB Mid Function?

Posted on 2015-01-07
Last Modified: 2015-01-08
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.

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

Open in new window

Question by:Sheryl Landis
  • 2
LVL 33

Expert Comment

by:Mike Eghtebas
ID: 40537212
public void Say(int iColumn, string s)
    string temp = "";
    temp = strData.Substring(0, iColumn) + s + strData.Substring(iColumn);
    strData = temp;

Open in new window

this works.
LVL 85

Accepted Solution

Mike Tomlinson earned 250 total points
ID: 40537245
Why not just do?...
public void Say(int iColumn, string s)
    strData = strData.Insert(iColumn, s);

Open in new window

See String.Insert():
LVL 33

Assisted Solution

by:Mike Eghtebas
Mike Eghtebas earned 250 total points
ID: 40537318

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.

Author Closing Comment

by:Sheryl Landis
ID: 40537634
Great solution!  Thank you

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

25 Experts available now in Live!

Get 1:1 Help Now