Solved

c# equivalent for VB Mid Function?

Posted on 2015-01-07
4
95 Views
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.
Thanks!

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

Open in new window

0
Comment
Question by:Sheryl Landis
  • 2
4 Comments
 
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);
    MessageBox.Show(temp.ToString());
    strData = temp;
}

Open in new window


this works.
0
 
LVL 85

Accepted Solution

by:
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():
http://msdn.microsoft.com/en-us/library/system.string.insert(v=vs.110).aspx
0
 
LVL 33

Assisted Solution

by:Mike Eghtebas
Mike Eghtebas earned 250 total points
ID: 40537318
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
 

Author Closing Comment

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

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Article by: Ivo
Anonymous Types in C# by Ivo Stoykov Anonymous Types are useful when  we do not need to follow usual work-flow -- creating object of some type, assign some read-only values and then doing something with them. Instead we can encapsulate this read…
This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

708 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

13 Experts available now in Live!

Get 1:1 Help Now