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
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
LVL 34

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 86

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 34

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.

738 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