Solved

c# equivalent for VB Mid Function?

Posted on 2015-01-07
4
97 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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

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…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…

776 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